Get device screen width and height in android in DP programmatically

How to dynamically detect,find and show android mobile phone screen dimensions in DP.

In this tutorial we are making an android application which can easily detect the current running android mobile phone device screen size in DP format. Because android phone supports dp not pixels so after discovering any device screen dimensions into DP, can help to developer to create more accurate android apps with more smooth and clear screen resolution. So here is the complete step by step tutorial for Get device screen width and height in android in DP programmatically.

android-project-download-code-button

How to Get device screen width and height in android in DP programmatically.

Code for MainActivity.java file.

 package com.screenwidthheightdp_android_examples.com;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView one,two;
 Button button;
 int deviceWidth,deviceHeight,height,width = 0;
 Context context;
 DisplayMetrics displayMetrics;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 one = (TextView)findViewById(R.id.textView1);
 two = (TextView)findViewById(R.id.textView2);
 button = (Button)findViewById(R.id.button1);
 context = getApplicationContext();
 
 displayMetrics = new DisplayMetrics();
 
 WindowManager windowmanager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
 
 windowmanager.getDefaultDisplay().getMetrics(displayMetrics);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 height = Math.round(displayMetrics.heightPixels / displayMetrics.density);
 
 width = Math.round(displayMetrics.widthPixels / displayMetrics.density);
 
 one.setText("Width = " + String.valueOf(width) + " - DP");
 
 two.setText("Height = " +String.valueOf(height) + " - DP");
 }
 });
 }
 
 
 
}

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.screenwidthheightdp_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Width Display Here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <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="18dp"
 android:text="Height Display Here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="42dp"
 android:text="Click here to Get device screen width and height in android in DP programmatically" />

</RelativeLayout>

Screenshot:

Get device screen width and height in android in DP programmatically

Click here to download Get device screen width and height in android in DP programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *