Find/Get current screen orientation in android programmatically

How to detect and show android device screen orientation mode name landscape,portrait on screen.

In this tutorial we are programmatically detecting the android device orientation mode on screen using activity.getResources() .getConfiguration() .orientation . So here is the complete step by step tutorial for Find/Get current screen orientation in android programmatically.

android-project-download-code-button

How to Find/Get current screen orientation in android programmatically.

Code for MainActivity.java file.

 package com.getcurrentscreen0rientation_android_examples.com;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 Button button;
 TextView textview;
 Activity activity;
 String orientation;
 int value;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 activity = MainActivity.this;
 textview = (TextView)findViewById(R.id.textView1);
 button = (Button)findViewById(R.id.button1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 value = activity.getResources().getConfiguration().orientation;

 if (value == Configuration.ORIENTATION_PORTRAIT) {
 
 orientation = "Portrait";
 }
 
 if (value == Configuration.ORIENTATION_LANDSCAPE) {
 
 orientation = "Landscape";
 }
 
 textview.setText(" Current Screen Orientation = " + orientation);
 
 }
 });
 }
}

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.getcurrentscreen0rientation_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:text="Orientation Show Here"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="41dp"
 android:text="Click here to Get current screen orientation in android programmatically" />

</RelativeLayout>

Screenshots:

Find/Get current screen orientation in android programmatically

 

orientation-2

Click here to download Find/Get current screen orientation in android programmatically project with source code.

Leave a Reply

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