StatusBar text color doesn't changed - android

I am changing status bar background color with this code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(getColorByResourceId(this, android.R.color.white));
}
}
#ColorInt
public int getColorByResourceId(#NonNull Context context, #ColorRes int resourceId) {
return ResourcesCompat.getColor(context.getResources(), resourceId, null);
}
}
Background changed well, but text color doesn't changed (it white too):
I am using Theme.AppCompat.Light.NoActionBar. How to make status bar text color dark?

You can not set a specific textColor to the statusBar. But you can make it visible on your statusBar background for API >= 23
As your statusBar is White, you can add the below attribute in your Base Application Theme or your custom statusBar theme:
<item name="android:windowLightStatusBar">true</item>
It will make texts slightly grayish which will make them visible on your white background.

Related

Android navigation bar icons color

I tried to color my bottom navigation bar
In some android versions it's look white background with white icons
protected void updateNavigationBarColor(Window window, boolean isLight) {
if (window == null) {
return;
}
View decorView = window.getDecorView();
if (decorView == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
window.setNavigationBarColor(Utils.getColor(R.color.navigation_bar_color));//black or white
WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(window, decorView);
controller.setAppearanceLightNavigationBars(isLight);
} else {
// Do nothing
}
}
The navigation bar background color is exactly what I ask for. but the 3 buttons (icons) are stay in always light
In themes.xml added to my theme
<item name="android:navigationBarColor">#color/transparent</item>
<item name="android:windowLightNavigationBar">true</item>

Change color and theme of fab button

I used custom style resource and used the following code to change the theme which changes action bar and status bar color but fab button theme doesn't change.
getTheme().applyStyle(switchValue? R.style.AppTheme1:R.style.AppTheme2 , true);
The color of Floating action button is defaulted to colorAccent you can change this from code.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fab.setBackgroundTintList(ColorStateList
.valueOf(getResources()
.getColor(colorsArray[themeId],getTheme())));
}
the valueOf method in the above code takes color of type int
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fab.setBackgroundTintList(ColorStateList
.valueOf(YourColorValue)));
}

Changing the color of the background of an Activity

I am trying to change the color of the background in my android app. At the moment when I press the button it changes the color for that specific activity and not for all the other activities. Is there a way to change it for the whole app?
public class colorPicker extends AppCompatActivity {
View view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_picker);
view = this.getWindow().getDecorView();
}
public void goPink(View v){
view.setBackgroundResource(R.color.Pink);
}
public void goGreen(View v){
view.setBackgroundResource(R.color.Green);
}
public void goYellow(View v){
view.setBackgroundResource(R.color.Yellow);
}
}
create a theme in your styles.xml and add following to that theme
<item name="android:windowBackground">#color/window_background
and set android:theme="#style/YourTheme" in your
<application>
...
</application
in manifest file
In this case, You need to add few changes in your activity.
In on onCreate
if(getIntent().getExtras() != null)
{
int theme = getIntent().getIntExtra("theme", R.style.AppTheme);
setTheme(theme);
getApplication().setTheme(theme);
//recreate();
}
Condition onCLick
if(condition)
getIntent().putExtra("theme", R.style.AppTheme2);
else
getIntent().putExtra("theme", R.style.AppTheme);
and maintain 2 theme
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary"></item></style>
and the second theme similar to it just change the name as BaseTheme2.
But this is not suggested to change the app theme at runtime.
You could change the window background color of your app theme and don't use a background for activities or you can use a transparent background for activities.

Set status bar color dynamically in android

How to set status bar color dynamically for an application, am using view pager while swiping (horizontally) status bar color and title bar and button should change the color . as per my code title and button color changing perfectly ,but the issue is status bar color taking next color from array list. how to fix that issue can anyone help me. here is my code
private int[] colors = new int[]{0xffffd200, 0xff37beb7, 0xff00ccff, 0xff8585c1, 0xfff2a03c, 0xff2a80b9, 0xfff15972,
0xffe9776c, 0xff9dcc96,0xff76c069};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = ((Activity) context).getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
int coloring = position % colors.length;
int new_color = colors[coloring];
window.setStatusBarColor(new_color);
title_bar.setBackgroundColor(new_color);
set_share.setBackgroundColor(new_color);
}
else{
int color = position % colors.length;
itemView.setBackgroundColor(colors[color]);
title_bar.setBackgroundColor(colors[color]);
set_share.setBackgroundColor(colors[color]);
}
To change status bar color use setStatusBarColor(int color). According the javadoc, we also need set some flags on the window.
Working snippet of code:
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.example_color));
This is taken from following reference:
How to change status bar color to match app in Lollipop? [Android]
coming to Status bar color, you can only add it to devices with API level more than 21. To those devices which satisfy this condition you can change the StatusBar color dynamically as shown below.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.Statusbar));
}
When I wanted to set the status bar color, I used https://github.com/jgilfelt/SystemBarTint
I used it like that :
public static void colorStatusBar(Window window, Activity activity) {
Log.v(Constants.LOG_TAG, "Start defining color bar status");
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
SystemBarTintManager tintManager = new SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(true);
tintManager.setTintColor(activity.getResources().getColor(R.color.colorPrimaryDark));
}
}
But beware that setting status bar color is only possible if your app runs on a phone with API >= 19

Android Material: Status bar color won't change

I'm developing an simple app to test the material design. I'm using com.android.support:appcompat-v7:21.0.0 and my activity looks like:
public class MyActivity extends ActionBarActivity {
...
}
The layout is defined as:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="128dp"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"/>
</LinearLayout>
Now i defined my theme following material guidelines:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary500</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark700</item>
</style>
I'd like to change the status bar color in pre Android 5 and set it to colorPrimaryDark but i can't find the way. I tried using:
getWindow().setStatusBarColor(..)
but setStatusBar color is available from level 21.
Why if i define a colorPrimaryDark in my theme and use appcompact the status bar doesn't change color?
Anyone can help?
While colouring the status bar is not supported <5.0, on 4.4 you can use a work around to achieve a darker colour:
Make the status bar translucent
<item name="android:windowTranslucentStatus">true</item>
Then use AppCompat's Toolbar for your appbar, making sure that it fits system windows:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
...
android:fitsSystemWindows="true"/>
Make sure to set your toolbar as your activity's toolbar:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
The toolbar stretches underneath the status bar, and the semi translucency of the status bar makes it appear to be a darker secondary colour. If that's not the colour you want, this combination allows you to fit a view underneath your status bar sporting the background colour of your choice (though it's still tinted darker by the status bar).
Kind of an edge case workaround due to 4.4 only, but there ya go.
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.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.actionbar));
}
Put this code in your Activity's onCreate method. This helped me.
As others have also mentioned, this can be readily solved by adding the following to the onCreate() of the Activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.primary_dark));
}
However, the important point I want to add here is that in some cases, even the above does not change the status bar color. For example, when using MikePenz library for Navigation Drawer, it implicityly overrides the status bar color, so that you need to manually add the following for it to work:
.withStatusBarColorRes(R.color.status_bar_color)
Status bar coloring is not supported in AppCompat v7:21.0.0.
From the Android developers blog post
On older platforms, AppCompat emulates the color theming where possible. At the moment this is limited to coloring the action bar and some widgets.
This means the AppCompat lib will only color status bars on Lollipop and above.
Switch to AppCompatActivity and add a 25 dp paddingTop on the toolbar and turn on
<item name="android:windowTranslucentStatus">true</item>
Then, the will toolbar go up top the top
This solution sets the statusbar color of Lollipop, Kitkat and some pre Lollipop devices (Samsung and Sony).
The SystemBarTintManager is managing the Kitkat devices ;)
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hackStatusBarColor(this, R.color.primary_dark);
}
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
public static View hackStatusBarColor( final Activity act, final int colorResID ) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
try {
if (act.getWindow() != null) {
final ViewGroup vg = (ViewGroup) act.getWindow().getDecorView();
if (vg.getParent() == null && applyColoredStatusBar(act, colorResID)) {
final View statusBar = new View(act);
vg.post(new Runnable() {
#Override
public void run() {
int statusBarHeight = (int) Math.ceil(25 * vg.getContext().getResources().getDisplayMetrics().density);
statusBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, statusBarHeight));
statusBar.setBackgroundColor(act.getResources().getColor(colorResID));
statusBar.setId(13371337);
vg.addView(statusBar, 0);
}
});
return statusBar;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
else if (act.getWindow() != null) {
act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
act.getWindow().setStatusBarColor(act.getResources().getColor(colorResID));
}
return null;
}
private static boolean applyColoredStatusBar( Activity act, int colorResID ) {
final Window window = act.getWindow();
final int flag;
if (window != null) {
View decor = window.getDecorView();
if (decor != null) {
flag = resolveTransparentStatusBarFlag(act);
if (flag != 0) {
decor.setSystemUiVisibility(flag);
return true;
}
else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
act.findViewById(android.R.id.content).setFitsSystemWindows(false);
setTranslucentStatus(window, true);
final SystemBarTintManager tintManager = new SystemBarTintManager(act);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintColor(colorResID);
}
}
}
return false;
}
public static int resolveTransparentStatusBarFlag( Context ctx ) {
String[] libs = ctx.getPackageManager().getSystemSharedLibraryNames();
String reflect = null;
if (libs == null)
return 0;
final String SAMSUNG = "touchwiz";
final String SONY = "com.sonyericsson.navigationbar";
for (String lib : libs) {
if (lib.equals(SAMSUNG)) {
reflect = "SYSTEM_UI_FLAG_TRANSPARENT_BACKGROUND";
}
else if (lib.startsWith(SONY)) {
reflect = "SYSTEM_UI_FLAG_TRANSPARENT";
}
}
if (reflect == null)
return 0;
try {
Field field = View.class.getField(reflect);
if (field.getType() == Integer.TYPE) {
return field.getInt(null);
}
} catch (Exception e) {
}
return 0;
}
#TargetApi(Build.VERSION_CODES.KITKAT)
public static void setTranslucentStatus( Window win, boolean on ) {
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
}
else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
I know this doesn't answer the question, but with Material Design (API 21+) we can change the color of the status bar by adding this line in the theme declaration in styles.xml:
<!-- MAIN THEME -->
<style name="AppTheme" parent="#android:style/Theme.Material.Light">
<item name="android:actionBarStyle">#style/actionBarCustomization</item>
<item name="android:spinnerDropDownItemStyle">#style/mySpinnerDropDownItemStyle</item>
<item name="android:spinnerItemStyle">#style/mySpinnerItemStyle</item>
<item name="android:colorButtonNormal">#color/myDarkBlue</item>
<item name="android:statusBarColor">#color/black</item>
</style>
Notice the android:statusBarColor, where we can define the color, otherwise the default is used.
Make Theme.AppCompat style parent
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:colorPrimary">#005555</item>
<item name="android:colorPrimaryDark">#003333</item>
</style>
And put getSupportActionBar().getThemedContext() in onCreate().
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
getSupportActionBar().getThemedContext();
}
**
Solution for naughty LOLLIPOP
**
hello i had rly big problem with having that darker than was my color... so this is solution to avoid that shadow behind status bar from solution with TranslucentStatus...
so in all of your java activity you need this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
and at your styles.xml you need to set background color (this color will be your status bar color):
<style name="AppCompat" parent="Theme.AppCompat">
<item name="android:colorBackground">#color/YOUR_STATUS_BAR_COLOR</item>
</style>
and at all your layouts you need to add layout which will be your background:
<LinearLayout
android:background="#color/YOUR_BACKGROUND_COLOR"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
/>
of course if you would like use #color/just name... you need to set that at colors.xml:
<color name="YOUR_STATUS_BAR_COLOR">#cf031c</color>
<color name="YOUR_BACKGROUND_COLOR">#383838</color>
here is whole process how we did done that: Whats the right approach to create or change color of a status bar?

Categories

Resources