To check and get details about the screen on an Android device, you can use the Android SDK and the DisplayMetrics class to retrieve information like the screen size, resolution, density, and more. Below is an example of how to write Android code to check screen properties such as screen size, resolution, and density.

1. Getting Screen Size and Resolution Using DisplayMetrics

Here's a simple code snippet that demonstrates how to retrieve the screen resolution and density in Android:

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the DisplayMetrics object
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        // Screen Width and Height in pixels
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;

        // Screen Density (dots per inch)
        float screenDensity = displayMetrics.density;

        // DPI (dots per inch)
        int screenDpi = displayMetrics.densityDpi;

        // Displaying the results
        TextView screenInfo = findViewById(R.id.screenInfo);
        screenInfo.setText("Screen Width: " + screenWidth + "px\n" +
                "Screen Height: " + screenHeight + "px\n" +
                "Density: " + screenDensity + "\n" +
                "DPI: " + screenDpi);
    }
}

Explanation:

  1. DisplayMetrics: This class provides various display properties like width, height, density, and DPI.

  2. widthPixels and heightPixels: These give the screen width and height in pixels.

  3. density: This gives the screen density as a float. For example:

    • 1.0 means 160 dpi (density-independent pixels, dp, are based on this value).
    • 1.5 means 240 dpi, and so on.
  4. densityDpi: This provides the exact DPI value of the screen, such as DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_MEDIUM, etc.

2. Checking Screen Orientation

You can also check whether the screen is in portrait or landscape mode using getResources().getConfiguration():

import android.content.res.Configuration;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView orientationInfo = findViewById(R.id.orientationInfo);

        // Check if the device is in portrait or landscape mode
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            orientationInfo.setText("Landscape Mode");
        } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            orientationInfo.setText("Portrait Mode");
        }
    }
}

3. Full Example: Get Screen Information

Here is a full example of how to display various screen information (such as width, height, density, orientation, etc.) in an Android app.

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.content.res.Configuration;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the DisplayMetrics object
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        // Screen Width and Height in pixels
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;

        // Screen Density (dots per inch)
        float screenDensity = displayMetrics.density;

        // DPI (dots per inch)
        int screenDpi = displayMetrics.densityDpi;

        // Check the screen orientation
        String orientation = "";
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            orientation = "Landscape";
        } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            orientation = "Portrait";
        }

        // Displaying the results
        TextView screenInfo = findViewById(R.id.screenInfo);
        screenInfo.setText("Screen Width: " + screenWidth + "px\n" +
                "Screen Height: " + screenHeight + "px\n" +
                "Density: " + screenDensity + "\n" +
                "DPI: " + screenDpi + "\n" +
                "Orientation: " + orientation);
    }
}

In this example:

  • The screen’s width and height are fetched using widthPixels and heightPixels.
  • The screen’s density (in dp) is fetched using density.
  • The DPI (dots per inch) of the screen is provided via densityDpi.
  • The screen orientation is checked, and based on the result, either "Portrait" or "Landscape" is shown.

4. Other Screen Information

You can also retrieve additional screen information, like the screen's density-independent pixels (dp) or pixel ratio. However, note that Android automatically handles screen scaling, so most applications should focus on using density-independent pixels (dp) instead of pixels when designing layouts for different screen sizes and densities.

Conclusion:

This code will give you an overview of the screen properties of the Android device. You can easily check the screen's resolution, density, and orientation using the DisplayMetrics class and other utilities. This is especially useful when developing apps that need to be responsive to different screen sizes and orientations.