Skip to content

Set entire Android Application in Portrait mode only(Portrait Orientation)- Kotlin

  • by

On Developing Android application, some application has needed only Portrait mode only (Game: Candy crush) or Portrait Orientation for an entire(full) app or particular on activity. Here we are sharing some common ways to achieve Portrait Mode.

How to Set entire Android Application in Portrait mode only(Portrait Orientation)- Kotlin

How to disable landscape mode for some of the views in my Android app?

There are 2 ways to achieve Activity level Orientation control.

1.  Add android:screenOrientation="portrait" to the activity in the AndroidManifest.xml.

<activity android:name=".MyActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait" />

2.  Setting programmatically in Java

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Note : In Activity you should call this before setContentView in onCreate() method.

In Kotlin same can be programmatically achieved using this code

requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

And of Landscape in Kotlin

 requestedOrientation =  (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Complete code in Kotlin

import android.content.pm.ActivityInfo
import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation =  (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main)
    }
}

How to set an entire android application in portrait mode only?

We are giving example in Kotlin language, Using 2 activity for proper testing, should work on both activities.

for beginners follow this tutorial first Build Your First Android App in Kotlin

Step 1. Create MyApplication class and extend Application class

add following code and methods

package `in`.eyehunt.androidapplicationportraitmode

import android.app.Activity
import android.app.Application
import android.content.pm.ActivityInfo
import android.os.Bundle

/**
 * Created by Eyehunt Team on 31/05/18.
 */
class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {

            override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
                activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            }

            override fun onActivityPaused(activity: Activity?) {
            }

            override fun onActivityResumed(activity: Activity?) {
            }

            override fun onActivityStarted(activity: Activity?) {
            }

            override fun onActivityDestroyed(activity: Activity?) {
            }

            override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
            }

            override fun onActivityStopped(activity: Activity?) {
            }
        })
    }
}
Step 2. Don’t forget to add an application in  AndroidManifest.xml.
    <application
        android:name=".MyApplication"
        ...
    </application>

Complete code AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.eyehunt.androidapplicationportraitmode">

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
Step 3. Create resource layout file activity_second.xml and add following code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="56dp"
        android:text="Second Activity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Step 4. Create SecondActivity.kt and add this code
package `in`.eyehunt.androidapplicationportraitmode

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

/**
 * Created by Eyehunt Team on 31/05/18.
 */
class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
    }
}
Step 5. Add button in activity_main.xml file, complete code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="in.eyehunt.androidapplicationportraitmode.MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_goto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:text="Go to Second Activity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

</android.support.constraint.ConstraintLayout>
Step 6. Add button_goto.setOnClickListener in MainActivity.kt

for not more about Kotlin Android must read this tutorial: Kotlin Android basic fundamental codes

package `in`.eyehunt.androidapplicationportraitmode

import android.content.Intent
import android.content.pm.ActivityInfo
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //requestedOrientation =  (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main)
        button_goto.setOnClickListener(View.OnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        })
    }
}
Step 6. Run the application, in an emulator or On your Android device

Output screenshots Android Application in Portrait mode kotlin example :

Android Application in Portrait mode kotli n
Android Application in Portrait Orientation kotlin

Download source code Android Application in Portrait mode example :

Tag: How to disable landscape mode for the entire application?

or How to force the Android app only “Portrait” orientation mode?

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

Note : This example (Project) is developed in Android Studio 3.0.1 ,tested on Android 7.1.1 ( Android Nougat), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”26″

Coding in Kotlin

Leave a Reply

Your email address will not be published. Required fields are marked *