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 live video, gaming, etc for 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.

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. The code for that has been given in both Java and Kotlin Programming Language for Android.
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 that 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
<? xml version = "1.0" encoding = "utf-8" ?>
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< Button
android:id = "@+id/btn"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerInParent = "true"
android:text = "click" />
</ RelativeLayout >
|
Step 3: Working with the MainActivity file
Go to the MainActivity file, and refer to the following code. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.
Kotlin
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)
val btn = findViewById<Button>(R.id.btn)
btn.setOnClickListener {
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width = size.x
val height = size.y
Toast.makeText(applicationContext, "Width: $width Pixels , Height: $height Pixels" , Toast.LENGTH_LONG).show()
}
}
}
|
Java
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(v -> {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Toast.makeText(getApplicationContext(), "Width: " + width + " Pixels , Height: " + height + " Pixels" , Toast.LENGTH_LONG).show();
});
}
}
|
Output: Run on Emulator