Home > other >  Hide the system's own bottom navigation - elements should be arranged at the bottom
Hide the system's own bottom navigation - elements should be arranged at the bottom

Time:12-19

everyone. I have an app in which I hide the navigation bar and show a bottom navigation. If I hide the former, the navigation should be at the bottom edge. how can I do this?

My layout file for the navigation:

 <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@ id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/bottom_nav"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Code for hiding:

 currentApiVersion = Build.VERSION.SDK_INT
    val flags: Int = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
            View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

    if (currentApiVersion >= Build.VERSION_CODES.KITKAT) {
        window.decorView.systemUiVisibility = flags
        val decorView: View = window.decorView
        decorView.setOnSystemUiVisibilityChangeListener { visibility ->
            if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN === 0) {
                decorView.systemUiVisibility = flags
            }
        }
    }

enter image description here

CodePudding user response:

The code is from one of my older projects but i think it will still work. In your activitiy use below code:

class MyActivity : AppCompatActivity() {
    private lateinit var decorView: View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.decorView.systemUiVisibility = hideSystemBars()
        setContentView(R.layout.activity_my)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        decorView = window.decorView
        if (hasFocus) {
            decorView.systemUiVisibility = hideSystemBars()
        }
    }

    fun hideSystemBars(): Int {
        return View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    }
}

CodePudding user response:

try calling his function, in onCreate()

fun hideSystemUI() {
    WindowCompat.setDecorFitsSystemWindows(window, false)
    WindowInsetsControllerCompat(window, findViewById(android.R.id.content)).let { controller ->
        controller.hide(WindowInsetsCompat.Type.systemBars())
        controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }
}
  • Related