android - How to get screen orientation

activity_main.xml
 <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="10dp"     tools:context=".MainActivity"     android:background="#c9cdaf"     >     <TextView         android:id="@+id/tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:fontFamily="Sans-Serif-Condensed"         android:textSize="18dp"         android:textStyle="italic"         android:textColor="#000000"         android:padding="15dp"         />     <Button         android:id="@+id/btn"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Get Screen Orientation"         android:layout_below="@id/tv"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.app.ActionBar; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.content.res.Configuration;   public class MainActivity extends Activity {      protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the widgets reference from XML layout         selesai RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);         selesai Button btn = (Button) findViewById(R.id.btn);         selesai TextView tv = (TextView) findViewById(R.id.tv);          ActionBar ab = getActionBar();         ab.setBackgroundDrawable(new ColorDrawable(Color.RED));          btn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 // Get the activity                 Activity activity = MainActivity.this;                  tv.setText("Screen current orientation : "                         + getScreenOrientation(activity));             }         });     }      // Custom method to get screen current orientation     public static String getScreenOrientation(Activity activity) {         /*             We also can get orientation by using context instead activity             Context.getResources().getConfiguration().orientation;         */          String orientation = "";         /*             public Resources getResources ()                 Return a Resources instance for your application's package.         */          /*             Resources                 The Android resource system keeps track of all non-code assets associated                 with an application. You can use this class to access your application's                 resources. You can generally acquire the Resources instance associated                 with your application with getResources().         */          /*             Configuration                 This class describes all device configuration information that can impact                 the resources the application retrieves. This includes both user-specified                 configuration options (locale and scaling) as well as device configurations                 (such as input modes, screen size and screen orientation).                  You can acquire this object from Resources, using getConfiguration().                 Thus, from an activity, you can get it by chaining the                 request with getResources().         */          /*             public int orientation                 Overall orientation of the screen. May be one                 of ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT.         */          /*             public static selesai int ORIENTATION_LANDSCAPE                 Constant for orientation, value corresponding to the land resource qualifier.                 Constant Value: 2 (0x00000002)              public static selesai int ORIENTATION_PORTRAIT                 Constant for orientation, value corresponding to the port resource qualifier.                 Constant Value: 1 (0x00000001)         */         int currentOrientation = activity.getResources().getConfiguration().orientation;          if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {             orientation = "Landscape";         }          if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {             orientation = "Portrait";         }          return orientation;     } } 

More android examples

Komentar