I have been able to change the background color of the taskbar that is in my App.
The idea is to change this color dynamically, depending on the value of the variable colorTiempo
, as you can see in this code:
private void setBarColor(int colorTiempo) {
switch (colorTiempo) {
case 1:
getSupportActionBar()
.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_toolbar_adviento)));
break;
case 2:
getSupportActionBar()
.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_toolbar_navidad)));
break;
case 3:
getSupportActionBar()
.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_toolbar_cuaresma)));
break;
case 6:
getSupportActionBar()
.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_toolbar_santos_blanco)));
break;
case 92:
getSupportActionBar()
.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_toolbar_santos_rojo)));
break;
default:
break;
}
}
This is an example:
The toolbar is in yellow, but it would change to another color if the value of colorTiempo
were different. That works perfectly with respect to the toolbar.
But at the top, where the time is, it's in blue.
Could I change the blue part to another custom color through code, like I do with the toolbar?
What method would I have to use to set the color of that bar?
One way to change the status bar can be done by changing the color
colorPrimary
it assigns to your theme:As an example I have an application in which I want to change the color of the status bar to green, I modify this color inside
colors.xml
:Dynamically you can do it this way from API 21, clearly defining the desired color inside
colors.xml
:To have as a result:
Hello, you can change it dynamically with the following code:
It will work as long as it is Android version Lollipop or higher. I leave you the reference of where I found it on the site in English: https://stackoverflow.com/a/26749343
Cheers!