Custom SupportActionBar looks different after redisplayed

Viewed 134

I'm trying to build a Single Activity App and using a custom SupportActionBar in my Activity. The other Views are all Fragments.

My custom SupportActionBar looks like this:

Correct SupportActionBar

Now I have the case that I have to display a video in fullscreen mode in a Fragment. Therefore I'm just calling the hide function of the SupportActionBar:

override fun onResume() {
super.onResume()
(activity as  MainActivity).supportActionBar!!.hide()}

That works fine, but when I want to display the bar again after the video is finished the bar looks not the same.

Wrong SupportActionBar

In my Fragment I'm calling the show function from SupportActionBar and the initToolbar function of the MainActivity:

override fun onStop() {
    super.onStop()
    (activity as MainActivity).supportActionBar!!.show()
    (activity as MainActivity).initToolbar()
}

And here is the initToolbar method from my Activity:

fun initToolbar() {
    setSupportActionBar(toolbar_all_custombar)
    supportActionBar!!.setDisplayShowTitleEnabled(false)
    supportActionBar!!.setIcon(R.drawable.ic_launcher)
}

The toolbar layout:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_all_custombar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:titleTextAppearance="@style/AppTheme.TitleTextStyle"
app:titleTextColor="@color/textColorPrimary">

<TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/all_toolbar_title"
    android:textColor="@android:color/white"
    android:textStyle="bold|italic"/>

</android.support.v7.widget.Toolbar>

I don't get it why my bar looks different after showing it again. What is the cause of this behaviour?

5 Answers

Try it once by putting some paddings or margins (bottom) on the TextView.

Use setSystemUiVisibility

Instead of supportActionBar!!.hide()

hide(): If the window hosting the ActionBar does not have the feature FEATURE_ACTION_BAR_OVERLAY it will resize application content to fit the new space available.

setSystemUiVisibility(): Hiding the ActionBar through this system UI flag allows you to more seamlessly hide it in conjunction with other screen decorations.

After making the SupportActionBar visible again, you could try calling (activity as MainActivity).supportActionBar.invalidate(); to redraw all views within it. This should cause your supportActionBar to be correctly redrawn.

You can set supportToolBar as null when you want to hide it and then restore the previous value when you want to show it.

This is tested and works in my case.

Thank you for your help! I finally solved it. The problem was that I used some View flags to achieve the fullscreen mode in the respective Fragment. Therefore I removed following function in this Fragment:

private fun hideSystemUiFullScreen() {
    exoplayer_videoview_container!!.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
            or View.SYSTEM_UI_FLAG_FULLSCREEN
            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
}

I don't changed my onResume and onStop method. So the issue was only the use of the View flags to get fullscreen mode.

Related