I can create a transparent notif+nav bar and then set below the notif bar a custom view that has a gradient background. This is ok.
But how can I create a nav bar that is black, and not transparent at the same time? (So: gradient notif bar+black filled nav bar)
Is it possible?
Here is my old code
fun setTransparentNotificationBar(window: Window) {
with(window) {
addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
}
}
Thanks
OK the answer is:
with(window) {
clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
statusBarColor = Color.TRANSPARENT
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
}
Related
I want to do full screen in the app
I did it with this code
`private fun hideSystemUI() {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
private fun showSystemUI() {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)`
But I have a camera on the screen and I see a
black bar.
How to stretch the screen so that this bar is not there
Kotlin programming language
Thank You Very Much
this problem happen with me for 2 different devices and this because of the front camera notch or screen cutout.
I used this code in oncreate() :
hideSystemUI();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().getAttributes().layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; }
Or in the theme style xml file add this line :
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
Go into the theme file and change your theme to "Theme.MaterialComponents.Light.NoActionBar
Use
requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title
getSupportActionBar().hide();
I have an app which is based on single activity and multiple fragments and some fragments needs to show into fullscreen when enters fragment and exit from fullscreen when exit. I am currently using flags to show fullscreen in Android Kitkat but its not optimal way i think. I also read ImmersiveMode but it's not working in lower Android version. Currently i am using these methods for fullscreen enter and exit.
//This method not showing transparent status bar also navigation bar and not showing system icons either
public static void setFullscreen(Activity activity) {
if (Build.VERSION.SDK_INT > 10) {
int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN;
boolean isImmersiveAvailable = android.os.Build.VERSION.SDK_INT >= 19;
if (isImmersiveAvailable) {
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
} else {
activity.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
public static void exitFullscreen(Activity activity) {
if (Build.VERSION.SDK_INT > 10) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
} else {
activity.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
public static void ShowHideFullscreen(boolean isFullscreen,Context cntx){
int newUiOptions = 0;
if (isFullscreen){
// BEGIN_INCLUDE (get_current_ui_flags)
// The UI options currently enabled are represented by a bitfield.
// getSystemUiVisibility() gives us that bitfield.
int uiOptions = ((Activity)cntx).getWindow().getDecorView().getSystemUiVisibility();
newUiOptions = uiOptions;
// END_INCLUDE (get_current_ui_flags)
// BEGIN_INCLUDE (toggle_ui_flags)
boolean isImmersiveModeEnabled =
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
if (isImmersiveModeEnabled) {
Log.d(TAG, "Turning immersive mode mode off. ");
} else {
Log.d(TAG, "Turning immersive mode mode on.");
}
// Navigation bar hiding: Backwards compatible to ICS.
if (Build.VERSION.SDK_INT >= 14) {
newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// Status bar hiding: Backwards compatible to Jellybean
if (Build.VERSION.SDK_INT >= 16) {
newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
// Immersive mode: Backward compatible to KitKat.
// Note that this flag doesn't do anything by itself, it only augments the behavior
// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample
// all three flags are being toggled together.
// Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
// Sticky immersive mode differs in that it makes the navigation and status bars
// semi-transparent, and the UI flag does not get cleared when the user interacts with
// the screen.
if (Build.VERSION.SDK_INT >= 18) {
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
((Activity)cntx).getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
}else {
((Activity)cntx).getWindow().clearFlags(newUiOptions);
}
}
These are method we use to enter or exit from fullscreen in fragment with hiding action bar. We use these to entered in fullscreen on onCreate and exit on onPause but sometimes it fails to show desired result.
We need to know what are the optimal way to show fullscreen fragment on lower API too(KITKAT) and also which are the best way to hide toolbar when showing fullscreen because our code ((AppCompatActivity)Objects.requireNonNull(getActivity())).getSupportActionBar().hide();
throws nullpointerexception sometimes.
Our issue:
Show fullscreen fragment with transparent status bar and drawing system icon over fullscreen fragment and transparent navigation bar.
Hiding and showing toolbar with method which safe to call without getting NullpointerExepection
We can target android Kitkat as lowest android version.
Fragment Fullscreen example with system icons and transparent navigation bar.
Update: I set IMMERSIVE mode on my fragment onResume and reset it back at onStop by these method.
private void hideSystemUI() {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
View decorView = getActivity().getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar().hide();
}
// Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
View decorView = getActivity().getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar().show();
}
It shows my fragment in fullscreen with status bar hidden (that's not what i want) and my fragments belong to Navigation Drawer. when i press back button from fullscreen fragment it shows my status bar over toolbar in previous fragment.
I solved this issue by creating a method which clears desired flags at onStop and add flags at onResume
#Override
public void onResume() {
super.onResume();
App_Functions.transparentStatusBar(getActivity(),true,false);
}
#Override
public void onStop() {
super.onStop();
App_Functions.transparentStatusBar(getActivity(),false,false);
}
if i want to show transparent statusbar but navigation bar should be same then i use true in first parameters of method and for clearing that flag just use false. Same way i am using true for complete fullscreen and false for clearing fullscreen flags
public static void transparentStatusBar(Activity activity, boolean isTransparent, boolean fullscreen) {
if (isTransparent){
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
((AppCompatActivity) Objects.requireNonNull(activity)).getSupportActionBar().hide();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
defaultStatusBarColor = activity.getWindow().getStatusBarColor();
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// FOR TRANSPARENT NAVIGATION BAR
//activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
Log.d(TAG,"Setting Color Transparent "+Color.TRANSPARENT+ " Default Color "+defaultStatusBarColor);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Log.d(TAG,"Setting Color Trans "+Color.TRANSPARENT);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}else {
if (fullscreen){
View decorView = activity.getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}else {
((AppCompatActivity) Objects.requireNonNull(activity)).getSupportActionBar().show();
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.getWindow().setStatusBarColor(defaultStatusBarColor);
}else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}
}
}
Please feel free to recommend better way to use this method across all platform.
How to handle the toolbar from fragment?
Where to put the toolbar is highly opinionated. But I recommend you to put the toolbar in each fragment xml file rather than keeping one toolbar for the whole application in the Activity xml file. See this sample app by Google, they are also placing the AppbarLayout and toolbar per Fragment rather than keeping it in the activity layout file.
So if you place the toolbar with in the fragment, you can easily hide/show it from the fragment without the fear of getting null pointer exceptions.
So your activity xml file will simply contain the NavHost Fragment only. So the whole screen will be managed by the currently visible Fragment :)
How to get the transparent statusbar for older API levels?
Devices below kitkat would not support transparent statusbar feature. Kitkat supports transluent statusbar, not transparent. So you can use transparent statusbar for API level lollipop or above. If the device version is below lollipop, ignore it as the platform does not support the feature. So rest of my answer assumes you want to achieve transparent status bar when the device version is lollipop or above.
Well then, How to get transparent status-bar for lollipop and above for selected fragments?
We need to create a custom NavHostFragment. This is required because, during fragment transitions, multiple fragment's view hierarchies can be added at the same time. If one consumes window insets, the other might not be laid out properly. To workaround that, we need to make sure we dispatch the insets to all children, regardless of how they are consumed.
class DispatchInsetsNavHostFragment : NavHostFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
view.setOnApplyWindowInsetsListener { v, insets ->
(v as? ViewGroup)?.forEach { child ->
child.dispatchApplyWindowInsets(insets)
}
insets
}
}
}
}
Wrap your nav host fragment with a Framelayout as shown below and put android:fitsSystemWindows="true"
<FrameLayout
android:id="#+id/navHostContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<fragment
android:id="#+id/navHostFragment"
android:name="com.yourdomain.app.DispatchInsetsNavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/main" />
</FrameLayout>
Set the following flags to the navHostContainer which we defined above, from your MainActivity class onCreate method after setContentView.
val navHostContainer: FrameLayout = findViewById(R.id.navHostContainer)
navHostContainer.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Make the content ViewGroup ignore insets so that it does not use the default padding
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
navHostContainer.setOnApplyWindowInsetsListener(NoopWindowInsetsListener)
}
Here is the NoopWindowInsetsListener
object NoopWindowInsetsListener : View.OnApplyWindowInsetsListener {
override fun onApplyWindowInsets(v: View, insets: WindowInsets): WindowInsets {
return insets
}
}
Thats all, from now on, your fragments has the complete ownership to the statusbar area. Your views will be drawn behind the transparent status bar. For some fragments, you may not want the statusbar to overlap your content, for those fragments, simply put android:fitsSystemWindows="true" to your root layout in the Fragment xml file.
How to set custom statusbar color for some Fragments?
Use the app:statusBarBackground attribute of CoordinatorLayout with android:fitsSystemWindows="true"
That's all. Happy coding :)
1- in style xml file change AppTheme parent to NoActionBar:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
2- use this method in your activity for transparent statusBar
private void hideStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
}
}
3- match_parent frameLayout tag in your activity_main(xml) for full screen the fragment
Is it possible to hide the navbar without hiding the status bar?
I've tried this in my styles's xml
<item name="android:windowFullscreen">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
and I hide de navbar like this in the activity
val decorView = window.decorView
val uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
decorView.systemUiVisibility = uiOptions
Thank you!!
Edit:
fun hideNavigationStatusBar(activity: Activity) {
activity.window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
val decorView = activity.window.decorView
val uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
decorView.systemUiVisibility = uiOptions
}
fun translucidNavigationBar(activity: Activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
val w = activity.window
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}
}
I can hide status and navigation bar with the first method and keep the status with a translucent navigation with the second, but still I can't reach to keep the status and completely hide the navigation bar.
The easiest way to solve this is in two parts, removing the navbar and then making the status bar transparent.
1 - Removing the navbar
To remove the navbar, create a new style on the styles.xml file under the values folder that inherits from Theme.AppCompat.Light.NoActionBar.
Then on the manifest.xml file change the theme of the Activity to your custom style.
With this the navbar will be removed, but the status bar will still be grayed out.
2 - Making the status bar transparent
You can follow this tutorial to make the status bar transparent: Transparent Status Bar
Or just add the following to the onCreate method of your Activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
With this you can expect an App style similar to the one in the next picture:
3 - Removing the status bar completely
To hide the status bar completely, add this on your onCreate method:
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
You can read more on this at Hiding the status bar
Result:
4 - Removing the navigation Bar
You can find a solution to this at the Android Developer site Hiding the navigation bar
Basically just add this to your onCreate:
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
You can use this method:
private void setTransparentStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
} else {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
I use this way to set light status bar for Android M, and it works well:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
But, it does not work at Android N. The words in status bar are still white. Is it a bug? Or is there any way to set light status bar at Android N?
Well, I found the question.It did works to set light status bar mode.But after that, i changed the status bar's color, called setSystemUiVisibility() again and set other value.So the light status bar is not valid.
Just call this method from any activity and pass color code too
public static void makeStatusBarLight(Activity activity, int color) {
Window window = activity.getWindow();
window.addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(color);
}
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
call like this
makeStatusBarLight(BaseActivity.this, getResources().getColor(R.color.yourcolor));
Just it, I have found how to hide title bar but I didnt find how hide / show the status bar pressing a button fr example. It is possible? thanks!
For poeple whose show part is not working , You can try following code
Show Status Bar
if (Build.VERSION.SDK_INT < 16) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
View decorView = getWindow().getDecorView();
// show the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(uiOptions);
}
To hide a status Bar
Use this code in your Activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Change Application theme in Manifest file as below
android:theme="#android:style/Theme.Black.NoTitleBar"
Reference - https://developer.android.com/training/system-ui/immersive.html
// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
Although action bar is overlayed with the status bar.