How to get the ActionBar height?
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)
}
How to get actionBar size properly programatically?
You can get Action bar height by
public void changeRecyclerViewPosition() {
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (getActivity().getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
params.setMargins(0,actionBarHeight, 0, 0);
myrv.setLayoutParams(params);
}
Get the Height of ActionBar
I found the alternative of ActionBar.getHeight()
If anyone facing the same issue then this link is useful.
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. ~ Anthony
// Calculate ActionBar's height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
get actionbar height in android
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.
What is the size of ActionBar in pixels?
To retrieve the height of the ActionBar in XML, just use
?android:attr/actionBarSize
or if you're an ActionBarSherlock or AppCompat user, use this
?attr/actionBarSize
If you need this value at runtime, use this
final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
If you need to understand where this is defined:
- The attribute name itself is defined in the platform's /res/values/attrs.xml
- The platform's themes.xml picks this attribute and assigns a value to it.
- The value assigned in step 2 depends on different device sizes, which are defined in various dimens.xml files in the platform, ie. core/res/res/values-sw600dp/dimens.xml
What's the height of the Android Toolbar?
The Toolbar
(if not extended) has the height of:
56dp
(default)48dp
(landscape)64dp
(sw600dp; i.e. tablet)
The Toolbar
is higher than the pre-lollipop ActionBar
; it was 48dp
by default, 40dp
in landscape and 56dp
in sw600dp.
And to retrieve the height of the Toolbar in XML, just use
?android:attr/actionBarSize
or if you're using the AppCompat library this
?attr/actionBarSize
Get navigation/action bar height
App.cs (Common code)
public static int ActionBarHeight;
MainActivity, in the OnCreate method (Droid part):
Android.Content.Res.TypedArray styledAttributes = Theme.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ActionBarSize });
App.ActionBarHeight = (int)(styledAttributes.GetDimension(0, 0) / Resources.DisplayMetrics.Density);
styledAttributes.Recycle();
If you want the height in pixels:
App.ActionBarHeight = (int)styledAttributes.GetDimension(0, 0);
Related Topics
Runtimeexception: Your Content Must Have a Listview Whose Id Attribute Is 'Android.R.Id.List'
Android Studio Keeps Refusing to Resolve Com.Android.Support:Appcompat-V7:29.0.1
How to Save/Store Objects in Sharedpreferences on Android
How to Set Ringtone in Android from My Activity
How to Define a Min and Max Value For Edittext in Android
How to Use Asynctask Correctly in Android
Get Selected Item Using Checkbox in Listview
How to Make the Corners of a Button Round
Android: Move a View on Touch Move (Action_Move)
Adding Custom Radio Buttons in Android
Error:(1, 0) Plugin With Id 'Com.Android.Application' Not Found
How to Add an Array or Object to Sharedpreferences on Android
Android and Facebook Share Intent
Android: Combining Text & Image on a Button or Imagebutton
Programmatically Go Back to the Previous Fragment in the Backstack