I want my android application to be only run in portrait mode?android


Answers

In Android Manifest File, put attribute for your <activity> that android:screenOrientation="portrait"

Question

I want my android application to be only run in portrait mode? How can I do that?




How to set app to be portrait only?

As seen in the question: I want my android application to be only run in portrait mode?

You should set the configChanges and screenOrientation flags for every activity, and not at the root of your manifest.

So it should look like this:

<activity android:name=".MainActivity"
  android:configChanges="orientation"
  android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>



How can we set in application run only vertically not horizontally?

You can specify in your activity in manifest.xml how you want it to work

e.g

<activity
android:screenOrientation="landscape"> //for landscape ie horizontal

and

<activity
android:screenOrientation="portrait"> //for portrait ie vertical



You do this in the AndroidManifest.xml file on the activity. Take a look at the Activity element documentation.

<activity android:name=".MyCustomActivity"
android:screenOrientation="portrait"
[...]

Programmatically you can do this in your activity as follows:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);



Android Screen Orientation Change (Portrait- Landscape)

1. Lock screen orientation change in Android

Your screen will always display in portrait mode, when you rotate your device, no changes will apply for the current activity.

android:screenOrientation="portrait"

if you want to lock the whole application screens. you should write the above code to all activities of the application.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mWidas2.mWidas2" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
  <uses-sdk />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <application android:label="WQS" android:icon="@drawable/temporaryIcon">
    <activity android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:configChanges="keyboard|keyboardHidden|orientation" />
  </application>
</manifest>

2. Lock screen orientation (screen rotation) programmatically in Android

This option is little tricky. What we need to do is, we will get the screen orientation change (rotation change) event, then both for landscape and portrait mode event, we will set ether landscape or portrait mode as per our requirement. Below is the code. We will set portrait mode always, so the screen will always stay in portrait mode.

// Check screen orientation or screen rotate event here
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen for landscape and portrait and set portrait mode always
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

3. Change screen orientation programmatically at anytime in Android

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

4. How to get screen orientation at runtime

//Get current screen orientation
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        int orientation = display.getOrientation(); 
         switch(orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;                  
        }

5. Find screen orientation change event in Android

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen for landscape and portrait
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }