Check Android ScreenOrientation At Runtime

In this Post we will see how to check android screen-orientation at Run-time , in the previous series i have covered how to fixed the Activity (Screen) to any particular orientation (i.e either Landscape or Portrait ) .
Android Portrait Mode Android LandScape Mode


  1. Firstly we will get the Resources object of application and.
  2. Then we will get the configuration object that is in effect with resource object  .
  3. The configuration class contains a constants whose values may be one of ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT. using this constant we will get to know what is the current orientation.

Resources res = getResources();
Configuration conf = res.getConfiguration();

int flag = conf.orientation;


1. XML Layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text=""
        android:textSize="25sp" />

</LinearLayout>

2. Activity


package com.pavan.screenorientationdemo;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView display;

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

  display = (TextView) findViewById(R.id.textView1);

  Resources res = getResources();
  Configuration conf = res.getConfiguration();

  int flag = conf.orientation;

  if (flag == Configuration.ORIENTATION_PORTRAIT) {
   display.setText("In PORTRAIT MODE");
  } else if (flag == Configuration.ORIENTATION_LANDSCAPE) {
   display.setText("In LANDSCAPE MODE");
  }

 }

}

1 comment:

MUHAMMADKALEEMULLAH KHAN said...

One of my whtsapp cntct is showing custom but all others are mobile why is it

Post a Comment