get Screen Horizontal Size - Android android.app

Android examples for android.app:Screen

Description

get Screen Horizontal Size

Demo Code

import android.content.Context;
import android.view.ViewGroup;

public class Main {

  private static final int STANDARD_WIDTH = 720;

  @SuppressWarnings("deprecation")
  public static int getHorizontalSize(Context context, int width) {
    if (width == ViewGroup.LayoutParams.MATCH_PARENT || width == ViewGroup.LayoutParams.WRAP_CONTENT) {
      return width;
    } else {//from w  w w.j ava 2  s.com
      return (getDisplayWidth(context) * width) / STANDARD_WIDTH;
    }
  }

  public static int getDisplayWidth(Context context) {
    return context.getResources().getDisplayMetrics().widthPixels;
  }

}

Related Tutorials