Change status bar color with AppCompat ActionBarActivity - android

In one of my Activities, I changed the Toolbar color using Palette. But on 5.0 devices using ActionBarActivity the status bar color is the color of my colorPrimaryDark in my activity theme so I have 2 very different colors and it does not look good.
I realize that in 5.0 you can use Window.setStatusBarColor() but ActionBarActivity does not have this.
so my question is in 5.0 how can I change the status bar color with ActionBarActivity?

I'm not sure I understand the problem.
I you want to change the status bar color programmatically (and provided the device has Android 5.0) then you can use Window.setStatusBarColor(). It shouldn't make a difference whether the activity is derived from Activity or ActionBarActivity.
Just try doing:
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);
}
Just tested this with ActionBarActivity and it works alright.
Note: Setting the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag programmatically is not necessary if your values-v21 styles file has it set already, via:
<item name="android:windowDrawsSystemBarBackgrounds">true</item>

There are various ways of changing the status bar color.
Using the styles.xml. You can use the android:statusBarColor attribute to do this the easy but static way.
Note: You can also use this attribute with the Material theme.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
You can get it done dynamically using the setStatusBarColor(int) method in the Window class. But remember that this method is only available for API 21 or higher. So be sure to check that, or your app will surely crash in lower devices.
Here is a working example of this method.
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(getResources().getColor(R.color.primaryDark));
}
where primaryDark is the 700 tint of the primary color I am using in my app. You can define this color in the colors.xml file.

I don't think the status bar color has been implemented in AppCompat yet. These are the attributes which are available:
<!-- ============= -->
<!-- Color palette -->
<!-- ============= -->
<!-- The primary branding color for the app. By default, this is the color applied to the
action bar background. -->
<attr name="colorPrimary" format="color" />
<!-- Dark variant of the primary branding color. By default, this is the color applied to
the status bar (via statusBarColor) and navigation bar (via navigationBarColor). -->
<attr name="colorPrimaryDark" format="color" />
<!-- Bright complement to the primary branding color. By default, this is the color applied
to framework controls (via colorControlActivated). -->
<attr name="colorAccent" format="color" />
<!-- The color applied to framework controls in their normal state. -->
<attr name="colorControlNormal" format="color" />
<!-- The color applied to framework controls in their activated (ex. checked) state. -->
<attr name="colorControlActivated" format="color" />
<!-- The color applied to framework control highlights (ex. ripples, list selectors). -->
<attr name="colorControlHighlight" format="color" />
<!-- The color applied to framework buttons in their normal state. -->
<attr name="colorButtonNormal" format="color" />
<!-- The color applied to framework switch thumbs in their normal state. -->
<attr name="colorSwitchThumbNormal" format="color" />
(From \sdk\extras\android\support\v7\appcompat\res\values\attrs.xml)

Just paste this function in your Utils class where you keep your all other common functions.
fun Activity.changeStatusBarColor(color: Int, isLight: Boolean) {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = color
WindowInsetsControllerCompat(window, window.decorView).isAppearanceLightStatusBars = isLight
}
and use it from anywhere like this:
changeStatusBarColor(
ContextCompat.getColor(
context,
R.color.black
), false
)
Note that here I also have managed the dark and light status bar colors separately to manage out icon and text colors of status bar.

Try this,
I used this and it works very good with v21.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimaryDark">#color/blue</item>
</style>

[Kotlin version] I created this extension that also checks if the desired color has enough contrast to hide the System UI, like Battery Status Icon, Clock, etc, so we set the System UI white or black according to this.
fun Activity.coloredStatusBarMode(#ColorInt color: Int = Color.WHITE, lightSystemUI: Boolean? = null) {
var flags: Int = window.decorView.systemUiVisibility // get current flags
var systemLightUIFlag = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
var setSystemUILight = lightSystemUI
if (setSystemUILight == null) {
// Automatically check if the desired status bar is dark or light
setSystemUILight = ColorUtils.calculateLuminance(color) < 0.5
}
flags = if (setSystemUILight) {
// Set System UI Light (Battery Status Icon, Clock, etc)
removeFlag(flags, systemLightUIFlag)
} else {
// Set System UI Dark (Battery Status Icon, Clock, etc)
addFlag(flags, systemLightUIFlag)
}
window.decorView.systemUiVisibility = flags
window.statusBarColor = color
}
private fun containsFlag(flags: Int, flagToCheck: Int) = (flags and flagToCheck) != 0
private fun addFlag(flags: Int, flagToAdd: Int): Int {
return if (!containsFlag(flags, flagToAdd)) {
flags or flagToAdd
} else {
flags
}
}
private fun removeFlag(flags: Int, flagToRemove: Int): Int {
return if (containsFlag(flags, flagToRemove)) {
flags and flagToRemove.inv()
} else {
flags
}
}

Thanks for above answers, with the help of those, after certain R&D for xamarin.android MVVMCross application, below worked
Flag specified for activity in method OnCreate
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
this.Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
}
For each MvxActivity, Theme is mentioned as below
[Activity(
LaunchMode = LaunchMode.SingleTop,
ScreenOrientation = ScreenOrientation.Portrait,
Theme = "#style/Theme.Splash",
Name = "MyView"
)]
My SplashStyle.xml looks like as below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">#color/app_red</item>
<item name="android:colorPrimaryDark">#color/app_red</item>
</style>
</resources>
And I have V7 appcompact referred.

Applying
<item name="android:statusBarColor">#color/color_primary_dark</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
in Theme.AppCompat.Light.DarkActionBar didn't worked for me. What did the trick is , giving colorPrimaryDark as usual along with android:colorPrimary in styles.xml
<item name="android:colorAccent">#color/color_primary</item>
<item name="android:colorPrimary">#color/color_primary</item>
<item name="android:colorPrimaryDark">#color/color_primary_dark</item>
and in setting
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window window = this.Window;
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
}
didn't had to set statusbar color in code .

This can be implemented if you're using Kotliln in Android:
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.statusBarColor = Color.WHITE

add this kotlin code in OnCreate() of Activity or Fragment :
if (Build.VERSION.SDK_INT >= 21) {
val window: Window = requireActivity().window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.statusBarColor = resources.getColor(R.color.very_light_pink)
}

Related

Adding a Badge on Overflow Menu icon - Android

Is there any easy way of adding a simple badge/dot on the Overflow Menu (three-dot menu) icon on the Toolbar?
I'm trying to show the user that there is a new menu item inside. Note that I should be able to add/remove this programmatically. Any help is appreciated.
First, customize the overflow button style in styles.xml:
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<!-- Your theme customization... -->
<item name="android:actionOverflowButtonStyle">#style/MyActionOverflowButtonStyle</item>
</style>
<!-- Here you choose the ID of the overflow icon. -->
<item type="id" name="myActionOverflowButtonID"/>
<!-- Style to replace action bar overflow icon. -->
<style name="MyActionOverflowButtonStyle" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="android:id">#id/myActionOverflowButtonID</item>
</style>
(inspired by How to change three dots button on android to other button, but with android:id instead of android:src in the style)
Then, add a badge:
// Get the button view
val view = findViewById<View>(R.id.myActionOverflowButtonID)
// Create and customize a badge, e.g:
val badgeDrawable = BadgeDrawable.create(context).apply {
backgroundColor = getColor(R.color.primary)
horizontalOffset = 30
verticalOffset = 30
number = 2
}
// Attach the badge to the button
// For this API, you need to update Material components dependency to 1.3.0 or later
BadgeUtils.attachBadgeDrawable(badgeDrawable, view)
You can use simple BadgeDrawable:
val badgeDrawable = BadgeDrawable.create(context).apply {
// configure (set background color, offsets, etc)
}
and add/remove it later:
BadgeUtils.attachBadgeDrawable(badgeDrawable, toolbar, menuItemId)
or
BadgeUtils.detachBadgeDrawable(badgeDrawable, toolbar, menuItemId)

how to set the background color of the status bar during the launching phase [duplicate]

This question already has answers here:
How to change the status bar color in Android?
(29 answers)
Closed 5 years ago.
Before my app was with android:targetSdkVersion="14" but i decide to make it a little more modern and update it to android:targetSdkVersion="22". However now i have a little problem, the color of the top status bar during the launch phase is now gray (like on the picture) and i would prefer to have it black (like it's was before).
Any idea what i need to do to have my status bar black again during the launch phase ?
NOTE
I would like to change the color of the statusbar via the styles.xml. actually i try to add
<item name="android:colorPrimaryDark">#android:color/black</item>
or
<item name="android:statusBarColor">#android:color/black</item>
didn't help! this question is not a duplicate of other because i can set the StatusBar color to be black without any problem after the app is launched or fully loaded. I need to setup the black background for the statusbar during the launching phase, i mean when user click on the icon of the app their is something like a launch screen (with a define to be black via <item name="android:windowBackground">#android:color/black</item>) but the statusbar color of this launch sreen is gray (only if android:targetSdkVersion="22")
The Easiest Way is :got to res -> value -> styles and add this
1: Add this in your color xml file
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#000000</color>
<color name="colorAccent">#FF4081</color>
</resources>
2:Add this in your styles.xml file
<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>
</style>
Note:
colorprimary is for Actionbar
colorPrimaryDark is for Statusbar
your can change these colors and u could get ur required result .
OUTPUT :
hope answerd the question if its useful then vote up
try this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(activity, R.color.background););
}
try this:
// Change color of action bar
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0099CC")));

Change status bar text color when primaryDark is white

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.

Can I set FLAG_LAYOUT_NO_LIMITS only for status bar?

I need to make transparent status bar. I am using getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) and it is make status bar as I want. But it also affect navigation bar: it became transparent and getWindow().setNavigationBarColor(Color.BLACK) do nothing.
Is there way to make transparent status bar only and not navigation bar?
this work for me
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
styles.xml
<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
v21\styles.xml
<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
status bar will be transparent or translucent, navigation bar won't
hope this helps!
using mikepenz's comment
what I exactly working code (converted to kotlin) below here.
// at AppCompatActivity, min SDK is 16, I tested api 25
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
}
if (Build.VERSION.SDK_INT >= 19) {
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
if (Build.VERSION.SDK_INT >= 21) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.statusBarColor = Color.TRANSPARENT
}
setContentView(R.layout.activity_main)
}
Scroll down to check how the end result looks like
First of all, define your styles.xml something like this-
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
DO NOT add the following line
<item name="android:windowTranslucentStatus">true</item>
Adding above line will NOT shift the layout up when the soft keyboard is shown on a Dialog with an EditText
Then override this style in v21 and v23 styles like this-
v21/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>
v23/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>
Activity code - Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setFlags(
LayoutParams.FLAG_LAYOUT_NO_LIMITS,
LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
setContentView(R.layout.YOUR_LAYOUT_RESOURCE_ID)
.
.
.
}
Activity code - Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(
LayoutParams.FLAG_LAYOUT_NO_LIMITS,
LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
setContentView(R.layout.YOUR_LAYOUT_RESOURCE_ID)
.
.
.
}
End result
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
fun showTransparentStatusbar() {
activity!!.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
fun removeStatusbarFlags() {
activity!!.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
Try this and wont regret it
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)
You can use like this
to hide status bar and navigation bar
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
getWindow().setAttributes(attributes);
and for showing the navigationBar again use this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
the color is grey for me, maybe you can force it to your primary color
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
Just flag above worked for me. Navigation buttons are visible, status bar and action bar are hidden.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
Is not working. Test device nexus 5.
i found the solution bro
<style name="transparent" parent="Theme.AppCompat.Light.NoActionBar">//AppCompat is the key; You can choose any other theme than the light-theme, but stick to AppCompat
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
//Other styling(optional)
</style>
and then apply this transparent theme to your activity manifest like this
<activity
...
android:theme="#style/transparent"/>
For all those interested in workarounds/hacks because Android API concerning this matter is just awful. Do not use any system window flags, set negative margin to the root of your layout, and then use setStatusBarColor to make your StatusBar transparent.
statusBarHeight = getResources().getDimensionPixelSize(R.dimen.margin_24dp);
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId != 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams) binding.getRoot().getLayoutParams();
rootParams.topMargin = -statusBarHeight;
binding.getRoot().setLayoutParams(rootParams);
getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));
where binding.getRoot() is of course the root of your layout
and result is
yes , You can use this code in Style.xml
<style name="transparent" parent="Theme.AppCompat.Light">//AppCompat is the key; You can choose any other theme than the light-theme, but stick to AppCompat
<item name="colorPrimaryDark">#00000000</item>//THIS is the important part.
//Other styling(optional)
</style>
Then to apply it to your layout, simply add the following line in the root layout(view):
android:theme="#style/transparent"
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
<style name="TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
This works on Samsung S10+ (Pie), Oppo F7 and Oppo F5S (Oreos):
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
As asked by the original poster, the app area is expanded on top and covers the status bar (where the clock is). But the android navigation (virtual) buttons remains at the bottom of the screen and the app sits on top of the android buttons.

Lollipop : draw behind statusBar with its color set to transparent

I have set my statusBar color to transparent for Lollipop only with the following line in my theme :
<item name="android:statusBarColor">#android:color/transparent</item>
Now I need to draw behind it, but I can't get any view draw behind it. I know how to do it with the windowTranslucentStatus property, but don't want to use this property since it will then ignore the color of the statusBar set to transparent.
Method #1:
To achieve a completely transparent status bar, you have to use statusBarColor, which is only available on API 21 and above. windowTranslucentStatus is available on API 19 and above, but it adds a tinted background for the status bar. However, setting windowTranslucentStatus does achieve one thing that changing statusBarColor to transparent does not: it sets the SYSTEM_UI_FLAG_LAYOUT_STABLE
and SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN flags. The easiest way to get the same effect is to manually set these flags, which effectively disables the insets imposed by the Android layout system and leaves you to fend for yourself.
You call this line in your onCreate method:
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
Be sure to also set the transparency in /res/values-v21/styles.xml:
<item name="android:statusBarColor">#android:color/transparent</item>
Or set the transparency programmatically:
getWindow().setStatusBarColor(Color.TRANSPARENT);
The good side to this approach is that the same layouts and designs can also be used on API 19 by trading out the transparent status bar for the tinted translucent status bar.
<item name="android:windowTranslucentStatus">true</item>
Method #2:
If you only need to paint a background image under your status bar, instead of positioning a view behind it, this can be done by simply setting the background of your activity's theme to the desired image and setting the status bar transparency as shown in method #1. This was the method I used to create the screenshots for the Android Police article from a few months ago.
Method #3:
If you've got to ignore the standard system insets for some layouts while keeping them working in others, the only viable way to do it is to work with the often linked ScrimInsetsFrameLayout class. Of course, some of the things done in that class aren't necessary for all scenarios. For example, if you don't plan to use the synthetic status bar overlay, simply comment out everything in the init() method and don't bother adding anything to the attrs.xml file. I've seen this approach work, but I think you'll find that it brings some other implications that may be a lot of work to get around.
I also saw that you're opposed to wrapping multiple layouts. In the case of wrapping one layout inside of another, where both have match_parent for height and width, the performance implications are too trivial to worry about. Regardless, you can avoid that situation entirely by changing the class it extends from FrameLayout to any other type of Layout class you like. It will work just fine.
This worked for my case
// Create/Set toolbar as actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Check if the version of Android is Lollipop or higher
if (Build.VERSION.SDK_INT >= 21) {
// Set the status bar to dark-semi-transparentish
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// Set paddingTop of toolbar to height of status bar.
// Fixes statusbar covers toolbar issue
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
}
// A method to find height of the status bar
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
For more information about working with statusBars: youtube.com/watch?v=_mGDMVRO3iE
Try this theme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimaryDark">#android:color/transparent</item>
<item name="colorPrimary">#color/md_blue_200</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
Be sure that, your layout set
android:fitsSystemWindows="false"
Instead of
<item name="android:statusBarColor">#android:color/transparent</item>
Use the following:
<item name="android:windowTranslucentStatus">true</item>
And make sure to remove the top padding (which is added by default) on your 'MainActivity' layout.
Note that this does not make the status bar fully transparent, and there will still be a "faded black" overlay over your status bar.
The solution from Cody Toombs almost did the trick for me. I'm not sure if this is Xamarin related or not, but I now have an acceptable solution:
This is my setup:
I have an Android project where I have referenced the Android.Support v4 and v7 packages. I have two styles defined:
values/styles.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyStyle" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
values-v21/styles.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyStyle" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
AndroidManifest targets "MyStyle":
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.agn.test.test">
<uses-sdk android:minSdkVersion="10" />
<application android:allowBackup="true" android:icon="#mipmap/icon" android:label="#string/app_name" android:theme="#style/MyStyle">
</application>
</manifest>
And finally the code in the Main Activity:
[Activity (Label = "Test", MainLauncher = true, Icon = "#mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Main);
//Resource.Layout.Main is just a regular layout, no additional flags. Make sure there is something in there like an imageView, so that you can see the overlay.
var uiOptions = (int)Window.DecorView.SystemUiVisibility;
uiOptions ^= (int)SystemUiFlags.LayoutStable;
uiOptions ^= (int)SystemUiFlags.LayoutFullscreen;
Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
Window.AddFlags (WindowManagerFlags.DrawsSystemBarBackgrounds);
}
}
Notice that I set DrawsSystemBarBackgrounds flag, this makes all the difference
Window.AddFlags (WindowManagerFlags.DrawsSystemBarBackgrounds);
I spent a lot of time getting it right, too much time in fact. Hopefully this answer helps anyone trying to achieve the same thing.
#Cody Toombs's answer lead to an issue that brings the layout behind the navigation bar. So what I found is using this solution given by #Kriti
here is the Kotlin code snippet for the same:
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true)
}
if (Build.VERSION.SDK_INT >= 19) {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
if (Build.VERSION.SDK_INT >= 21) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
getWindow().setStatusBarColor(Color.TRANSPARENT)
}
private fun setWindowFlag(activity: Activity, bits: Int, on: Boolean) {
val win: Window = activity.getWindow()
val winParams: WindowManager.LayoutParams = win.getAttributes()
if (on) {
winParams.flags = winParams.flags or bits
} else {
winParams.flags = winParams.flags and bits.inv()
}
win.setAttributes(winParams)
}
You also need to add
android:fitsSystemWindows="false"
root view of your layout.
I had the same problem so i create ImageView that draw behind status bar API 19+
Set custom image behind Status Bar gist.github.com
public static void setTransparent(Activity activity, int imageRes) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
// set flags
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);
}
// get root content of system window
//ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
// rootView.setFitsSystemWindows(true);
// rootView.setClipToPadding(true);
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
if (contentView.getChildCount() > 1) {
contentView.removeViewAt(1);
}
// get status bar height
int res = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int height = 0;
if (res != 0)
height = activity.getResources().getDimensionPixelSize(res);
// create new imageview and set resource id
ImageView image = new ImageView(activity);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
image.setLayoutParams(params);
image.setImageResource(imageRes);
image.setScaleType(ScaleType.MATRIX);
// add image view to content view
contentView.addView(image);
// rootView.setFitsSystemWindows(true);
}
You can use ScrimInsetFrameLayout
https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout.java
android:fitsSystemWindows="true" should set on scrim layout!
I will be adding some more information here. The latest Android developments have made it pretty easy to handle a lot of cases in status bar. Following are my observations from the styles.xml
Background color: for SDK 21+, as a lot of answers mentioned,<item name="android:windowTranslucentStatus">true</item> will make the status bar transparent and show in front of UI. Your Activity will take the whole space of the top.
Background color: again,for SDK 21+, <item name="android:statusBarColor">#color/your_color</item> will simply give a color to your status bar, without affecting anything else.
However, in later devices (Android M/+), the icons started coming in different shades. The OS can give a darker shade of gray to the icons for SDK 23/+ , if you override your styles.xml file in values-23 folder and add <item name="android:windowLightStatusBar">true</item>.
This way, you will be providing your user with a more visible status bar, if your status bar has a light color( think of how a lot of google apps have light background yet the icons are visible there in a greyish color).
I would suggest you to use this, if you are giving color to your status bar via point #2
In the most recent devices, SDK 29/+ comes with a system wide light and dark theme, controllable by the user. As devs, we are also supposed to override our style file in a new values-night folder, to give user 2 different experiences.
Here again, I have found the point #2 to be effective in providing the "background color to status bar". But system was not changing the color of status bar icons for my app. since my day version of style consisted of lighter theme, this means that users will suffer from low visibility ( white icons on lighter background)
This problem can be solved by using the point #3 approach or by overriding style file in values-29 folder and using a newer api <item name="android:enforceStatusBarContrast">true</item> . This will automatically enforce the grayish tint to icons, if your background color is too light.
With Android Studio 1.4, the template project with boiler plate code sets Overlay theme on your AppbarLayout and/or Toolbar. They are also set to be rendered behind the status bar by fitSystemWindow attribute = true. This will cause only toolbar to be rendered directly below the status bar and everything else will rendered beneath the toolbar. So the solutions provided above won't work on their own. You will have to make the following changes.
Remove the Overlay theme or change it to non overlay theme for the toolbar.
Put the following code in your styles-21.xml file.
#android:color/transparent
Assign this theme to the activity containing the navigation drawer in
the AndroidManifest.xml file.
This will make the Navigation drawer to render behind the transparent status bar.
Similar to some of the solutions posted, but in my case I did the status bar transparent and fix the position of the action bar with some negative margin
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Color.TRANSPARENT);
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
lp.setMargins(0, -getStatusBarHeight(), 0, 0);
}
And I used in the toolbar and the root view
android:fitsSystemWindows="true"
There is good library StatusBarUtil from #laobie that help to easily draw image in the StatusBar.
Just add in your build.gradle:
compile 'com.jaeger.statusbarutil:library:1.4.0'
Then in the Activity set
StatusBarUtil.setTranslucentForImageView(Activity activity, int statusBarAlpha, View viewNeedOffset)
In the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<ImageView
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:adjustViewBounds="true"
android:layout_height="wrap_content"
android:src="#drawable/toolbar_bg"/>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/view_need_offset"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<!-- Your layout code -->
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
For more info download demo or clone from github page and play with all feature.
Note: Support KitKat and above.
Hope that helps somebody else!
All you need to do is set these properties in your theme
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
The accepted answer worked for me using a CollapsingToolbarLayout. It's important to note though, that setSytstemUiVisibility() overrides any previous calls to that function. So if you're using that function somewhere else for the same view, you need to include the View.SYSTEM_UI_FLAG_LAYOUT_STABLE and View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN flags, or they will be overridden with the new call.
This was the case for me, and once I added the two flags to the other place I was making a call to setSystemUiVisibility(), the accepted answer worked perfectly.
Here is the theme I use to accomplish this:
<style name="AppTheme" parent="#android:style/Theme.NoTitleBar">
<!-- Default Background Screen -->
<item name="android:background">#color/default_blue</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
The Right solution is to Change a property in XML under your Activity tag to below style. It just works
android:theme="#style/Theme.AppCompat.Light.NoActionBar"

Categories

Resources