I want to make the navigation bar fully transparent in my app. I have tried the other answers, but they only make the status bar fully transparent and the navigation bar still has a dark half-transparent color. Here is my code:
styles.xml:
<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:navigationBarColor">#android:color/transparent</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
Activity.java:
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
Here is the result I'm trying to get: goal
and here is the result I'm currently getting: result
Is there any way to achieve a fully transparent navigation bar?
Try to set <item name="android:android:windowTranslucentStatus">true</item> set android:statusBarColor to #android:color/transparent.
Then add code below:
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
If you also want the navigation bar to be translucent, set android:navigationBarColor to #android:color/transparent and combine the flag View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION as well.
Try it may it will work.
Related
I have activity where I overlay Android status bar over a picture near the top of the screen.
I achieve that by inheriting activity style from Theme.AppCompat.DayNight.NoActionBar and then doing this in onCreate():
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
That achieves transparent status bar at the top, which is fine.
The problem is, this also makes Android navigation bar at the bottom transparent and overlaid over controls near the bottom of the activity, which is not what I want. I want navigation bar to be opaque, and take up its vertical space without going over my controls, as in common activities that don't spill over status bar.
Is it possible to control transparency and overlap of the navigation bar separately from the status bar without any kind of artificial margin or padding ?
If I understand correctly, your intention is to get a transparent status bar without touching the navigation bar as shown in the screenshot below.
Personally I also tried to use the parameter FLAG_LAYOUT_NO_LIMITS without achieving the desired result. The problem was that I get the status bar and navigation bar at the same time being "covered" by the graphics as buttons. A temporary solution was to detect the navigation bar and programmatically add padding to the graphics. This is partially useless, as many android users use the adb shell overscan command to hide the navigation bar. The main problem with this command is that it effectively hides the navigation bar but deceives the system as the boolean that represents whether the bar is present or not is not changed. This API was completely removed in Android 11.
The desired effect can be achieved by playing with styles.
Add these attributes to your activity style:
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
You will get a style like this:
<style name="ExampleThemeStackOverFlow" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/colorAccentX</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:navigationBarColor">#color/colorPrimary</item>
<item name="android:windowLightNavigationBar" tools:targetApi="27">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
I want my toolbar to take the space of status bar and the image used for it be the background of the status bar and I successfully do that with this code inside my activity
//for using status bar space
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
The problem is this code make my activity layout take the full screen even the space with navigation bar so how to solve this?
Here is a screenshot:
This is the solution I used for testing on android 10 and other versions, so check if this is of any help.
In your styles.xml create a custom theme, you can set parent theme to what your app theme is if it has any of the NoActionBar (AppCompat/DayNight/material etc.) set as parent or set one directly(since without NoActionBar a default one will get generated on top), the major requirement is these three lines:
<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:statusBarColor">#00000000</item>
</style>
You can check the documentation for statusBarColor if you wish, basically tells you what needs to be done.
The color for the status bar. If the color is not opaque, consider setting {#link android.view.View#SYSTEM_UI_FLAG_LAYOUT_STABLE} and {#link android.view.View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}. For this to take effect, the window must be drawing the system bar backgrounds with {#link android.R.attr#windowDrawsSystemBarBackgrounds} and the status bar must not have been requested to be translucent with {#link android.R.attr#windowTranslucentStatus}. Corresponds to {#link android.view.Window#setStatusBarColor(int)}.
So simply set a transparent color or any color you wish to set to the status bar as per your requirement. And set this theme to the activity you want.
Now in the activity create this method as shown below:
private void showCustomUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
Those are the flags you require, don't add View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION while setting flags as it hides the navigation bar which your requirement is to show.
You'll need to call the method otherwise statusbar won't be created properly.
So try this, and let me know if it worked for you.
EDIT: Don't forget to remove FLAG_LAYOUT_NO_LIMITS flags incase you have them in activity as it will create problems. Here's what i got on testing for android 10.
Method 1
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
if setting windowTranslucentStatus to true won't work you can try this
Method 2
styles.xml ; just change the parent and don't add windowTranslucentStatus
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
override style for v21 & v23
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
</style>
and add this in your activity.class
getWindow().setFlags(
LayoutParams.FLAG_LAYOUT_NO_LIMITS,
LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
reference from answer1, answer2
hope this helps!
By using these flags you can achieve that.
private void showSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
call this method.
I have first encountered this strange behaviour in my main app which is currently in development. In order to debug the issue i created a test app.
The test app has only one activity MainActivity which extends Activity.
The theme for this activity is AppTheme.NoActionBar declared in styles.xml
<style name="AppTheme">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme.Base">
<item name="android:statusBarColor">#FFFFFF</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="AppTheme.Base" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
With this the output looks like this Image 1
Now that's not what i expected,look how the status bar and toolbar are merged together.
But when i removed this line of code
<item name="android:windowTranslucentNavigation">true</item>
This is what i ended up with
Notice this time the toolbar and status bar are not merged, instead we have the status bar in place and the toolbar below it.
I am unable to understand how in Image 1 the layout is getting drawn under the status bar
I know the question will confuse a lot of you reading this. But how can adding a attribute
<item name="android:windowTranslucentNavigation">true</item>
which has nothing to do with status bar is effecting it.
My test device is running on android Pie
Okay so after a bit of research i found that
android:windowTranslucentNavigation=true
not only makes the navigation bar translucent, but it also allows the layout to draw draw behind the status bar.
The problem is that if we want to have a colored navigation bar (for Lollipop and higher) instead of translucent, we need to remove that line and by doing so we won't be able to draw behind the status bar anymore.
Workaround:
Declare styles.xml(v21) and set the navigation bar color
<item name="android:navigationBarColor">#color/red</item>
Now since this styles.xml file will be used only for devices with API >= 21 we need to make sure that the layout is also drawn behind the status bar for API >= 21 so,add the following lines in your activity's onCreate before setContentView
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) // Checks the API level of the device
{
getWindow()
.getDecorView();
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
Conclusion:
For devices (API < 21) android:windowTranslucentNavigation=true will make the navigation bar translucent and the layout will be drawn behind the status bar and for devices (API >= 21) we can modify the color of the navigation bar without breaking the UI
I have a tabbed activity with my custom theme that looks like this
This is the styles.xml for that theme
<style name="myCustomDialog" parent="Theme.AppCompat.Dialog">
<item name="colorPrimaryDark">#color/MyTransparent</item>
<item name="windowNoTitle">true</item>
<item name ="android:textColor">#color/PaleBlack</item>
<item name="android:editTextColor">#color/PaleBlack</item>
<item name="android:buttonStyle">#style/myCustomButon</item>
</style>
I used this line of code to expand the size of the dialog
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
But when I do, I get a black bar at the top, which I think is the action bar, because when I set the colorPrimaryDark to transparent, it turned grey. It looks like this
How can I get rid of the bar?
Try this:
Window window = getWindow();
// clear all flags but dim behind
window.setFlags(FLAG_DIM_BEHIND, Integer.MAX_VALUE);
I would like to change the color of highlighted bar in Android Studio:
How can i do it?
You can change it by setting the android:statusBarColor or android:colorPrimaryDark attribute of the style you're using for your app in styles.xml.
(android:statusBarColor inherits the value of android:colorPrimaryDark by default)
For example (since we're using an AppCompat theme here, the android namespace is omitted):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">#color/your_custom_color</item>
</style>
On API level 21+ you can also use the Window.setStatusBarColor() method from code.
From its docs:
For this to take effect, the window must be drawing the system bar
backgrounds with
WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS and
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS must not be set.
If color is not opaque, consider setting
View.SYSTEM_UI_FLAG_LAYOUT_STABLE and
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN.
To set these flags you could do something like this:
// getWindow() is a method of Activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
The status bar is a system window owned by the operating system.
On pre-5.0 Android devices, applications do not have permission to alter its color, so this is not something that the AppCompat library can support for older platform versions. The best AppCompat can do is provide support for coloring the ActionBar and other common UI widgets within the application.
On post-5.0 Android devices,
Changing the color of status bar also requires setting two additional flags on the Window; you need to add the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag and clear the FLAG_TRANSLUCENT_STATUS flag.
Window window = activity.getWindow();
// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// finally change the color
window.setStatusBarColor(activity.getResources().getColor(R.color.my_statusbar_color));
You can also add these lines of code in the main activity
if (Build.VERSION.SDK_INT >= 21)
{
getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.statusbar)); //status bar or the time bar at the top (see example image1)
getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.dark_nav)); // Navigation bar the soft bottom of some phones like nexus and some Samsung note series (see example image2)
}
example image1 setStatusBarColor
example image2 setNavigationBarColor
add the status bar color to your style and done
<item name="android:statusBarColor">#color/black</item>
this is for API level 21+
changing status bar color is available just for android above lollipop
1.you can change status bar color programmatically by this line:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(context, R.color.your_color));
}
2.you can do this with an smooth transition animation:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int startColor = getWindow().getStatusBarColor();
int endColor = ContextCompat.getColor(context, R.color.your_color);
ObjectAnimator.ofArgb(getWindow(), "statusBarColor", startColor, endColor).start();
}
3.or you can add this to your theme style in values/styles.xml file. item colorPrimaryDark will be used for your app status bar color
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorPrimaryDark">#color/your_color</item>
will only be visible in Lollipop and greater than Lollipop(API) devices.
P.S. you need to have Theme.AppCompat as your base/main theme
If you use custom actionBar so you can try this one.
<style name="AppThemeBrowser" parent="Theme.AppCompat.NoActionBar">
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style>
Or if you use AppTheme actionBar so, you can try this one
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:statusBarColor">#color/colorPrimaryDark</item>
------------------------------------
</style>
Hope this will help you.
HappyCoding
Change it in the themes as usual, like not using jetpack compose.
colorPrimary and colorPrimaryVariant in themes and themes night.
<style name="Theme.AndroidDevChallenge" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/SweetRed</item>
<item name="colorPrimaryVariant">#color/SweetRedDarker</item>
<item name="colorOnPrimary">#color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_200</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
Right click theme folders in the values folder, which is in the res folder :|
<!--write something like this in the color.xml, with your own color code-->
<color name="status_bar_light">#FFea80fc</color>
<color name="status_bar_dark">#FF263238</color>
In themes.xml, change colorPrimaryVariant to this
<item name="colorPrimaryVariant">#color/status_bar_light</item>
Add this in theme.xml(night)
<item name="colorPrimaryVariant">#color/status_bar_dark</item>
And there you have it, happy coding ;)
To change the color for particular activity just use
app:statusBarBackground="#color/colorPrimaryDark"
or any other color value in place of #color/colorPrimaryDark in the mail layout file of your desired activity in the parent layout tag like in my case default it was CoordinatorLayout which is parent of all (Keep all other layout within this CoordinatorLayout otherwise it may not work)
Hope this helps, this worked in my case although i'm not completely sure how.
Note:- Status bar color is supported on api level 19 or 21 and above api level.
Please check this Link : change status bar color