I know I can use colorPrimary to determine the color of Toolbar, and colorPrimaryDark to determine the color of Status bar.
I'm using the following theme
<!-- Base application theme. -->
<style name="Theme.Noteplus.Base.Brown" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimaryLight</item>
<item name="colorPrimaryDark">#ff0000</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
One of the interesting attribute is, when I slide out the navigation menu, the status bar becomes transparent automatically.
During run-time, sometime I would like to change the color of status bar.
setTitle("Recycler bin");
toolbar.setBackgroundColor(Color.BLUE);
getWindow().setStatusBarColor(Color.parseColor("#5694FF"));
It will looks as follow
Unfortunately, calling setStatusBarColor, will also loss the transparency attribute of status bar, when we slide out the navigation menu.
May I know, how to change status bar color during run-time, without lossing its transparency attribute? For my case, after I changing the status bar to blue during run-time, when I slide out navigation drawer, I wish to see status bar transparency attribute being retained.
Update
I had tried
private void setStatusBarColor(int color) {
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(color);
}
}
It doesn't help to provide transparency attribute when the navigation drawer slides out.
You are using a DrawerLayout. That means, that instead of using Window#setStatusBarColor(int) you should be using one of DrawerLayout#setStatusBarBackground() overloads.
The equivalent of your code is following:
ColorDrawable colorDrawable = new ColorDrawable(0xFF5694FF);
drawerLayout.setStatusBarBackground(colorDrawable);
I've applied minor changes to the template app that can be created with Android Studio wizard:
I guess your problem with transparency is not about the method you are using but its rather about the color itself!
You are using this getWindow().setStatusBarColor(Color.parseColor("#5694FF"));
Try to use RGBA instead of RGB so make it transparent, should be something like this:
getWindow().setStatusBarColor(Color.parseColor("#5694FF80"));
private void changeStatusBarColor(String color){
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor(color));
}
}
If you check here in stack overflow everyone before that line of code set the transparency of the status bar to solid with
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
Is there any way to change the color of the navigation bar BUTTONS not the navigation bar itself from white to grey:
to
you can do this in API > 21 as follow :
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.some_color));
}
see more information here
or just add following code in values-v21/style.xml
<item name="android:navigationBarColor">#color/theme_color</item>
I am trying to reproduce the behaviour of Google Calendar application:
but I have not found a way to change the status text color. If i set the colorPrimaryDark as white I cannot see the icons neither text of status bar due their color is white as well.
Is there any way to change the status bar text color?
I'm not sure what API level your trying to target, but if you can use API 23 specific stuff, you can add the following to your AppTheme styles.xml:
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
<item name="android:windowLightStatusBar">true</item>
when android:windowLightStatusBar is set to true, status bar text color will be able to be seen when the status bar color is white, and vice-versa
when android:windowLightStatusBar is set to false, status bar text color will be designed to be seen when the status bar color is dark.
Example:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Status bar stuff. -->
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
<item name="android:windowLightStatusBar">true</item>
</style>
you can do that programmatically like this answer
just add this
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
The compat version works on API 23+.
Here it is:
// deprecated
// WindowInsetsControllerCompat(window, view).isAppearanceLightStatusBars = Boolean
// also deprecated
// ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = Boolean
WindowCompat.getInsetsController(window, decorView)?.isAppearanceLightStatusBars = Boolean
You can get window directly from an Activity.
I like to add it to Window extension methods:
fun Window.setLightStatusBars(b: Boolean) {
WindowCompat.getInsetsController(this, decorView)?.isAppearanceLightStatusBars = b
}
You need androidx.core for this
it's very simple:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);// set status text dark
getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this,R.color.white));// set status background white
and vice versa:
getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this, R.color.black));
View decorView = getWindow().getDecorView(); //set status background black
decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); //set status text light
Following #Jon's answer I would update it a little but on new apis. On new apis with themes and night themes (dark mode) I would do it by adding the v23/styles.xml and set the status bar background and text color there:
<item name="android:statusBarColor">#color/lightColor</item>
<item name="android:windowLightStatusBar">true</item>
And in the night/styles.xml:
<item name="android:statusBarColor" tools:targetApi="l">#color/darkColor</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">false</item>
The default styles.xml wouldn't contain any of this code, or just this, but remember to not set it to light:
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
This way we are setting the light background (and text color) for status bar but only for devices with api 23+. On devices <23 background will not be changed, as I think this is something that we dont want knowing that the text color will stay white.
The dark theme was added on API 29, so we don't have to be afraid of dark theme on api 21 ;)
The drawback of this however is that we are adding another file that we will need to remember to manage.
As previous, the SYSTEM_UI_FLAG_LIGHT_STATUS_BAR do the work in my case, don't forget to set for higher than API 22.
add this to oncreate after the setContentView:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);// set status text dark
getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this,R.color.colorPrimaryDark));// set status background white
It works for me
Try this once.
In your activity onCreate() method, paste the following code.
try {
if (android.os.Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, R.color.color_red));
}
} catch (Exception e) {
e.printStackTrace();
}
Note: color_red - is the status bar colour.
In your activity onCreate() method, paste the following code after the setContentView(R.layout.activity_generic_main);
Here is the sample code below.
public class GenericMain extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generic_main);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
Try this if not splash page
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getActivity().getWindow().setNavigationBarColor(ContextCompat.getColor(context, R.color.white));
getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(context, R.color.white));
With API 21+, this works for me:
WindowInsetsControllerCompat windowInsetsController =
WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
windowInsetsController.setAppearanceLightStatusBars(true);
I use
Theme.AppCompat.DayNight
and this code works for the Day and for the Night mode:
getWindow().setStatusBarColor(getWindow().getNavigationBarColor());
WindowInsetsControllerCompat windowInsetsController =
WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO)
windowInsetsController.setAppearanceLightStatusBars(true);
else
windowInsetsController.setAppearanceLightStatusBars(false);
To have a white status bar and black text color do this (Kotlin):
In the onCreate function of your main activity add this
val window: Window = window
WindowInsetsControllerCompat(window,window.decorView).isAppearanceLightStatusBars = true
In resoursces/styles.xml add this
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:statusBarColor">#ffffff</item> <!-- this line sets the status bar color (in my case #ffffff is white) -->
<!-- the following lines are not required -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimary</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
This works with API level 21 as well.
For anyone in the future looking to change status bar color from white programmatically in a fragment and back to primary dark when leaving fragment for minimum api 21< 23 in android using Java
private void updateStatusBar(boolean isEnter){
Window window = requireActivity().getWindow();
int color = ContextCompat.getColor(requireActivity(),R.color.colorAlertDialog);
if(isEnter) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
else
clearDecorFlags(window);
}
else {
color = ContextCompat.getColor(requireActivity(),R.color.colorPrimaryDark);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
window.getDecorView().setSystemUiVisibility(window.getDecorView().getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
else
clearDecorFlags(window);
}
window.setStatusBarColor(color);
}
private void clearDecorFlags(Window window){
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
So it's a bit different in case of kotlin
//for Dark status bar icon with white background
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.white))
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv())
getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.black))
// for dark background and light theme colours of icon.
I have searched everywhere but didn't get any help. I want to use a background image as full screen which will cover status bar and the footer navigation also which having a back button with home button and a task switcher button.
Like below image:
Transparent navigation bar work for Android 4.4+ (API 19) (just like status bar).
values/styles.xml
<style name="FullScreenEffectTheme" parent="Theme.AppCompat.NoActionBar">
<!-- ... -->
</style>
values-v19/styles.xml
<style name="FullScreenEffectTheme">
<!-- StatusBar -->
<item name="android:statusBarColor">#android:color/transparent</item>
<!-- NavigationBar -->
<item name="android:navigationBarColor">#android:color/transparent</item>
</style>
In code set flags:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
Don't forget to set this style to activity :)
Then you need to add padding or Space to your layout.
Important: If you want to have landscape orientation for phones translucent navigation bar cause ugly issue and in this case you should off this feature. I solved it like Tanner Perrien explained here.
An app that uses immersive mode leaves a black bar at the bottom of the screen when the app is returned to after waiting a while (activity is destroyed).
What is happening: (I have enabled the developer option: "Don't keep activities" to reproduce this).
First time launch of the app. Immersive mode works as expected.
Swipe up to reveal the 'immersive sticky' nav bar, and use the 'Home' button to leave the app. The background of the nav bar briefly shows a black background before the app closes.
Use the 'Recents' button, and select the app to resume it.
The app opens to briefly reveal the nav bar over a black bar. The system ui collapses into immersive mode but the black bar remains.
This bug also only appears on Lollipop, not KitKat.
I have stripped back the app to simply launching a dummy activity with no functionality apart from setting System UI flags:
public class DummyActivity extends FragmentActivity {
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
setSystemUiVisibility();
}
}
public void setSystemUiVisibility() {
if (getWindow() != null && getWindow().getDecorView() != null) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}
EDIT:
After creating a new fresh project with only this Activity I have seen this issue reproduced when using an app theme extending "android:Theme.Holo"..., and fixed the issue in this sample project when I extend the Material theme instead:
Change
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar.Fullscreen">
</style>
to
<style name="AppTheme"parent="android:Theme.Material.Light.NoActionBar.Fullscreen">
</style>
Unfortunately this fix hasn't solved the issues in my main project, but it brings me closer to a solution and might help others with the same issue.
I was having the same problem. Here are a few updates I made which made it go away. Hopefully one of these works for you!
activity_main.xml
android:fitsSystemWindows="false"
style-v21
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowSharedElementsUseOverlay">false</item>
MainActivity.java
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
);
For me it was a black bar at the top. Key thing was this in the activity style:
// Important to draw behind cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
Here is my code:
1- In Activity
private fun hideSystemUI() {
sysUIHidden = true
// 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
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // Hide nav bar
or View.SYSTEM_UI_FLAG_FULLSCREEN // Hide status bar
)
}
private fun showSystemUI() {
sysUIHidden = false
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // layout Behind nav bar
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // layout Behind status bar
)
}
2- In root view of xml layout
android:fitsSystemWindows="false"
3- Style for the Full screen Activity:
<style name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">#style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
<item name="android:statusBarColor">#50000000</item>
<item name="android:navigationBarColor">#50000000</item>
// Important to draw behind cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">#color/sysTransparent</item>
</style>
Using
<style name="AppTheme"parent="android:Theme.Material.Light.NoActionBar.Fullscreen"> </style>
does solved the problem, but it only work for api:21.
In order to make it works for all system. You can create a directory called style-v21 in res directory, and put that style.xml there. Android system is smart enough to pick which one to use for new or old phone.