I must admit I fell into a bit of a trap on this experience. As a result I will present failed attempts I had at detecting an orientation change and then give the solution I ended up with at the end of the article. My goal was to change the layout of some widgets when in portrait mode. I had designed the screen for landscape mode. Then when I rotated it on the emulator I was unsatisfied with the results. I had set up my layout so that only two LinearLayouts needed to have their orientation set from horizontal to verticle. I then set out to discover the best way to detect a change in orientation.
The first approach I tried was to use an android.view.OrientationEventListener. I used some slightly modified code from
this article to test it out. I think that article is great, but I was less than satisfied with the results from OrientationEventListener. First the emulator appeared to take longer when switching orientation. But the biggest problem was the onOrientationChanged() method was never called! Very disappointing! I assume I forgot to set something up properly, but it is hard without finding some proper documentation. I have also seen various blog articles stating problems with it since 2007 and it still doesn't seem to function as one would think here in 2010. :(
My second approach was to follow
this form post. I thought this was a cleaver little trick that just might do the job for me. I got it to work by adding
android:configChanges="orientation" to my AndroidManifest.xml file and
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
to my Activity. I had to add this method without the @Override compile and run and then add back the @Override. Eclipse was throwing an error at me. Within the newConfig parameter was an orientation property that I used for changing the layout logic. I really liked this option but the downside was onConfigurationChanged() was only being called when changing from landscape to portrait which is all I really needed but I didn't like that it only worked half the time it was suppose to.
The third approach I looked at was from
this article. I read the article which I liked and saw the comments about SensorManager's API has been changed three times, and could use at least one more update in my opinion. I thought that I would be able to make it work and was able to until I tried to register the SensorEventListener with the SensorManager. At this point I considered it another failure and maybe a lost cause.
After these three failed attempts I changed my thinking. I had been trying to find a clean and efficient way of detecting an orientation change and changing a couple properties on my layouts. A new thought that I had was what if I just create an entirely new layout to be shown in portrait mode.
These are the steps I used.
I opened up my xml layout that I wanted to change. At the top were some options that look like this.
I changed the config drop down to portrait, and then hit the create button on the right hand side.
The only option I selected was portrait.
and with that I had a new xml that I was editing and setting up for portrait mode. When I run the app in the emulator it chooses the right layout for portrait and the other layout for landscape.
Done deal.
P.S.
Which turns out to be a good thing because the portrait layout is going to be arranged completely different now. :)