I would like to have the statusbar and the navigationbar Translucent on my Main activity, while all the other activities use the Material Design.
What I got so far is this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
But the result is:
I even tried to set the color to transparent:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(android.R.color.transparent));
window.setNavigationBarColor(getResources().getColor(android.R.color.transparent));
}
But I don't get the graduated shading I'm looking for to make the navigation buttons and the icons on the status bar visible:
Ideas?
I went around the problem.
I created a drawable that simulates the shade I was looking for and put it as wallpaper, while making the status bar and navigation bar transparent:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = this.getWindow();
Drawable background = this.getResources().getDrawable(R.drawable.background);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(android.R.color.transparent));
window.setNavigationBarColor(getResources().getColor(android.R.color.transparent));
window.setBackgroundDrawable(background);
}
Here's the result:
From Android Lollipop it is fully translucent, without any shade. So you should do a color like #33000000 to get a shade in the statusbar.
Edit
I think you can only set a color for the statusbar. But you can add an ImageView with a gradient drawable and only show it when you're on lollipop. The status bar is 25dp high. I think there is also an attribute for this, but I don't know it. This way you simulate a gradient in the statusbar.
Related
This question already has answers here:
How to change the color of the status bar in Android?
(11 answers)
Closed 2 years ago.
Is it possible to change the color of the upper part of the action bar? As shown in the attached image
Yes, you can. As #CommonsWare has said you can change the color of the StatusBar by editing the colorPrimaryDark item in your styles resource file.
Screenshot of what it looks like in Android Studio
To change the StatusBar Color through Java Code, you can add the following code to your activity. Note that the Android Device must be running on API 21 or higher.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getResources().getColor(R.color.darkPurple));
}
Also, if you want to change the foreground text of the StatusBar, you can just add the following code to the if block. Note that the Android Device must be running on API v23 or higher.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// for a black color foreground text:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
// for a white color foreground text, just replace setSystemUiVisibility param with ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
First of all this is the Status bar not the Action bar. However, if you want to change the statusbar color grammatically you can use Window.setStatusBarColor(). But the device should have android 5.0.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.BLUE);
}
Should work for you.
I'm trying to change the status bar color in a DialogFragment.
However, on some devices, like OnePlus6 (Android 9) it doesn't work.
Furthermore it makes the status bar icons dissapear.
Here's the snippet I'm using:
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = Color.WHITE
Also tried:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
Try this:
getActivity().getWindow().clearFlags(WindowManager.FLAG_FULL_SCREEN);
I have to make layout, where top of the layout should be drawn under SystemBars. (min API level for this app is 22)
I set these flags to achieve that. In other activities I can draw whole fragment with views under SystemBars, but here I cant do it.
I used this code to allow layout to be drawn under SystemBars
window.apply {
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
statusBarColor = resources.getColor(android.R.color.transparent)
navigationBarColor = resources.getColor(android.R.color.transparent)
setBackgroundDrawable(background)
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
This is result in app:
I need to draw that grey area under system bar.
Update:
#Redman solution worked only partially.
Now I can draw under StatusBar but also I can draw under software buttons (navigationButtons) as you can see at screen below. Is there any way how to allow layout to be drawn under SystemBars but do not allow it under navigationButtons? Because that code what I've posted works for different fragments in app. But in other fragments I don't have ImageView as a part of ToolBar. If you have just ToolBar with for example 2-color gradient background and some backButton + SearchView, it is working fine. But as I add ImageView, ImageView will not "slide" under top SystemBar(aka StatusBar). Even in image above you can clearly see that green color (it's ToolBar background drawn under StatusBar). But ImageView is not allowed to be drawn under that bar.
The only way how to achieve that was with that special line of code
window.apply {
setFlags( WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}
but this line also allowed to draw layout content under NavigationButtons (which is an issue).
In your Activity Theme add these properties
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
and in parent layout add property
android:fitsSystemWindows="true"
Edit
Try this to do it programmatically, it should work above on devices having kitkat and above
window.apply {
setFlags( WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}
use toolbar view in your xml file. See below example
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="#dimen/height_50dp"
android:background="#f1f1f1">
\\define your any element here. Like back arrow or title
</android.support.v7.widget.Toolbar>
All you need to do is set these properties in your theme:
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
Your activity / container layout you wish to have a transparent status bar needs this property set:
android:fitsSystemWindows="true"
It is generally not possible to perform this for sure on pre-kitkat, looks like you can do it but some strange code makes it so.
EDIT: I would recommend this lib: https://github.com/jgilfelt/SystemBarTint for lots of pre-lollipop status bar color control.
Well after much deliberation I've learned that the answer to totally disabling the translucency or any color placed on the status bar and navigation bar for lollipop is to set this flag on the window:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow(); // in Activity's onCreate() for instance
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
How about this:
Window window = getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(this, R.color.primary_dark));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
window.addFlags(flags);
}
This should make Only StatusBar transparent.
If this didn't help, try adding:
<item name="android:windowTranslucentStatus">true</item>
To the Activity styles code in styles.xml.
Where the action bar meets the status bar there is this weird tint or elevation.
Does anyone know where I could find the settings to change it?
Thanks
What it normally looks like:
What it looks like now:
Edit:
I am also running Cyanogenmod 13.
Not sure where its adding that tint, but I was able to set it after it added back to normal
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setElevation(0);
window.setStatusBarColor(getColor(R.color.colorPrimaryDark));
}
I am creating an application with some Activities. I want to change the status bar color and status bar icon colors which should look like the following image.
.
Is it possible to change? I am able to change the background color using the following code.
if (android.os.Build.VERSION.SDK_INT >= 21)
{
Window w = getWindow(); // in Activity's onCreate() for instance
w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
w.setStatusBarColor(getResources().getColor(R.color.white_background_text));
}
Above code is working fine. Now I want to change the Wi-Fi icon, network icon, battery and time. Is it possible to do?
From another answer
Yes it's possible to change it to gray (no custom colors) but this only works from API 23 and above you only need to add this in your values-v23/styles.xml
<item name="android:windowLightStatusBar">true</item>