January 3, 2010

[SOLVED] Set Screen Orientation Programmatically


There are times where you need to ensure that your application is displayed only in a certain orientation. For example, suppose you are writing a game that should only be viewed in landscape mode. In this case, you can programmatically force a change in orientation using the setRequestOrientation() method of the Activity class:

package net.learn2develop.OrientationAware;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

public class Orientation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//---change to landscape mode---
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}

To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);




Developing Orientation-Aware Android Applications.

10 comments:

  1. [...] via [SOLVED] Set Screen Orientation Programmatically « RussenReaktor’s Weblog. [...]

    ReplyDelete
  2. Is this possible from outside of an activity? So I can force an app, that's not controlled by me, to change orientation?

    Thanks
    Tom

    ReplyDelete
  3. Just put attach a command to the Intent before it's sent, and retrieve it when the class gets instantiated.

    In the intent
    intent.putExtra("orientation", "portrait");


    In the class
    String orientation = getIntent().getExtras().getString("orientation");
    if(orientation == "portrait"){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
    // defaults to landscape
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    ReplyDelete
  4. [...] http://russenreaktor.wordpress.com/2010/01/03/solved-set-screen-orientation-programmatically/ [...]

    ReplyDelete
  5. You can purchase a micro - SD memory stick which has two gigabytes (GB) of memory while also purchasing
    a memory stick Pro - Duo which only has one GB
    or less of memory space. The following suggestions should be considered before buying a memory card:
    a. The memory card reader is used in usb-capable pc and other devices helping the user in collecting information from
    the system which is placed in the memory card reader.

    ReplyDelete
  6. you are really a just right webmaster. The site loading pace
    is amazing. It seems that you are doing any unique trick. Moreover, The contents are masterpiece.
    you have done a magnificent process on this subject!

    ReplyDelete
  7. You are so cool! I do not think I've read anything like this before. So nice to discover someone with some original thoughts on this topic. Seriously.. thank you for starting this up. This web site is one thing that is required on the web, someone with a bit of originality!

    ReplyDelete
  8. wonderful post, very informative. I'm wondering why the opposite experts of this sector do not realize this. You must proceed your writing. I'm confident, you have a
    great readers' base already!

    ReplyDelete
  9. Hmm is anyone else having problems with the pictures on this blog
    loading? I'm trying to find out if its a problem on my end or if it's the blog.
    Any feedback would be greatly appreciated.

    ReplyDelete