Find/Get screen density in android programmatically

How to detect android mobile phone device screen current dpi dynamically on button click and show on screen.

Screen density represents the current dpi size of screen that supports your android mobile phone device. Because all of the mobile phones supports different-different dpi versions. So in this tutorial we are determine the current device dpi and display that on screen using TextView. So here is the complete step by step tutorial for Find/Get screen density in android programmatically.

android-project-download-code-button

List of screen density according to dp sizes :

  • 0.75 – ldpi
  • 1.0 – mdpi
  • 1.5 – hdpi
  • 2.0 – xhdpi
  • 3.0 – xxhdpi
  • 4.0 – xxxhdpi

How to Find/Get screen density in android programmatically.

Code for MainActivity.java file.

 package com.getscreendensity_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 Button button;
 TextView textview;
 float value = 0;
 DisplayMetrics displaymetrics;
 Activity activity;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 textview = (TextView)findViewById(R.id.textView1);
 
 activity = MainActivity.this;
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 displaymetrics = new DisplayMetrics();
 
 activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
 
 value = getResources().getDisplayMetrics().density;
 
 textview.setText("Screen Density = " + String.valueOf(value));
 
 
 }
 });
 }
}

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.getscreendensity_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:layout_marginTop="180dp"
 android:text="Density Show 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/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:text="Click here to Find/Get screen density in android programmatically" />

</RelativeLayout>

Screenshot:

Get screen density in android programmatically

Click here to download Find/Get screen density in android programmatically project with source code.

Leave a Reply

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