How to get Android Screen Size in pixels and inches programmatically

How to get Android Screen Size in pixels and inches programmatically

In this tutorial, we are going to learn how to get android screen size in pixels and inches programmatically. This will be a simple tutorial but you will find it useful in different areas during android application development.

We are going to create a simple interface that will portray what we are planning to achieve.

We are going to use three TextView controls to label the device height, width and the screen size in inches. Also the ImageView is simply use to illustrate the mobile device and the corresponding TextView components will be use for label.

In order to understand what we are planning to do, we are going to show a screen shot of what we will achieve in this tutorial as shown below.

sizeapp

Before we start, it is important for you to understand the tools and environment I used in this application development tutorial. Feel free to use any tools you are familiar with.

Windows 7

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidScreenSize

Package: com.inducesmile.androidscreensize

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button.

If you are using Android Studio as your choice IDE, the new project will create a default MainActivity.java file and its corresponding layout file. Open the two files because we are going to make use of them.

Let try to recreate android UI that is similar to what is found in the image above. As I stated above, we are going to use three TextView and one ImageView control. Open the activity_main.xml layout file in your project, copy and paste the code in the file.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/text_icon">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:baselineAligned="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:paddingLeft="8dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/height_pixel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/primary_text"
                android:textSize="20sp"
                android:text="720"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="0.4">

            <TextView
                android:id="@+id/inches_pixel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/primary_text"
                android:layout_gravity="center|top"
                android:layout_marginTop="16dp"
                android:textSize="20sp"
                android:text="4 inches"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="400dp"
                android:contentDescription="@string/hello_world"
                android:src="@drawable/mobiletool"/>

            <TextView
                android:id="@+id/width_pixel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/primary_text"
                android:layout_gravity="center"
                android:layout_marginBottom="24dp"
                android:textSize="20sp"
                android:text="420"/>


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1">

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

The layout xml code above is simple. Take time and go through it so that you will understand it more. You are free to adjust or modify the layout file in other to suit your project specification better.

We will move on the our main activity class – MainActivity.java. The class will use the layout file as its interface display. We will get the handles of the TextView controls we added to our layout file.

Thereafter, we will create a method called getScreenDimension(). This method will to used to get our screen width and height dimension in pixel and the screen size of the device in inches. This information will be returned as a string array.

The complete code for the MainActivity.java file is as shown below.

package inducesmile.com.completeandroid;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ScreenSizeFragment extends Fragment {

    public ScreenSizeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_screen_size, container, false);

        TextView heightInPixel = (TextView)view.findViewById(R.id.height_pixel);
        TextView widthInPixel = (TextView)view.findViewById(R.id.width_pixel);
        TextView inches = (TextView)view.findViewById(R.id.inches_pixel);

        String[] screenData = getScreenDimension();

        widthInPixel.setText(screenData[0]);
        heightInPixel.setText(screenData[1]);
        inches.setText(screenData[2]);

        return view;
    }

    private String[] getScreenDimension(){
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        int dens = dm.densityDpi;
        double wi = (double)width / (double)dens;
        double hi = (double)height / (double)dens;
        double x = Math.pow(wi, 2);
        double y = Math.pow(hi, 2);
        double screenInches = Math.sqrt(x+y);

        String[] screenInformation = new String[3];
        screenInformation[0] = String.valueOf(width) + " px";
        screenInformation[1] = String.valueOf(height) + " px" ;
        screenInformation[2] = String.format("%.2f", screenInches) + " inches" ;

        return screenInformation;
    }

}

The returned are from the getScreenDimension() is assigned to the individual instances of the TextView.

Now, when you run your application you will see the interface that looks similar to the sample that was shown earlier on.

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published.

If you like this tutorial, please kindly download my new android app in Google play store by clicking the image below

Add a Comment