Posts Tagged ‘configuration’

h1

Orientation

March 31, 2009

Originally I had hoped that users would not want to deal with the keyboard when in the gym, so I came up with a simple number pad to enter the workout data.  If the user wanted to open the keyboard the proportion of the screen made it so that the number pad was unreachable.  First fatal mistake – since the save button was on the number pad.  Anyway….WorkItOut 1.1 fixed that.

The goal in 1.1 was to show the number pad in portrait mode and hide the number pad in landscape mode when the keyboard slides out.

Here are the steps to achieve this:

1)  Go to the AndroidManifest

For the activity add android:configChanges=”keyboardHidden|orientation”

This allows the application to handle orientation changes and makes it so the app does not restart the activity on an orientation change.  I use it for the charts also so that the app doesn’t have to ping the Google Charts site every time you change screen orientation.

2)  Use onConfigurationChanged(Configuration change)

So now that the app is controlling the configuration changes, you have to set the behavior.  I just use an if statement with change.orientation == Configuration.ORIENTATION_LANDSCAPE

If it’s landscape hide the numpad, otherwise display it.

3)  Use getWindowManager().getDefaultDisplay().getMetrics(dm)

onConfigurationChanged only comes into play when you switch from one view to another within the activity.  But what if you enter the activity in landscape mode.  For that I used the WindowManager to get the screen dimension.  If dm.heightpixels==320 then I know I am in landscape mode.

So these steps cover all the bases for sliding the keyboard open or closing it.  Obviously there is more complexity if you want to use the accelerometer to determine which orientation the screen is currently in.