53

I want to make my app work only in landscape mode but can't make it work. I have given screenOrientation = "landscape" even though the first page will be in landscape mode and other activity will be in portrait.

XML FILE

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Main"
              android:label="@string/app_name"
              android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>     

    </activity>

    <activity android:name=".IntroHome"
              android:label="@string/app_name"
              android:screenOrientation="landscape">
    </activity>

    <activity android:name=".ObjectivesPage"
              android:label="@string/app_name"
              android:screenOrientation="landscape" >
    </activity>

    <activity android:name=".MenuPage"
              android:label="@string/app_name"
              android:screenOrientation="landscape" >
    </activity>
</application>

JAVA CLASS

public class ObjectivesPage extends Activity{

    ImageButton  imgButton;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.objectivespage);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        imgButton = (ImageButton)findViewById(R.id.buttonCloseNGo);
        imgButton.setOnClickListener(onClickCloseNGo);
    }

    private OnClickListener onClickCloseNGo = new OnClickListener(){

        public void onClick(View v) {
            Intent intent = new Intent(ObjectivesPage.this,MenuPage.class);
            startActivity(intent);
        }
    };
}
Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
shripal
  • 1,222
  • 4
  • 19
  • 40

5 Answers5

89

Keep this part of the manifest as it already is. For example, consider the IntroHome activity.

<activity android:name=".IntroHome"
           android:label="@string/app_name"
           android:screenOrientation="landscape"  
           >
</activity>

And for the activity XML, make sure you have the IntroHome activity layout XML only in the layout-land folder. This way, the activity / activities you have will only show the the landscape version of the XML that you have defined.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • Hmmm... Strange. It works perfect like that in my app. In fact, now that i checked, i had an earlier XML in the default layout folder and the particular shows up just in its landscape version anyway. The activity does not go to its portrait version at all. – Siddharth Lele Dec 30 '10 at 10:09
  • shripal, did you add to your activity? It has to be in the AndroidManifest file. – Gerard Sep 25 '14 at 05:31
3

You can also try setting the orientation from your code

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Check this link for more info

http://www.devx.com/wireless/Article/40792/0/page/5

DeRagan
  • 22,827
  • 6
  • 41
  • 50
  • no friend all the this types of the possibilities i have already tried please any other solutiong.....i am also providing you my xml file in my question – shripal Dec 30 '10 at 09:54
  • setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) should work perfectly. If it doesn't probably there is something wrong in your implementation. I have used this successfully in the past as well – DeRagan Dec 30 '10 at 10:16
  • 1
    Thanks! The XML entry android:screenOrientation="landscape" did not work, but this code-based solution worked perfectly. – CoderOfTheNight Nov 10 '15 at 11:38
  • Beware! it will recreate activity which may cause some other problems. – Salman Khalid Dec 31 '18 at 13:45
3

You can use following code as per requirement:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); and setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

you have to put these code before setContentView(R.layout.layout_name.xml).

chalitha geekiyanage
  • 6,464
  • 5
  • 25
  • 32
Sudipta Som
  • 6,537
  • 9
  • 43
  • 67
1

Try adding:

android:configChanges="orientation|keyboardHidden"

Let me know if it helps!! Just a guess!!

Prateek Jain
  • 1,234
  • 14
  • 24
-2

This looks like an old post, but it looks to me like if you set the mode to landscape in the manifest file, and then switch the mode to landscape in the java code, you might be switching the mode twice causing the device to render in portrait mode instead. Try commenting out the code in your java file and see what happens. If it doesn't work all you have to do is change it back. Easy peasy test.

James
  • 7
  • 1