Rotational Forces, Part Four

6

In the first three parts of this series, we covered ways for your Android application to cope with rotation events. Some are simple, some are more flexible.

Today, though, we’ll take a look at a radical alternative: tell Android not to rotate your activity at all. If the activity does not rotate, you do not have to worry about writing code to deal with rotations.

To block Android from rotating your activity, all you need to do is add android:screenOrientation=”portrait” (or “landscape”, as you prefer) to your AndroidManifest.xml file, as shown below:

[sourcecode language=”xml”]










[/sourcecode]

Since this is applied on a per-activity basis, you will need to decide which of your activities may need this turned on.

At this point, your activity is locked into whatever orientation you specified, regardless of what you do. The following screen shots show the activity we developed in the first three posts, using the above manifest, with the emulator set for both portrait and landscape orientation — note that the UI does not move a bit, but remains in portrait mode.

Forced-Portrait Orientation, in Portrait Mode
Forced-Portrait Orientation, in Portrait Mode
Forced-Portrait Orientation, in Landscape Mode
Forced-Portrait Orientation, in Landscape Mode

6 COMMENTS

  1. You still have to deal with the slide action. Even though you tell the activity to stay in portrait mode, the sliding open of the keyboard still destroys and recreates your activity. If you are going to use this method I would also combine it with the method mentioned in part 3 of rotational forces (android:configChanges=”keyboardHidden|orientation”). Since your screen does not change you do not have to worry about implementing the onConfigurationChanged() event, because you do not need to do anything when the keyboard slides open and closed.

  2. Hello,

    I did write an apllication that should only be displayed in landscape.
    I added the line:
    android:screenOrientation=”landscape”
    to the manifest file.
    After that the application looks fine if I start the Simulator with the Portrait shell.
    If I start the simulator with the landscape shell the apllication is displayed wrong. The simulator turns the screen 90 degrees to the right. The top of the screen is still on the right side where the phone keys are.
    Any idea what might be wrong?

    Chris

  3. recipes that involve putting epsom salts in…

    any open orifice. don’t spend good money on tinctures that promise quick results. good nutrient rich food is all any normal body needs to get and stay as clean as nature intended. you’ve designed a great blog for yourself and now…

  4. or one bad link within an article…

    could seriously damage your reputation.additionally you may find that your readership doesn’t like the guest bloggers or a guest blogger in particular. the easiest way to find this out is to periodically poll your readers to make sure they’re happy w…

Comments are closed.