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}">
toandroid:theme="@style/Theme.AppCompat.Light.NoActionBar">
or
anything that ends withNoActionBar
.
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
Navigating to Previous Fragment Without Reloading It
How to Set Android_Ndk_Home So That Android Studio Does Not Ask for Ndk Location
How to Completely Disable Selinux in Android L in the Init.Rc File
Socket.Io Client Getting Connected Disconnected Repeatedly
Could Not Identify Launch Activity: Default Activity Not Found
How to Tell If a View Is Visible on Screen in Android
How to Post Raw Whole Json in the Body of a Retrofit Request
How to Check If Activity Is in Foreground or in Visible Background
How to Save an Activity State Using the Save Instance State
Control the Playback Speed of Video in Android
Convert File: Uri to File in Android
How to Emulate Gps Location in the Android Emulator
How to Start Android Application on Device Boot
How to Change the Default Height of a Bottomsheetdialog