Search code examples
androidscreenorientationlandscapeportrait

Orientation Change - Update UI


I use data of the accelerometer in my app. If the device´s angle is changed the screen orientation should change. In order to change the orientation I have 2 layouts, one for portrait and one for landscape.

When the orientation changes a function is called that changes the layout with:

setContentView(R.layout.landscape); or setContentView(R.layout.portrait);

This works fine but I have a problem with several UI elements like buttons or ToggleButtons. I initialize a onClickListener in the onCreate Method for them and each time the orienation gets changed I initialize a new onClickListener.

Unfortunately I can´t change the state of ToggleButtons anymore.

How can I solve this problem?


Solution

  • you can use /layout-land and /layout-port and the phone will automatically switch them for you on orientation change. see

    http://developer.android.com/guide/practices/screens_support.html#DesigningResources

    as for the toggle buttons, see onRetainNonConfigurationInstance()

    http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29