Change actionbar color programmatically more than once
this is a quick fix that i found
mActionBar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);
How to change Action Bar and Status Bar color in activity?
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)));
}
}
Changing the color of a supportActionBar programmatically
...changing the background drawable of the support ActionBar changes the
color of most of the bar but leaves the old color around the text and
icons...
The reason this is happening is because you're using android:theme
instead of style
. theme
gets applied to every child in your Toolbar
, in this case including the TextView
title background and your settings button background. style
is meant to be used at the view level and will only be applied to your Toolbar
background.
Before
<android.support.v7.widget.Toolbar
...
android:theme="@style/LocationBar" />
After
<android.support.v7.widget.Toolbar
...
style="@style/LocationBar" />
Change Action bar background and text colours programmatically using AppCompat
instead of getActionBar()
use getSupportActionBar()
EDIT:
You are getting errors, because your imports are wrong. Please use the same as below. This works just fine.
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
public class TestActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#95CDBA")));
actionBar.setTitle(Html.fromHtml("<font color='#000099'>Hello World</font>"));
}
}
Yes, this is Google's mistake, it should have a different name. SupportActionBar would be great.
If you are unable to fix the imports, you can explicitly specify which one like this
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
How do I change the background color of the ActionBar of an ActionBarActivity using XML?
As per documentation - "You can control the behaviors and visibility of the action bar with the ActionBar APIs, which were added in Android 3.0 (API level 11)."
So, ActionBar will not work for your target environment which is at API level 10 (Android 2.3.3).
Just in case, if you target for minimum API level 11 , you can change ActionBar's background color by defining custom style, as:
<resources>
<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">ANY_HEX_COLOR_CODE</item>
</style>
</resources>
And, set "MyTheme" as theme for application / activity.
Related Topics
Tabhost with Fragments and Fragmentactivity
Open Soft Keyboard Programmatically
Android O - Old Start Foreground Service Still Working
Problems with Gridview Inside Scrollview in Android
Android Studio Gradle Error: Multiple Dex Files Define
How to Exclude Certain Messages by Tag Name Using Android Adb Logcat
How to Change the New Tablayout Indicator Color and Height
Bluetooth and Wifi Printing for Android
Updating the List View When the Adapter Data Changes
Programmatically Obtain the Android API Level of a Device
How to Change the Fontsize in an Android Webview
Ongloballayoutlistener: Deprecation and Compatibility
How to Loop Through Dynamic JSON String Recursively in Android