Friday, March 18, 2016

android - Get screen layout size category

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="16dp"
    tools:context=".MainActivity"
    android:background="#e0e6e6"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="25sp"
        android:gravity="center"
        android:textColor="#b933a3"
        android:textStyle="bold"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;


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

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);

        // Set the TextView text
        mTextView.setText("Screen Layout Size Category\n");

        /*
            public int screenLayout
                Bit mask of overall layout of the screen.

                The SCREENLAYOUT_SIZE_MASK bits define the overall size of the screen.
                They may be one of SCREENLAYOUT_SIZE_SMALL, SCREENLAYOUT_SIZE_NORMAL,
                SCREENLAYOUT_SIZE_LARGE, or SCREENLAYOUT_SIZE_XLARGE.
        */
        /*
            public static final int SCREENLAYOUT_SIZE_MASK
                Constant for screenLayout: bits that encode the size.

                Constant Value: 15 (0x0000000f)
        */

        // Determine the screen layout size category
        if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
                    == Configuration.SCREENLAYOUT_SIZE_NORMAL){
            /*
                public static final int SCREENLAYOUT_SIZE_NORMAL
                    Constant for screenLayout: a SCREENLAYOUT_SIZE_MASK value indicating the screen
                    is at least approximately 320x470 dp units, corresponds to the normal resource
                    qualifier. See Supporting Multiple Screens for more information.

                    Constant Value: 2 (0x00000002)
            */
            mTextView.setText(mTextView.getText()+"Normal");
        }else if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
                == Configuration.SCREENLAYOUT_SIZE_SMALL){
            /*
                public static final int SCREENLAYOUT_SIZE_SMALL
                    Constant for screenLayout: a SCREENLAYOUT_SIZE_MASK value indicating the
                    screen is at least approximately 320x426 dp units, corresponds to the small
                    resource qualifier. See Supporting Multiple Screens for more information.

                    Constant Value: 1 (0x00000001)
            */
            mTextView.setText(mTextView.getText()+"Small");
        }else if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
                == Configuration.SCREENLAYOUT_SIZE_LARGE){
            /*
                public static final int SCREENLAYOUT_SIZE_LARGE
                    Constant for screenLayout: a SCREENLAYOUT_SIZE_MASK value indicating the screen
                    is at least approximately 480x640 dp units, corresponds to the large resource
                    qualifier. See Supporting Multiple Screens for more information.

                    Constant Value: 3 (0x00000003)
            */
            mTextView.setText(mTextView.getText()+"Large");
        }else if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
                == Configuration.SCREENLAYOUT_SIZE_XLARGE){
            /*
                public static final int SCREENLAYOUT_SIZE_XLARGE
                    Constant for screenLayout: a SCREENLAYOUT_SIZE_MASK value indicating the screen
                    is at least approximately 720x960 dp units, corresponds to the xlarge resource
                    qualifier. See Supporting Multiple Screens for more information.

                    Constant Value: 4 (0x00000004)
            */
            mTextView.setText(mTextView.getText()+"XLarge");
        }else if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
                == Configuration.SCREENLAYOUT_SIZE_UNDEFINED){
            /*
                public static final int SCREENLAYOUT_SIZE_UNDEFINED
                    Constant for screenLayout: a SCREENLAYOUT_SIZE_MASK value indicating
                    that no size has been set.

                    Constant Value: 0 (0x00000000)
            */
            mTextView.setText(mTextView.getText()+"Undefined");
        }
    }
}