How to get the ActionBar height?
AndroidAndroid ActionbarAndroid Problem Overview
I am trying to get the height of the ActionBar
(using Sherlock) every time an activity is created (specially to handle configuration changes on rotation where the ActionBar height might change).
For this I use the method ActionBar.getHeight()
which works only when the ActionBar
is shown.
When the first activity is created for the first time, I can call getHeight()
in the onCreateOptionsMenu
callback. But this method is not called after.
So my question is when can I call getHeight() and be assured that it doesn't return 0? Or if it is not possible, how can I set the height of the ActionBar ?
Android Solutions
Solution 1 - Android
While @birdy's answer is an option if you want to explicitly control the ActionBar size, there is a way to pull it up without locking the size that I found in support documentation. It's a little awkward but it's worked for me. You'll need a context, this example would be valid in an Activity.
// Calculate ActionBar height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Kotlin:
val tv = TypedValue()
if (requireActivity().theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
val actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
}
Solution 2 - Android
In XML, you should use this attribute:
android:paddingTop="?android:attr/actionBarSize"
Solution 3 - Android
Ok I was googling around and get to this post several times so I think it'll be good to be described not only for Sherlock but for appcompat also:
Action Bar height using appCompat
Pretty similiar to @Anthony description you could find it with following code(I've just changed resource id):
TypedValue tv = new TypedValue();
if (getActivity().getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Pre Sandwitch Problem
I needed action bar height because on pre ICE_CREAM_SANDWITCH devices my view was overlaping action bar. I tried setting android:layout_marginTop="?android:attr/actionBarSize", android:layout_marginTop="?attr/actionBarSize", setting overlap off to view/actionbar and even fixed actionbar height with custom theme but nothing worked. That's how it looked:
Unfortunately I found out that with method described above I get only half of the height(I assume that options bar is not taken in place) so to completely fix the isue I need to double action bar height and everything seems ok:
If someone knows better solution(than doubling actionBarHeight) I'll be glad to let me know as I come from iOS development and I found most of Android view's stuff pretty confusing :)
Regards,
hris.to
Solution 4 - Android
@Anthony answer works for devices that support ActionBar
and for those devices which support only Sherlock Action Bar
following method must be used
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
if(actionBarHeight ==0 && getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true)){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
//OR as stated by @Marina.Eariel
TypedValue tv = new TypedValue();
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}else if(getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Solution 5 - Android
i think the safest way would be :
private int getActionBarHeight() {
int actionBarHeight = getSupportActionBar().getHeight();
if (actionBarHeight != 0)
return actionBarHeight;
final TypedValue tv = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
} else if (getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
return actionBarHeight;
}
Solution 6 - Android
To set the height of ActionBar
you can create new Theme
like this one:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.FixedSize" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarSize">48dip</item>
<item name="android:actionBarSize">48dip</item>
</style>
</resources>
and set this Theme
to your Activity
:
android:theme="@style/Theme.FixedSize"
Solution 7 - Android
If you are using a Toolbar as the ActionBar,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
then just use the layout_height of the toolbar.
int actionBarHeight = toolbar.getLayoutParams().height;
Solution 8 - Android
Java:
int actionBarHeight;
int[] abSzAttr;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
abSzAttr = new int[] { android.R.attr.actionBarSize };
} else {
abSzAttr = new int[] { R.attr.actionBarSize };
}
TypedArray a = obtainStyledAttributes(abSzAttr);
actionBarHeight = a.getDimensionPixelSize(0, -1);
xml:
?attr/actionBarSize
Solution 9 - Android
If you're using Xamarin/C#, this is the code you're looking for:
var styledAttributes = Context.Theme.ObtainStyledAttributes(new[] { Android.Resource.Attribute.ActionBarSize });
var actionBarSize = (int)styledAttributes.GetDimension(0, 0);
styledAttributes.Recycle();
Solution 10 - Android
you can use this function to get actionbar's height.
public int getActionBarHeight() {
final TypedArray ta = getContext().getTheme().obtainStyledAttributes(
new int[] {android.R.attr.actionBarSize});
int actionBarHeight = (int) ta.getDimension(0, 0);
return actionBarHeight;
}
Solution 11 - Android
A ready method to fulfill the most popular answer:
public static int getActionBarHeight(
Activity activity) {
int actionBarHeight = 0;
TypedValue typedValue = new TypedValue();
try {
if (activity
.getTheme()
.resolveAttribute(
android.R.attr.actionBarSize,
typedValue,
true)) {
actionBarHeight =
TypedValue.complexToDimensionPixelSize(
typedValue.data,
activity
.getResources()
.getDisplayMetrics());
}
} catch (Exception ignore) {
}
return actionBarHeight;
}
Solution 12 - Android
Here's an updated version with Kotlin I used assuming phone is higher than Honeycomb:
val actionBarHeight = with(TypedValue().also {context.theme.resolveAttribute(android.R.attr.actionBarSize, it, true)}) {
TypedValue.complexToDimensionPixelSize(this.data, resources.displayMetrics)
}
Solution 13 - Android
This helper method should come in handy for someone. Example: int actionBarSize = getThemeAttributeDimensionSize(this, android.R.attr.actionBarSize);
public static int getThemeAttributeDimensionSize(Context context, int attr)
{
TypedArray a = null;
try{
a = context.getTheme().obtainStyledAttributes(new int[] { attr });
return a.getDimensionPixelSize(0, 0);
}finally{
if(a != null){
a.recycle();
}
}
}
Solution 14 - Android
you just have to add this.
public int getActionBarHeight() {
int height;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
height = getActivity().getActionBar().getHeight();
} else {
height = ((ActionBarActivity) getActivity()).getSupportActionBar().getHeight();
}
return height;
}
Solution 15 - Android
I found a more generic way to discover it:
int[] location = new int[2];
mainView.getLocationOnScreen(location);
int toolbarHeight = location[1];
where 'mainView' is the root view of your layout.
The idea is basically get the Y position of the mainView as it is set right below the ActionBar (or Toolbar).
Solution 16 - Android
The action bar height differs according to the applied theme. If you're using AppCompatActivity the height will be different in most of cases from the normal Activity.
If you're using AppCompatActivity you should use R.attr.actionBarSize instead of android.R.attr.actionBarSize
public static int getActionBarHeight(Activity activity) {
TypedValue typedValue = new TypedValue();
int attributeResourceId = android.R.attr.actionBarSize;
if (activity instanceof AppCompatActivity) {
attributeResourceId = R.attr.actionBarSize;
}
if (activity.getTheme().resolveAttribute(attributeResourceId, typedValue, true)) {
return TypedValue.complexToDimensionPixelSize(typedValue.data, activity.getResources().getDisplayMetrics());
}
return (int) Math.floor(activity.getResources()
.getDimension(R.dimen.my_default_value));
}
Solution 17 - Android
In xml, you can use ?attr/actionBarSize, but if you need access to that value in Java you need to use below code:
public int getActionBarHeight() {
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv,
true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(
tv.data, getResources().getDisplayMetrics());
} else {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
getResources().getDisplayMetrics());
}
return actionBarHeight;
}
Solution 18 - Android
The action bar now enhanced to app bar.So you have to add conditional check for getting height of action bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
height = getActivity().getActionBar().getHeight();
} else {
height = ((ActionBarActivity) getActivity()).getSupportActionBar().getHeight();
}
Solution 19 - Android
A simple way to find actionbar height is from Activity onPrepareOptionMenu
method.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
....
int actionBarHeight = getActionBar().getHeight());
.....
}
Solution 20 - Android
In javafx (using Gluon), the height is set at runtime or something like it. While being able to get the width just like normal...
double width = appBar.getWidth();
You have to create a listener:
GluonApplication application = this;
application.getAppBar().heightProperty().addListener(new ChangeListener(){
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
// Take most recent value given here
application.getAppBar.heightProperty.get()
}
});
...And take the most recent value it changed to. To get the height.
Solution 21 - Android
After trying everything that's out there without success, I found out, by accident, a functional and very easy way to get the action bar's default height.
Only tested in API 25 and 24
C#
Resources.GetDimensionPixelSize(Resource.Dimension.abc_action_bar_default_height_material);
Java
getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height_material);