Table of Contents
What is the DisplayMetrics class ?
The DisplayMetrics class is used to get various information about the screen in android .The information that we can get are the following :
DENSITY_DEVICE_STABLE :The density of the device in ppi read from the system settings , this density does not change.densityDpi :The user can change the display size in android , and set it to Small , Default , Large , Larger , Largest . This will affect the device density . densityDpi is the DENSITY_DEVICE_STABLE plus or minus the set display size.

density :this is the densityDpi divided by the default density which is 160 ppi . So if we want to know how much a dp will be equal in pixels on this device , we can multiply the dp by the density.scaledDensity :this is the density multiplied by the set font size , which can be one of the following : small , normal , large , huge . So it will be the density multiplied by 0.85 , 1 , 1.15 , 1.3 . The scale density can be used to calculate how much an sp is equal in pixels , by multiplying the sp by the scalDensity .widthPixels :the device width in pixelsheightPixels :the device height in pixelsxdpi :the density of the device on the x axis . This is equal to DENSITY_DEVICE_STABLEydpi :the density of the device on the y axis . This is equal to DENSITY_DEVICE_STABLE.
DisplayMetrics class an example
In this application we are using the DisplayMetrics class to get various information about the screen . The application is formed of a MainActivity.java file
package com.twiserandom.a002displaymetrics;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
DisplayMetrics displayMetrics;
TextView tvDisplayMetrics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//call the super method
setContentView(R.layout.activity_main);
// set the content view to the layout activity main
tvDisplayMetrics = (TextView) findViewById(R.id.tvDisplayMetrics);
displayMetrics = getResources().getDisplayMetrics();
// Get the displayMetrics for the current device
//width and height
tvDisplayMetrics.append(String.format("widthPixels is : %d \n", displayMetrics.widthPixels));
tvDisplayMetrics.append(String.format("heightPixels is : %d \n\n", displayMetrics.heightPixels));
// DENSITY_DEVICE_STABLE : read from the system property
tvDisplayMetrics.append(String.format("DENSITY_DEVICE_STABLE is : %d \n\n", DisplayMetrics.DENSITY_DEVICE_STABLE));
// densitydpi : DENSITY_DEVICE_STABLE ± display size [Small , Default , Large , Larger , Largest]
// density : ratio of densitydpi / 160
tvDisplayMetrics.append(String.format("densityDpi (= DENSITY_DEVICE_STABLE ± display size [Small , Default , Large , Larger , Largest]) is : %d \n\n", displayMetrics.densityDpi));
tvDisplayMetrics.append(String.format("density is densitydpi / 160 : %f \n\n", displayMetrics.density));
// scaleDensity [ ratio of (densityDpi / 160) * fontScale ]
tvDisplayMetrics.append(String.format("scaledDensity [ ratio of (densityDpi / 160) * [small :0.85 , normal 1 , large 1.15 , huge 1.3] ]: %f \n\n", displayMetrics.scaledDensity));
tvDisplayMetrics.append(String.format("xdpi is : %f \n", displayMetrics.xdpi));
tvDisplayMetrics.append(String.format("ydpi is : %f \n", displayMetrics.ydpi));
}
}
, and of an activity_main.xml file .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:gravity="center"
android:text="Display Metrics"
android:textSize="20dp">
</TextView>
<TextView
android:id="@+id/tvDisplayMetrics"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:padding="20dp"
android:textSize="16dp">
</TextView>
</LinearLayout>This application is run on the nexus 4 smartphone .
When the display size is set to default , and the font size is set to normal , we have the following display metrics :
- widthPixels : 768
- heightPixels : 1184
- DENSITY_DEVICE_STABLE : 320
- densityDpi : 320
- denisy : 2
- scaleDensity : 2
- xdpi : 320
- ydpi : 320
so on this phone ,
- 1dp is equal to 1px multiplied by the density and is equal to 2 px.
- 1sp is equal to 1px multiplied the scaleDensity and is equal to 2px.

When the display size is set to Large , and the font size is set to normal , we have the following display metrics :
- widthPixels : 768
- heightPixels : 1174
- DENSITY_DEVICE_STABLE : 320
- densityDpi : 352
- denisy : 2.2
- scaleDensity : 2.2
- xdpi : 320
- ydpi : 320
so on this phone ,
- 1dp is equal to 1px multiplied by the density and is equal to 2.2 px.
- 1sp is equal to 1px multiplied the scaleDensity and is equal to 2.2px

When the display size is set to Default , and the font size is set to large , we have the following display metrics :
- widthPixels : 768
- heightPixels : 1184
- DENSITY_DEVICE_STABLE : 320
- densityDpi : 320
- denisy : 2.0
- scaleDensity : 2.3
- xdpi : 320
- ydpi : 320
so on this phone ,
- 1dp is equal to 1px multiplied by the density and is equal to 2.0 px.
- 1sp is equal to 1px multiplied the scaleDensity and is equal to 2.3 px.

When the display size is set to Larger , and the font size is set to largest , we have the following display metrics :
- widthPixels : 768
- heightPixels : 1165
- DENSITY_DEVICE_STABLE : 320
- densityDpi : 384
- denisy : 2.4
- scaleDensity : 3.12
- xdpi : 320
- ydpi : 320
so on this phone ,
- 1dp is equal to 1px multiplied by the density and is equal to 2.4 px.
- 1sp is equal to 1px multiplied the scaleDensity and is equal to 3.12 px.


