Skip to main content

Hiding App bar in a particular fragment with Navigation UI

If you are using navigation UI in your application , if you want to hide your app bar (top bar of your app showing navigation icon)  or back navigation arrow in a particular fragment you can do like this in your Mainactivity
class MainActivity : AppCompatActivity() {
  class MainActivity : AppCompatActivity() {
  private lateinit var drawerlayout : DrawerLayout
    private lateinit var navView : NavigationView
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        drawerlayout = findViewById(R.id.drawer_layout)
        navView = findViewById(R.id.nav_view)
        val navController : NavController = this.findNavController(R.id.navhost)
        NavigationUI.setupActionBarWithNavController(this,navController,drawerlayout)
        NavigationUI.setupWithNavController(navView,navController)
        navController.addOnDestinationChangedListener { _, destination, _ ->  
            if(destination.id==R.id.qaFragment){
                supportActionBar?.hide()
            }
            else{
                supportActionBar?.show()
            }
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController : NavController = findNavController(R.id.navhost)
        return NavigationUI.navigateUp(navController,drawerlayout)
    }
}
In the main activity you have to use OnDestinationChangeListener, in the listener you have to check for destination  fragment for which you want to hide the actionBar and hide it.
navController.addOnDestinationChangedListener { _, destination, _ ->  
            if(destination.id==R.id.qaFragment){
                supportActionBar?.hide()
            }
            else{
                supportActionBar?.show()
            }
        }

Comments

Popular posts

Android Sqlite and ListView Example

This is simple application which insert data into Sqlite database --and shows the data  from the database in a ListView ListView is not used in android Anymore . We use RecyclerView and CardView   in Android RecyclerView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/04/android-recyclerview-example.html RecyclerView with CardView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/09/android-card-view-example.html This  is the first Activity of application which shows data from database in listview. Register here buton will start a Registration Activity. Submit button will add data to database and show it in the ListView of MainActivity. Update can be performed by clicking ListView items.     you can download the source code of this project from  google drive   https://drive.google.com/folderview?id=0BySLpWhqmbbdS0dtT1R2TXdBWEE&usp=sharing click on the abov...

Android List View using Custom Adapter and SQLite

following is a simple applicaton to create ListView using  Custom adapter.screenshot of the application  is like this . ListView is not used in android Anymore . We use  RecyclerView  and  CardView   in Android RecyclerView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/04/android-recyclerview-example.html RecyclerView with Cardview Example is available on the following link http://androidtuts4u.blogspot.in/2017/09/android-card-view-example.html The ListView below the submit button is populated using Custom Adapter.Data is stored and retrieved using SQLite databsase. you can download the source code of this project from  google drive   https://drive.google.com/folderview?id=0BySLpWhqmbbdUXE5aTNhazludjQ&usp=sharing click on the above link ->sign into  your  google account ->add this to your google drive -> open it in google drive and download it. To create a simple ...

Swipe or OnFling Event Android

This  is a simple application Demonstrating Swipe or onFling() event on ListView. you can download the source code of this project from  google drive   https://drive.google.com/folderview?id=0BySLpWhqmbbdSVB4M0hXb0VxcU0&usp=sharing click on the above link ->sign into  your  google account ->add this to your google drive -> open it in google drive and download it. 1. Following is the MainActivity (DemoSwipe.java) of the application package com.arun.demolistviewswipe; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class DemoSwipe extends Activity { ListView lvCountry; Strin...