Disable screen orientation change in android programmatically

How to stop automatically screen rotating mode to portrait to landscape in android app.

Every Android mobile phone gives us two screen mode changing buttons, Portrait mode and Landscape mode button to change our phone screen orientation. But sometimes some of application dose not required changing screen orientation so android app developer can stop android app layout orientation moving using single function. So here is the complete step by step tutorial for Disable screen orientation change in android programmatically using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); method.

android-project-download-code-button

How to Disable screen orientation change in android programmatically.

Code for MainActivity.java file.

 package com.disablescreenorientationchange_android_examples.com;

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


public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.disablescreenorientationchange_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Disable screen orientation change in android programmatically"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Screenshot:

Disable screen orientation change in android programmatically

Click here to download Disable screen orientation change in android programmatically project.

Leave a Reply

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