Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide actionbar in some fragments with Android Navigation Components?

I am using android navigation components to navigate fragments. I can easily set action bar by using this code in the Main Activity :

    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

But If I want to hide the supportActionbar in some of the fragments then what should be the best approach?

like image 763
taha naqvi Avatar asked Apr 20 '20 13:04

taha naqvi


People also ask

Which of the ways can be used to hide the ActionBar in android?

If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme.

How do we hide the menu on toolbar in one fragment?

How do we hide the menu on toolbar in one fragment? When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment.

Which method is used to hide the activity title?

The requestWindowFeature(Window. FEATURE_NO_TITLE) method of Activity must be called to hide the title.

How add and remove fragments?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();


1 Answers

For the fragments that you want to hide the SupportActionBar, you can hide it in onResume() with .hide(), and show it again in onStop() with .show()

@Override
public void onResume() {
    super.onResume();
    ActionBar supportActionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
    if (supportActionBar != null)
        supportActionBar.hide();
}

@Override
public void onStop() {
    super.onStop();
    ActionBar supportActionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
    if (supportActionBar != null)
        supportActionBar.show();
}
like image 149
Zain Avatar answered Oct 24 '22 19:10

Zain