How to Find the Screen Resolution of a Device Programmatically in Android?

Screen Resolution refers to the number of pixels on display. A higher resolution means more pixels and more pixels provide the ability to display more visual information. This entity is widely used in applications related to the broadcasting of real-time visuals such as a live video, gaming, etc for the optimization and frame conversions. The same information can also be used to detect if there is damage to any of the pixels present on the screen. Practically, it is possible to retrieve this information. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

Sample gif

Approach

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file



Go to the activity_main.xml file which represents the UI of the application, and create a Button which on click would generate a Toast displaying the number of pixels available at the width and length. Below is the code for the activity_main.xml file.

XML

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!--Button which onclick creates a Toast Message-->
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="click" />
  
</RelativeLayout>

chevron_right


Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file, and refer 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.

Kotlin

filter_none

edit
close

play_arrow

link
brightness_4
code

import android.graphics.Point
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declare the button from the layout file
        val btn = findViewById<Button>(R.id.btn)
  
        // Action when the button is clicked
        btn.setOnClickListener {
  
            // get default display from the windows manager
            val display = windowManager.defaultDisplay
  
            // declare and initialize a point
            val size = Point()
  
            // store the points related details from the 
            // display variable in the size variable
            display.getSize(size)
  
            // store the point information in integer 
            // variables width and height
            // where .x extracts width pixels and 
            // .y extracts height pixels
            val width = size.x
            val height = size.y
  
            // Toast will display the width and height values
            Toast.makeText(
                applicationContext,
                "Width: $width Pixels , Height: $height Pixels",
                Toast.LENGTH_LONG
            ).show()
        }
    }
}

chevron_right


Output: Run on Emulator




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.