Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Find Dots-Per-Inch (DPI) of Screen in Android Programmatically?

Improve Article
Save Article
Like Article
  • Last Updated : 30 Jan, 2022
Improve Article
Save Article
Like Article

Dots-Per-Inch or DPI is a measure of pixel density over the physical area on the screen. A pixel is the smallest unit of any screen display. and the sum of all the pixels present on the screen is termed as Screen Resolution. The pixels available to the user are called Viewport and in this article, we will show you how you could fetch the DPI of the Viewport in Android.

Dots-Per-Inch (DPI) of Screen

Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package org.geeksforgeeks.screendpi
import android.os.Bundle
import android.util.DisplayMetrics
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and Initializing the 
          // TextView from the layout file
        val mTextView = findViewById<TextView>(
        // Fetching the screen DPI value
        val mScreenDPI = resources.displayMetrics.density
        mTextView.text = mScreenDPI.toString()



My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!