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 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 application like this 1.  Create a class which extends  

Unknown filesystem grub rescue> Error ubuntu

when computer shows    Unknown filesystem grub rescue>   on starting ubuntu  do these things to recover ---. Boot  your computer on a ubuntu live-CD or live-USB ---choose option "Try ubuntu without installation" ---Install Boot Repair       -  open terminal then type             sudo add-apt-repository ppa:yannubuntu/boot-repair && sudo apt-get update -press enter then type     sudo apt-get install -y boot-repair && boot-repair   ---- if you can't install Boot Repair from repository, dowload it from official   website of Boot Repair --- After Installing open it and click the "Recommended repair" button .         this will fix your system  --- Reboot your system it will work fine if your system is not fixed after this do the following(you have to install grub)   --- If your linux partition is in sda10 sudo mount /dev/sda10 /mnt sudo mount --bind /dev /mnt/d

DataBinding - ViewBinding in Android

ViewBinding is a feature that allow you to write code more easily.  First we will s ee an App without ViewBinding then we will enable ViewBinding in the App .Screenshot of our app is , it is asimple application when we  click the Button score Will Increase You can also see this tutorial in my youtube channel you can download source code of this project from GitHub https://github.com/arunkfedex/DemoNavGraphTest Layout file is activity_main.xml <?xml version="1.0" encoding="utf-8"?> <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"> <TextView android:id="@+id/text1