miércoles, 8 de enero de 2014

Android: Obtaining the screen size


// Screen size, it includes the action bar in its calculation
// 
private static DisplayMetrics getScreenMetricsIncludingActionBar() {

  return Resources.getSystem().getDisplayMetrics();
}

// Screen size removing the action bar
// 
private static DisplayMetrics getScreenMetricsRemovingActionBar(
Activity activity) {

  DisplayMetrics metrics = new DisplayMetrics();
 activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

  return metrics;

}

As a side note, the status bar height can be calculated like follows:
public static int getStatusBarHeight(Activity activity) {

 Rect rectgle = new Rect();
 Window window = activity.getWindow();
 window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
  
 return rectgle.top;
}

And the action bar height can be obtained like this:

XML:

?android:attr/actionBarSize

XML using ActionBarSherlock or AppCompat:

?attr/actionBarSize

Programatically:
final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
                    new int[] { android.R.attr.actionBarSize });
mActionBarSize = (int) styledAttributes.getDimension(0, 0);

styledAttributes.recycle();

No hay comentarios:

Publicar un comentario