Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

How to save the position of a ScrollView when the orientation changes in Android July 28, 2011

Filed under: Android,Coding — Rupert Bates @ 2:29 pm

To save and restore the scroll position of a ScrollView when the phone orientation changes you can do the following:
Save the current position in the onSaveInstanceState method:

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putIntArray("ARTICLE_SCROLL_POSITION",
                new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()});
    }

Then restore the position in the onRestoreInstanceState method, note that we need to post a runnable to the ScrollView to get this to work:

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        final int[] position = savedInstanceState.getIntArray("ARTICLE_SCROLL_POSITION");
        if(position != null)
            mScrollView.post(new Runnable() {
                public void run() {
                    mScrollView.scrollTo(position[0], position[1]);
                }
            });
    }
Advertisements
 

Make the backspace key work in Firefox on Linux July 21, 2011

Filed under: Linux — Rupert Bates @ 10:55 am

To make the backspace key in Firefox on Linux take you back to the previous page as it does on other platforms and in other browsers do the following:

  1. put about:config in the address bar and hit enter
  2. scroll down to browser.backspace_action and double click it
  3. set the value to 0
 

Commit working directory changes to a new branch in git July 1, 2011

Filed under: Git — Rupert Bates @ 3:30 pm

To create and checkout a new branch in git taking any uncommitted changes with you use:

git checkout -b my_new_branch