Saturday, September 26, 2015

android - How to get screen size

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="#fff9dc"
    android:animateLayoutChanges="true"
    >
    <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 Size"
        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.util.DisplayMetrics;



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);
        final TextView tv = (TextView) findViewById(R.id.tv);

        ActionBar ab = getActionBar();
        ab.setBackgroundDrawable(new ColorDrawable(Color.YELLOW));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Activity activity = MainActivity.this;
                tv.setText("Screen width : " + getScreenWidthInPixels(activity) + "pixels");
                tv.setText(tv.getText() + "\nScreen height : "
                        + getScreenHeightInPixels(activity) + "pixels");
           }
        });

    }

    // Custom method to get android device screen width in pixels
    public static int getScreenWidthInPixels(Activity activity){
        /*
            DisplayMetrics
                A structure describing general information about a display,
                such as its size, density, and font scaling.
        */
        DisplayMetrics dm  = new DisplayMetrics();
        /*
            public WindowManager getWindowManager ()
                Retrieve the window manager for showing custom windows.
        */

        /*
            WindowManager
                The interface that apps use to talk to the window manager.
                Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these.
                Each window manager instance is bound to a particular Display.
        */

        /*
            public abstract Display getDefaultDisplay ()

                Returns the Display upon which this WindowManager instance will create new windows.

                Despite the name of this method, the display that is returned is not necessarily
                the primary display of the system (see DEFAULT_DISPLAY). The returned display
                could instead be a secondary display that this window manager instance
                is managing. Think of it as the display that this WindowManager instance uses by default.

                To create windows on a different display, you need to obtain a WindowManager
                for that Display. (See the WindowManager class documentation for more information.)

                Returns
                The display that this window manager is managing.
        */

        /*
            public void getMetrics (DisplayMetrics outMetrics)

                Gets display metrics that describe the size and density of this display.

                The size is adjusted based on the current rotation of the display.

                The size returned by this method does not necessarily represent the actual
                raw size (native resolution) of the display. The returned size may be adjusted
                to exclude certain system decor elements that are always visible. It may also
                be scaled to provide compatibility with older applications that were
                originally designed for smaller displays.

                Parameters
                outMetrics A DisplayMetrics object to receive the metrics.
        */
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

        /*
            public int widthPixels
                The absolute width of the display in pixels.
        */
        int width = dm.widthPixels;
        return width;
    }

    // Custom method to get android device screen height in pixels
    public static int getScreenHeightInPixels(Activity activity){
        DisplayMetrics dm = activity.getResources().getDisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

        /*
            public int heightPixels
                The absolute height of the display in pixels.
        */
        int height = dm.heightPixels;
        return height;
    }
}
More android examples