How to Detect Screen Resolution of a Device in Java

Hello learners!. In this tutorial, we will know how to detect the screen resolution of a device in Java.

Detect Screen Resolution of a Device in Java

Firstly, We use the Java Toolkit library from the Java AWT (Abstract Window Toolkit) package in our code by importing it. The Java Toolkit class is an abstract class of the AWT package which has various component and native toolkit implementations. For more details about Toolkit, its components, methods, please refer to the below posts:

As a result, we can perform many GUI (Graphic User Interface) operations and create objects asynchronously using the Java Toolkit class. It contains many methods for obtaining the size of the window, desktop properties, events, etc. We will now look into a method of the Toolkit class to get our device screen resolution.

Using the getScreenSize() Method

We use the getScreenSize() method of the Java Toolkit class which is used to obtain the screen size in pixels. Furthermore, if multiple displays are present, it will obtain the screen size of the primary native display used.

We then use the Dimension class to store the width and height information obtained from the getScreenSize() method. We use the Dimension class because many methods of Java AWT return dimension objects. However, the Dimension class can be used to store the height and width in an integer as well as double-precision format.

Let’s see the code:

//Java Program to detect the screen resolution of a device in Java.
import java.awt.*;

public class ScreenResolution {
    public static void main(String[] args) 
        Toolkit toolkit=Toolkit.getDefaultToolkit(); //Defining the Toolkit from Java AWT
        Dimension screen = toolkit.getScreenSize(); //Obtains the screen size of the device
        int height = (int)screen.getHeight(); //Obtains the height of the device
        int width = (int)screen.getWidth(); //Obtains the width of the device
        System.out.println("Device Screen Resolution is : " + width+" x "+height);
        System.out.println("Width of the screen is : " + width);
        System.out.println("Height of the screen is : " + height);


Device Screen Resolution is : 1536 x 864
Width of the screen is : 1536
Height of the screen is : 864


We observe that the screen size of our device is displayed in pixels using the getScreenSize() method. We can also use the getScreenResolution() method instead to obtain the screen size information in dots per inch.

Thanks for Reading!

Leave a Reply

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