get Screen Density - Android User Interface

Android examples for User Interface:Screen Resolution

Description

get Screen Density

Demo Code


//package com.java2s;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class Main {
    public static float getScreenDensity(Context ctx) {
        float density = 0;
        WindowManager manager = (WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = manager.getDefaultDisplay();

        DisplayMetrics metrics = new DisplayMetrics();
        defaultDisplay.getMetrics(metrics);

        density = metrics.density;//from  ww w. ja  v a2  s  .c om
        return density;
    }
}

Related Tutorials