How to Hide Action Bar for Fragment

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

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();
}

How to hide the default fragment actionBar create our own actionBar in android & kotlin

I have solved the problem.

val appBarConfiguration = AppBarConfiguration(setOf( R.id.navigation_home, R.id.navigation_messages, R.id.navigation_profile)) 
setupActionBarWithNavController(navController, appBarConfiguration)

Just remove the above two lines from the main activity and your problem is solved.

Removing Action Bar completely from Fragment

If you want to completely remove the ActionBar (AppBar) completely from your App, then follow these steps:

  • Go to your AndroidManifest.xml
  • Edit this line: android:theme="@style/{theme}"> to android:theme="@style/Theme.AppCompat.Light.NoActionBar"> or
    anything that ends with NoActionBar.

And if you want to remove the ActionBar just from your Fragment, then add next code in the onCreateView() of the Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);

// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.yourLayout, container, false);
}

Or if you want to remove the ActionBar from an Activity, add this one line of code to the onCreate() method of the Activity.

setTheme(R.style.{your style}

Remember

  • The {style} should end with NoActionBar
  • And most importantly, add the code before these two lines of code (add it to the top of the method):

    super.onCreate(savedInstanceState);
    setContentView(R.layout.{your layout});

Hope this answer helped!



Related Topics



Leave a reply



Submit