Question

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?

Was it helpful?

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

OTHER TIPS

As described by Bill Gary above, why not just let the system take care of orientation changes for you? Also the standard system widgets should automatically preserve their state when you do this. See here for why and how to manage custom state, if you need to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top