Get device screen width Height in pixels android programmatically

Discover activity screen dimensions size into pixels format dynamically via coding in android on button click.

Every live screen on planet contain its screen dimensions into pixels and all of the already defined its screen size on box but some of android devices dose not tell the phone user to its size into pixels format. With the help of pixels format app developer can easily make applications right as the screen sizes including multiple screen support by creating unique layout for each and every screen size. So here is the complete step by step tutorial for Get device screen width Height in pixels android programmatically.android-project-download-code-button

How to Get device screen width Height in pixels android programmatically.

Code for MainActivity.java file.

package com.getdevicescreenwidthheighpixels;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.TextView;


public class MainActivity extends Activity {

 TextView one,two;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 one = (TextView)findViewById(R.id.textView2);
 two = (TextView)findViewById(R.id.textView4);
 
 DisplayMetrics displayMetrics = new DisplayMetrics();
 WindowManager windowmanager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
 windowmanager.getDefaultDisplay().getMetrics(displayMetrics);
 int deviceWidth = displayMetrics.widthPixels;
 int deviceHeight = displayMetrics.heightPixels;
 
 one.setText(String.valueOf(deviceWidth));
 two.setText(String.valueOf(deviceHeight));
 
 }
}

Code for activity_main.xml layout file.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.getdevicescreenwidthheighpixels.MainActivity" >

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="16dp"
 android:text="Large Text"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="176dp"
 android:text="Width in Pixels"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignRight="@+id/textView1"
 android:layout_below="@+id/textView2"
 android:layout_marginTop="24dp"
 android:text="Height in Pixels"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/textView2"
 android:layout_below="@+id/textView3"
 android:layout_marginTop="16dp"
 android:text="Large Text"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots:

Get device screen width Height in pixels android programmatically

Click here to download Get device screen width Heigh in pixels android programmatically project.