I have an activity and a few fragments in it, and I have an action bar that I need to be visible all the time except for the case when I open MyTestFragment
, so when I open this fragment I need my action bar to be hidden.
As my Activity is AppCompatActivity
, I tried to call this method (from Activity) in order to hide an actionBar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
supportActionBar?.hide()
...
and it works, however, I need to make this call from fragment, so I do it like this
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(activity as AppCompatActivity).supportActionBar!!.hide()
}
and the result is
The action bar became kind of invisible instead of gone (you see this gray pass right under the clock)
What am I missing here?
UPD
I would like to add explanation to the problem, in order to illustrate this I did such code:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
Thread {
Thread.sleep(8000)
mainExecutor.execute {
supportActionBar?.show() <---- 2
}
}.start()
Thread {
Thread.sleep(12000)
mainExecutor.execute {
supportActionBar?.hide() <---- 3
}
}.start()
supportActionBar?.hide() <---- 1
...
What is happening here is - the method marked 1
is invoking first and the actionbar hides as expected. The next method #2 invokes show()
for the action bar and the action bar appears as expected, however, when it comes to method #3 and tries to hide the actionbar again the bug is here. Instead of hiding the actionbar it kind of makes it invisible and you can see a grey line (like on the screenshot).
So, looks like it is possible to hide the actionbar only immediately in OnCreate()
method if you try to do it after you will see a grey line instead of the actionbar.
UPD
My MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/awl_application_drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_activity_layout_bg_color"
tools:openDrawer="end">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<application.widget.toolbar.BondToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/errorViewContainer"
android:layout_below="@id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar_layout"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
style="@style/AppTabLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_nav_tab_height"
android:background="@color/deprecated_palette_FF000000"
android:elevation="6dp"
android:fillViewport="true"
android:padding="0dp"
app:tabPaddingBottom="0dp"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
app:tabPaddingTop="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:tabMinWidth="@dimen/bottom_nav_tab_min_width" />
<CustomView
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@id/tab_layout"
app:layout_constraintTop_toTopOf="parent"
app:theme="@style/ThemeOverlay.AppCompat.Light" />
</androidx.constraintlayout.widget.ConstraintLayout>
<FrameLayout
android:id="@+id/navigation_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_marginTop="?android:attr/actionBarSize" />
</androidx.drawerlayout.widget.DrawerLayout>