I have received a design spec. in which the status bar icons' color is to be changed completely. I know I can change the status bar color in theme, however that is not my question. I want to override the icon color/icons themselves as shown in the image attached.
I searched a bit but could not find a way to do it.
I am aware that as a last resort, I can change the existing status bar icon tintcolor the following attribute in my app theme.
<item name="android:windowLightStatusBar" tools:targetApi="23">true</item>
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.
<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
Related
I am Struggling to change the color of the status bar for each activity.
I have tried the create theme and style listed in posts on this site to no avail.
How can I make each activity have a status bar of different color?
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.
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
I'm trying to make the background colour of my ActionBar a colour corresponding to the code. For some reason my app crashes when it reaches this line of code.
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3f9845")));
When I remove it the error goes (ActionBar background is set to black by default). I've tried rearranging the line to be above and below
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_register1);
but still no luck.
This is the theme I'm using if it has any relevance
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> </style>
Thanks in advance for any help
If you are using AppCompat, you need to call getSupportActionBar() instead.
If you want to change the color by style with the recent versions of AppCompat, you just need to set the "colorPrimary" attribute in your theme to the color you want.
you can set it in the style:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/my_action_bar_color</item>
</style>
I have an Activity with an action bar. minSdkVersion is 11.
The action bar is a grey color, I would like to change it to a different color so that it matches other colors in my application.
I have created the following styles
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#FFF</item>
</style>
And set this activity's theme in the appmanifest to MyTheme but when I run the application I get this error:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
What am I doing wrong?
See https://developer.android.com/training/basics/actionbar/styling.html, specifically these parts
Note: If you are using the Support Library APIs for the action bar,
then you must use (or override) the Theme.AppCompat family of styles
(rather than the Theme.Holo family, available in API level 11 and
higher)
and
When using the Support Library, you must instead use the Theme.AppCompat themes:
Theme.AppCompat for the "dark" theme.
Theme.AppCompat.Light for the "light" theme.
Theme.AppCompat.Light.DarkActionBar for the light theme
with a dark action bar.
Basically you have to replace 'Holo' with 'AppCompat'
If you are using ActionBarActivity, you need to use an AppCompat theme, which brings the Lollipop style Action Bars to all devices and support for the Material Color Palette allowing you to write a theme such as
<style name="MyTheme" parent="#android:style/Theme.AppCompat.Light">
<item name="ColorPrimary">#color/primary</item>
</style>
to style your action bar color automatically.
If you'd prefer to not use any of that (and look very different on Lollipop devices), you can instead extend FragmentActivity or Activity, depending on if you want to use the support library fragments (which backport and fix a number of issues around nested fragments and state saving).
Just use this, if your extending ActivityActionBar on your activity then you need to use the AppCompat themes.
<style name="AppCompatTheme" parent="#android:style/Theme.AppCompat.Light">
<item name="ColorPrimary">#color/primary</item>
</style>
or use style/Theme.AppCompat.Dark for white text :)
I am using AppCompat and have successfully implemented the new action bar that rolled out with lollipop. The only problem is the pressed background color of the action items. I want to show a different background color for the action item when pressed. Any idea how it can be done?
You have a couple of options. But first, some background:
The action items in AppCompat use theme attribute ?attr/actionBarItemBackground (see res/values/styles_base.xml) which is set to ?attr/selectableItemBackgroundBorderless (see res/styles/themes_base.xml) by default. This attribute is set to a borderless ripple on L and #drawable/abc_item_background_holo_light on previous versions.
The action bar itself is themed by setting ?attr/actionBarTheme (themes_base.xml) and is set to #style/ThemeOverlay.AppCompat.ActionBar by default. On Holo, this theme overrides the actionBarItemBackground, so you'll need to make your changes here.
So, the easy way to override the action item background for all action bars would be to set actionBarItemBackground in your actionBarTheme. You'll probably also want to override selectableItemBackground since the CloseMode item doesn't use actionBarItemBackground (no idea why).
values/styles.xml:
<style name="MyAppTheme" parent="Theme.AppCompat">
...
<item name="actionBarTheme">#style/MyActionBarTheme</item>
</style>
<style name="MyActionBarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
...
<item name="actionBarItemBackground">#drawable/whatever_you_want</item>
<item name="selectableItemBackground">#drawable/whatever_you_want</item>
</style>
Note: These changes apply to all API levels, so if you still want ripples on API 21+, you'll want both drawable and drawable-v21 versions of #drawable/whatever_you_want, the latter of which incorporates ripples.
for me this worked:
<!-- pre 21 -->
<item name="actionBarItemBackground">#drawable/selector_ab_tab_indicator</item>
<!-- post 21 -->
<item name="android:selectableItemBackgroundBorderless">#drawable/selector_ab_tab_indicator</item>