Sunday, September 27, 2015

android - How to change 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="#e7ebcb"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Screen Orientation"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.ActionBar;
import android.content.pm.ActivityInfo;
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.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
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        final Button btn = (Button) findViewById(R.id.btn);

        ActionBar ab = getActionBar();
        ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFA0A38B")));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get the activity
                Activity activity = MainActivity.this;

                if(getScreenOrientation(activity) == "Landscape")
                {
                    /*
                        ActivityInfo
                            Information you can retrieve about a particular application activity
                            or receiver. This corresponds to information collected from the
                            AndroidManifest.xml's <activity> and <receiver> tags.
                    */

                    /*
                        public static final int SCREEN_ORIENTATION_PORTRAIT
                            Constant corresponding to portrait in the screenOrientation attribute.
                            Constant Value: 1 (0x00000001)
                    */

                    /*
                        public static final int SCREEN_ORIENTATION_LANDSCAPE
                            Constant corresponding to landscape in the screenOrientation attribute.
                            Constant Value: 0 (0x00000000)
                    */
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
                else if (getScreenOrientation(activity) == "Portrait")
                {
                    // Change the screen orientation to landscape
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }
        });
    }

    // Custom method to get screen current orientation
    public static String getScreenOrientation(Activity activity) {
        String orientation = "";
        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