Binh Le
- Forum posts: 3
Mar 1, 2014, 5:36:11 AM via Website
Mar 1, 2014 5:36:11 AM via Website
I wanna create a method that is used for all app. As following
private int getRealContentSize(){
int statusBarHeight = Common.getStatusBarHeight(this);
int actionBarHeight = Common.getActionBarHeight(this);
int navigationBarHeight = Common.getNavigationBarHeight(this);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int totalHeight = statusBarHeight + actionBarHeight + navigationBarHeight;
Log.i("BAR", "status + " + statusBarHeight +
"; action bar + " + actionBarHeight +
"; navigation " + navigationBarHeight);
if(totalHeight < 50){
totalHeight = 50;
}
int thisHeight = metrics.heightPixels - totalHeight;
return thisHeight;
}
public static int getNavigationBarHeight(Context pContext){
Resources resources = pContext.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
public static int getStatusBarHeight(Context pContext) {
Resources resources = pContext.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
public static int getActionBarHeight(Context pContext) {
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (pContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,pContext.getResources().getDisplayMetrics());
}
return actionBarHeight;
}
I tested on 2.3.3, 4.0.3, 4.4.2. It seems like still get error when displaying. Does anyone have other like this or having some methods can solve this problem ?
Thanks all
private int getRealContentSize(){
int statusBarHeight = Common.getStatusBarHeight(this);
int actionBarHeight = Common.getActionBarHeight(this);
int navigationBarHeight = Common.getNavigationBarHeight(this);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int totalHeight = statusBarHeight + actionBarHeight + navigationBarHeight;
Log.i("BAR", "status + " + statusBarHeight +
"; action bar + " + actionBarHeight +
"; navigation " + navigationBarHeight);
if(totalHeight < 50){
totalHeight = 50;
}
int thisHeight = metrics.heightPixels - totalHeight;
return thisHeight;
}
public static int getNavigationBarHeight(Context pContext){
Resources resources = pContext.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
public static int getStatusBarHeight(Context pContext) {
Resources resources = pContext.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
public static int getActionBarHeight(Context pContext) {
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (pContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,pContext.getResources().getDisplayMetrics());
}
return actionBarHeight;
}
I tested on 2.3.3, 4.0.3, 4.4.2. It seems like still get error when displaying. Does anyone have other like this or having some methods can solve this problem ?
Thanks all
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.