Reputation: 6187
I want to get actionbar height in my oncreate method, not after views created like this
first way
content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
Log.d(tag + " onCreate", "getSupportActionBar().getHeight()=" + getSupportActionBar().getHeight());
return true;
}
});
for that, I use this code
second way
final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
int mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
but my problem is when I use first way I get actionbar height 56, and when i use secound way i get actionbar height 48.
and i think first way is correct answer but i dont want to wait until views created.
can anyone help me about this?
"i use htc desire 200 for test with density mdpi"
Upvotes: 4
Views: 3025
Reputation: 16214
You can have two different height between these two methods because they are returning the value of two different things. The first method returns the actual ActionBar
height. The second method returns the value of the android attribute actionBarSize
.
The correct method to have the real ActionBar
height is the first but if you say that you don't want to wait the creation of the views, if you are using Toolbar
you can set via xml his height to ?android:attr/actionBarSize
and use the second method wherever you want to get the height in pixels.
Upvotes: 1