Wednesday, 29 February 2012

Android - handling rotation/orientation in an Activity - the easy way!

As I've been asked a few times about the easy way to handle rotation in an Android Activity, I thought I'd outline what you need to do here. It is really easy when you know how!

Firstly, you need to ensure that you create your layouts such that they resize easily. See this post for more details:

Secondly, if you find that you need a different layout for your Activity (say in Landscape mode), create it in a parallel folder to your portait layout like this...:


In your mainfest xml activity... block, make sure you put this:


In your Activity's onCreate() method implementation, pretty much all you need to do is call this method:


Create the following override method in your Activity (this is called when the orientation changes as your Activity is rotated):

public void onConfigurationChanged(Configuration newConfig) {

  // Copy current item values from all your UI gadgets...!
  String myvalue = myitem.toString();
  // etc...

  // Call the method that re-orients and re-creates the gadget bindings

  // Restore the gadget values to the post-rotation UI:

Finally, you can create your new method, which makes your Activity use the correct layout to suit your orientation, and which looks-up all the gadgets, prepares their data and sets-up their event handlers (remember: this is called both at creation time, and whenever a rotation event occurs!):

private void myConfigureUI() {

  // Can check the orientation of the screen...
  if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  } else {

  // This will automatically pick-up the landscape layout or portrait layout...

  // Look-up new items etc. ... mySpinner is an instance variable in this example...
  mySpinner = (Spinner) findViewById(;

  mySpinner .setOnItemSelectedListener(new OnItemSelectedListener() { ...
  // etc. ...


Francis Western-Smith said...

Getting more and more excited about the Android version of mixtikl!
I know it's probably a silly question, but any idea of roughly when it'll be ready?
I'm all set with an Advent Vega with a new rom and trying to resist the temptation of a Galaxy Tab 7.7!
Great to hear you have plans for more Android versions of your software!

Pete Cole said...

Hi Francis!

We're currently working though the process for getting Mixtikl for Android approved for the Amazon app store. I'd hope that Mixtikl will appear there in a few weeks - I'm not sure exactly when however! :)

Do please note however that the Amazon app store is currently accessible only to Amazon customers in the US - I'd *hope* that they'll roll this out worldwide at some point, but we'll have to see what happens I guess!

Best wishes,


Francis Western-Smith said...

Yeah, that's a bit of a pain. I'll be doing all I can to find a way around it...
Good to hear how close it is!

Pete Cole said...

In case you're interested, take a look here...!

Best wishes,


Pete Cole said...

It has been announced in the press that the Amazon Android store will be rolled-out to other countries from this summer!

Lucas Rosseti said...

Hi! My name is Lucas, I'm new in android development and now I have problems with configuration change in Galaxy Tab2.
I've did the same thing you do, but it not work in Galaxy Tab2, what I need to do to handling the configuration change on it?
Thank you for any help...