Category: Coding

  • Android: Facebook SDK “Invalid Key Hash” on Ubuntu


    I kept getting a different Facebook key hash from the one being actually generated by my debug app when it runs. Turns out I’ve been entering the wrong keystore password.

    To get your key hash for Facebook SDK on Ubuntu, run the following command:

    In case you don’t have the keytool program yet, you’ll see the following message:

    The program ‘keytool’ can be found in the following packages:
    * gcj-5-jre-headless
    * openjdk-8-jre-headless
    * gcj-4.8-jre-headless
    * gcj-4.9-jre-headless
    * openjdk-9-jre-headless
    Try: sudo apt install

    What I did was install openjdk-9-jre-headless by running the command:

    Wait for your selected program to install and try the first command again to generate key hash for Facebook on Ubuntu.

    When prompted for keystore password, try to use “android”.

  • Android: SharedPreferences not accessible from SyncAdapter

    When I was trying to access a SharedPreferences value from my Android app’s SyncAdapter, I kept on receiving an old value that was saved to the key.

    Quick solution to inaccessible SharedPreferences from SyncAdapter

    A quick solution (which may be a short-term one) I tried was to remove android:process=":sync" from the following code:

    But beware:

    The attribute android:process=”:sync” tells the system to run the Service in a global shared process named sync. If you have multiple sync adapters in your app they can share this process, which reduces overhead.

    My long-term solution to inaccessible SharedPreferences from SyncAdapter

    Source:
    SharedPreference committed in SyncAdapter not updated in Activity?

  • Android app dev: Intent extras set in onBackPressed() misssing when back to onActivityResult()

    It’s funny how we spent about an hour trying to figure out why the Intent extras that we’re setting are not being sent back to onActivityResult(). If we only knew that the solution was as simple as moving one line of code.

    The call to super.onBackPressed() should be placed after setting Intent extras and after finishing the current activity. Like so:

  • Android app dev: Missing import org.apache.commons.lang3.text.WordUtils;


    I use a few instances of WordUtils.capitalizeFully() on an Android app that I’ve been working on. And there are times when the import org.apache.commons.lang3.text.WordUtils;
    breaks, sometimes after merging with some of my git branches.

    To fix this, what I do is open app/build.gradle and add the following line under dependencies:

    Hope this saves somebody else’s day 😆 I got the solution from How to add org.apache.commons.lang3 to AndroidStudio with gradle

  • Something to avoid when using Android RecyclerView

    When using Android’s RecyclerView, don’t use the same inflated XML layout file for items you intend to shuffle on the fly! Their contents will overlap.

    -CK

  • Android RecyclerView notifyItemChanged() IllegalStateException “Cannot call this method while RecyclerView is computing a layout or scrolling”


    If your Android app is crashing with error IllegalStateException “Cannot call this method while RecyclerView is computing a layout or scrolling”, you may try placing your notifyitemchanged inside a Runnable.

    Assuming you have a call to notifyItemChanged() inside onBindViewHolder() that is making your app crash with IllegalStateException, you may try something like this:

  • Ubuntu: Show current Git branch on Terminal prompt


    There are times when I keep running git status or git branch to check which branch I’m on and I just found that I could do something to show the name of my current Git branch on my Terminal prompt, thanks to a colleague.

    In the following screenshot I’m working in the directory “sample_app”, on Git branch that is named “static-pages”:

    ubuntu-terminal-prompt-git-branch-

    In Ubuntu, here’s how I did it.

    Create .bash directory in Home:
    mkdir ~/.bash

    Go to the new directory
    cd ~/.bash

    Clone the git-aware project from GitHub:
    git clone git://github.com/jimeh/git-aware-prompt.git

    Edit your .bashrc file. I use Vim editor for this:
    vim ~/.bashrc

    Add the following to the top of your .bashrc:

    If you want colored prompts, add the following to the bottom of your .bashrc:

    Otherwise, if you doon’t like colors, use this instead:

    Open a new Terminal tab or restart Terminal. Navigate to any directory that has a Git repository and your Ubuntu’s Terminal prompt should now show the current Git branch’s name.