Actual result: The status bar appears over the action bar Toolbar
and the MenuItem
inside the action bar is cut off. Note: "Test" is the action bar's title
.
Expected result:
The top bound of the action bar Toolbar
should appear directly below the bottom bound of the status bar and any MenuItem
s inside the action bar should be completely visible.
Activity's XML layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/background"
tools:ignore="ContentDescription"/>
<android.support.v7.widget.Toolbar
android:id="@+id/action_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
android:fitsSystemWindows="true"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</FrameLayout>
The action bar title
and MenuItem
are added at runtime.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
setSupportActionBar((Toolbar) findViewById(R.id.action_bar));
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setTitle("Test");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_test, menu);
return true;
}
Before I added fitsSystemWindows="true"
to the Toolbar
view, the status bar still overlayed the action bar, but the 'SKIP' MenuItem
was vertically centered in the action bar, causing it to appear partially underneath the status bar. I expected the fitsSystemWindows=true
flag to give me my expected result (mentioned above), but it did not. It's as if fitsSystemWindows="true"
correctly positioned the 'SKIP' button but did not adjust the position of the action bar itself. Anyone know what might be the issue here?
EDIT: I realize that I could remove fitsSystemWindows="true"
and add a marginTop="...statusBarHeight"
to the Toolbar
view, but I am looking for the a cleaner way to solve this.
My issue was due to me setting the Toolbar
's layout_height
to ?attr/actionBarSize
. I originally thought that fitsSystemWindows
repositions the Toolbar
, but it appears to add a padding instead. So when top padding is added to the Toolbar
, the Toolbar
's contents are pushed down. Since the Toolbar
's height is fixed, the contents are pushed below the lower bound of the container. I ended up setting ?attr/actionBarSize
as the value for the Toolbar
's minHeight
attribute to solve this. Below is my updated Toolbar
:
<android.support.v7.widget.Toolbar
android:id="@+id/action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@android:color/transparent"
android:fitsSystemWindows="true"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
Caveat: This works if you aren't wanting to show anything directly below the action bar since the action bar's height, for one reason or another, is about twice the height that it needs to be to contain a single line of the home icon, title, and/or menu items. If anyone knows a way to achieve a non-overlapping status bar AND a normal size action bar, please share your insight. I would be forever grateful.
Caveat update: Okay. So apparently my action bar was receiving extra, bottom padding equivalent to the height of the navigation bar because I set <item name="android:windowTranslucentNavigation">true</item>
on my Activity
's theme. I verified this by removing the windowTranslucentNavigation
attribute. I am testing this on a 7.1 Pixel using Android Support Libraries v25.1.1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With