I am trying to change toolbar color from different class, but always failed. I don't know, what's wrong?
I've tried to do this using LayoutInflater
but still failed. Can you help me to solve this issue?
LoadColor.java
public class LoadColor {
private Context context;
private HomeActivity hA;
final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX";
public LoadColor(Context context) {
this.context = context;
}
public void LoadPreferences(){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_settings, null,false);
LayoutInflater tiup = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View homeAct = tiup.inflate(R.layout.activity_home, null,false);
Toolbar tb = (Toolbar) homeAct.findViewById(R.id.toolbarHome);
RadioGroup radioGroup = (RadioGroup) contentView.findViewById(R.id.radioSex);
SharedPreferences sharedPreferences = context.getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
int savedRadioIndex = sharedPreferences.getInt(KEY_SAVED_RADIO_BUTTON_INDEX, 0);
RadioButton savedCheckedRadioButton = (RadioButton) radioGroup.getChildAt(savedRadioIndex);
savedCheckedRadioButton.setChecked(true);
RadioGroup genderGroup = (RadioGroup) contentView.findViewById(R.id.radioSex);
RadioButton male = (RadioButton) contentView.findViewById(R.id.theme1);
RadioButton female = (RadioButton) contentView.findViewById(R.id.theme2);
if (genderGroup.getCheckedRadioButtonId() == -1) {
hA = new HomeActivity();
hA.setToolbarColor(tb, context.getResources().getColor(R.color.colorPrimary));
}
else {
if (male.isChecked()) { // one of the radio buttons is checked
hA = new HomeActivity();
hA.setToolbarColor(tb, context.getResources().getColor(R.color.colorPrimary));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
((Activity) context).getWindow().setStatusBarColor(Color.parseColor("#014a53"));
}
}
else if (female.isChecked()) {
hA = new HomeActivity();
hA.setToolbarColor(tb, context.getResources().getColor(R.color.colorAccent));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
((Activity) context).getWindow().setStatusBarColor(Color.parseColor("#db503d"));
}
}
}
}
}
activity_home.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarHome"
android:layout_width="match_parent"
android:layout_height="64dp"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</android.support.design.widget.AppBarLayout>
HomeActivity.java
private LoadColor Lc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//toolbar logo and desc
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbarHome);
setSupportActionBar(topToolBar); //munculkan menu ke toolbar
topToolBar.setLogo(R.mipmap.ikon);
topToolBar.setLogoDescription(getResources().getString(R.string.logo_desc));
Lc = new LoadColor(this);
Lc.LoadPreferences();
} //OnCreate
public static void setToolbarColor(Toolbar toolbar, @ColorInt int color) {
toolbar.setBackgroundColor(color);
}
You can see the setStatusBarColor
code in LoadColor.java
it's work, but in the toolbar setBackgroundColor
it doesn't work.
Inside your onCreate or wherever you want to change the toolbar color you may call this static utility function and pass it the reference of the toolbar for which you want to change the background. This of course happens after you have identified which color you want to use for the background.
//Tools.java
public static void setToolbarColor(Toolbar toolbar, @ColorInt int color) {
toolbar.setBackgroundColor(color);
}
For example:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//toolbar logo and desc
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbarHome);
setSupportActionBar(topToolBar); //munculkan menu ke toolbar
topToolBar.setLogo(R.mipmap.ikon);
topToolBar.setLogoDescription(getResources().getString(R.string.logo_desc));
//determine which color you want to use for the toolbar's background here
//you may use a local method to do that and return the resource value
//it can be an int resource or it can simply be a stored resource.
Tools.setToolbarColor(toolbar,getResources().getColor(R.color.colorPrimary));
//you can also parse the color from a string
setToolbarColor(topToolBar, Color.parse("RED"));
} //OnCreate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.