Android How to - Get Screen Width








Question

We would like to know how to get Screen Width.

Answer

//from w w  w.j  a v  a 2  s  .  co m
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class Main {
  public static int getScreenWidth(Context context) {
    DisplayMetrics metric = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context
        .getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(metric);
    return metric.widthPixels;
  }

}