Sunday, September 27, 2015

android - How to detect orientation change

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="#d9dbd4"
    >
    <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_landscape"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Orientation Landscape"
        android:layout_below="@id/tv"
        />
    <Button
        android:id="@+id/btn_portrait"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Orientation Portrait"
        android:layout_below="@id/btn_landscape"
        />
</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;
import android.widget.TextView;


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_landscape = (Button) findViewById(R.id.btn_landscape);
        final Button btn_portrait = (Button) findViewById(R.id.btn_portrait);

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

        btn_landscape.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Set screen orientation to landscape
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        });

        btn_portrait.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Set screen orientation to portrait
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        });
    }


    /*
        public void onConfigurationChanged (Configuration newConfig)
            Called by the system when the device configuration changes while your activity
            is running. Note that this will only be called if you have selected
            configurations you would like to handle with the configChanges attribute
            in your manifest. If any configuration change occurs that is not selected
            to be reported by that attribute, then instead of reporting it the system will
            stop and restart the activity (to have it launched with the new configuration).

            At the time that this function has been called, your Resources object will
            have been updated to return resource values matching the new configuration.

            Parameters
            newConfig : The new device configuration.
    */
    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);

        TextView tv = (TextView) findViewById(R.id.tv);

        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            tv.setText("Screen orientation changed.");
            tv.setText(tv.getText() + "\nOrientation : Landscape.");
            tv.setTextColor(Color.RED);
        }

        if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            tv.setText("Screen orientation changed.");
            tv.setText(tv.getText() + "\nOrientation : Portrait.");
            tv.setTextColor(Color.BLUE);
        }
    }
}
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cfsuman.me.androidcodesnippets" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!--
            android:configChanges="orientation|screenSize"
                This prevent restarting the activity when screen orientation change.
        -->
        <!--
            public void onConfigurationChanged (Configuration newConfig)
                Called by the system when the device configuration changes while your
                activity is running. Note that this will only be called if you have
                selected configurations you would like to handle with the
                configChanges attribute in your manifest.

                So, we need to include orientation in android:configChanges,
                if we want to handle it in activity onConfigurationChanged event.
        -->
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

More android examples