How to change Action Bar and Status Bar color in activity?
Solution 1
if I understand correctly you want to change from java code .. When you call an if try to use this method:
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeColor(R.color.red);
}
public void changeColor(int resourseColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(ContextCompat.getColor(getApplicationContext(), resourseColor));
}
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(resourseColor)));
}
}
Solution 2
You can do it by defining the custom theme for activity. First you need to create styles.xml
file in res/values/
path.
styles.xml
<resources>
<style name="MyCustomTheme" parent="Theme.AppCompat.Light">
<item name="android:statusBarColor">@color/color_primary</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@color/color_primary</item>
</style>
</resources>
Then in your AndroidManifest.xml
write:
<activity
android:name=".MyActivity"
android:theme="@style/MyCustomTheme"/>
Or in your activity:
setTheme(android.R.style.MyCustomTheme);
Dumbo
Work: IT Solutions Developer at SEB, Lithuania. Mainly working with hibernate. Creating web services to connect server with ambulance portable radio. Also writing postgreSQL database scripts and supporting developers who are working with android applications.
Updated on June 15, 2022Comments
-
Dumbo almost 2 years
I want to change the color of my action bar and status bar programmatically only on one activity, depending it's state, green for go and red for stop. I want to keep the system wide theme of orange and only have the red or green depending on the outcome of whats inside the activity, with an if Statement.
How could it be possible to do that?-
Admin about 6 yearsI also need to change the colour of my status bar
-
-
Admin about 6 yearsNeeds to be done programmatically within the activity not the XML file
-
Dumbo about 6 yearsYou can use
setTheme(android.R.style.MyCustomTheme)
method after yourif()
statement. @zarrexx -
Admin about 6 yearsWhat do I call "red"? can I call it as a variable named "#FF0000
-
Dumbo about 6 years
R.color.red
is defined color name inres/values/colors.xml
. Example:<color name="red">#770000</color>
@zarrexx -
Dumbo about 6 years@MattDeveloper How to do that in lover version than API 21?
-
Admin about 6 yearsno but red is undefined
-
Admin about 6 yearsHow can i set it within the java rather than xml?
-
Admin about 6 years
changeColor(Color.parseColor("#00FF00"));
causes my activity to crash -
Dumbo about 6 years@zarrexx if you want to use class variable with "#FF0000" value. You can use
Color.parseColor(stringVariable);
instead ofContextCompat.getColor(getApplicationContext(), resourseColor)
andgetResources().getColor(resourseColor)
. -
Admin about 6 yearsCould you show how I could adapt the
changeColor
method to usechangeColor(Color.parseColor("#00FF00"));
-
Dumbo about 6 years@zarrexx look there: dropbox.com/s/4w2kqnaak8149wx/answer_.txt?dl=0
-
Admin about 6 yearsThank you very much