Change color and theme of fab button - android

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)));
}

Related

Change StatusBar color and show ActionMode at the same time

I change status bar color whenever action mode is shown using this piece of code:
override fun onActionModeStarted(mode: ActionMode?) {
super.onActionModeStarted(mode)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window?.statusBarColor = colorBlue
}
}
The problem is action mode shows up with an animation while window.statusBarColo = changes status bar color immediately and as a result status bar color is changed a little bit sooner than action mode being displayed and it is so unpleasant to see. Do you guys have any solution for this?

Status bar and toolbar equal to the Android chat

How do I make the toolbar the same as Android chat? That is white and also the status bar.
you can try ` if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow()
.setStatusBarColor(
ContextCompat.getColor(activity, color)
);
}`

StatusBar text color doesn't changed

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.

How to set light status bar for Android N

I use this way to set light status bar for Android M, and it works well:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
But, it does not work at Android N. The words in status bar are still white. Is it a bug? Or is there any way to set light status bar at Android N?
Well, I found the question.It did works to set light status bar mode.But after that, i changed the status bar's color, called setSystemUiVisibility() again and set other value.So the light status bar is not valid.
Just call this method from any activity and pass color code too
public static void makeStatusBarLight(Activity activity, int color) {
Window window = activity.getWindow();
window.addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(color);
}
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
call like this
makeStatusBarLight(BaseActivity.this, getResources().getColor(R.color.yourcolor));

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

Categories

Resources