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:
http://sseyod.blogspot.com/2010/09/art-of-scalable-android-layouts.html

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...:

res/layout/fred.xml
res/layout-land/fred.xml


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

android:configChanges="orientation|keyboardHidden"


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

  myConfigureUI();


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

@Override
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
  myConfigureUI();

  // Restore the gadget values to the post-rotation UI:
  myitem.setText(myvalue);
}


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) {
    setContentView(R.layout.main_landscape);
  } else {
    setContentView(R.layout.main);
  }
  */

  // This will automatically pick-up the landscape layout or portrait layout...
  setContentView(R.layout.main);

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

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

6 comments:

Anonymous 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,

Pete

Anonymous 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...!

http://www.amazon.com/Mixtikl-Generative-Music-Mixer-Sequencing/dp/B007TN4ADO/ref=sr_1_1?ie=UTF8&qid=1334311877&sr=8-1

Best wishes,

Pete

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!

Unknown 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...