Dynamic Action Bar and Status bar Color

Action bar color dynamically. Sometimes it is necessary to change the Action Bar and status bar color dynamically. This project shows how it can be achieved simply. Status bar color change is supported for lollipop and above devices. So the project works by checking build versions of the device.Only action-bar color is can be changed for devices below lollipop and both action bar and status-bar color are changeable for devices above lollipop, else app crashes. To give an overview activity contain three seek-bars each representing the primary colors Red,Blue and Green. The goal is to create our required color by combining these colors. For the first step is to check the build version of the device. if it’s below lollipop the default color is set, and if it’s above lollipop default status-bar color also applied.
Changes made to the seek-bar are being saved so when you reopen the app the color is the last one you selected. You can modify the code as you like “link to source code is down below”. Now straight to code because it’s simple and nothing more to say about it.
MainActivity.java
public class MainActivity extends AppCompatActivity { SeekBar seekBarRed,seekBarGreen,seekBarBlue; RadioGroup idForRadioGroup; TextView textR,textG,textB; private SharedPreferences sharedPreferences; private SharedPreferences.Editor editor; private boolean bar=true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); idForRadioGroup=(RadioGroup)findViewById(R.id.idForRadioGroup); RadioButton rButton = (RadioButton) idForRadioGroup.getChildAt(0); rButton.setChecked(true); RadioButton rButton2 = (RadioButton) idForRadioGroup.getChildAt(1); rButton2.setVisibility(View.GONE); sharedPreferences = getSharedPreferences("key_clr", Context.MODE_PRIVATE); editor=sharedPreferences.edit(); textR=(TextView)findViewById(R.id.textR); textG=(TextView)findViewById(R.id.textG); textB=(TextView)findViewById(R.id.textB); seekBarRed=(SeekBar)findViewById(R.id.seekBarRed); seekBarGreen=(SeekBar)findViewById(R.id.seekBarGreen); seekBarBlue=(SeekBar)findViewById(R.id.seekBarBlue); getClr(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { rButton2.setVisibility(View.VISIBLE); initStatusBarClr(); } idForRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.radio0: // do operations specific to this selection bar=true; getClr(); break; case R.id.radio1: // do operations specific to this selection bar=false; getClr(); break; } } }); seekBarRed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { textR.setText("= "+String.valueOf(progress)); putClr(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); seekBarGreen.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { textG.setText("= "+String.valueOf(progress)); putClr(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); seekBarBlue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { textB.setText("= "+String.valueOf(progress)); putClr(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } public void ActionBarClr(){ getSupportActionBar().setBackgroundDrawable( new ColorDrawable(Color.rgb(seekBarRed.getProgress() ,seekBarGreen.getProgress(),seekBarBlue.getProgress()))); } public void StatusBarClr(){ Window window = this.getWindow(); // clear FLAG_TRANSLUCENT_STATUS flag: window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // finally change the color if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // window.setStatusBarColor(Color.BLUE); window.setStatusBarColor(Color.rgb(seekBarRed.getProgress() ,seekBarGreen.getProgress(),seekBarBlue.getProgress())); } } public void initStatusBarClr(){ int r=sharedPreferences.getInt("s_r",0); int g=sharedPreferences.getInt("s_g",0); int b=sharedPreferences.getInt("s_b",0); Window window = this.getWindow(); // clear FLAG_TRANSLUCENT_STATUS flag: window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // finally change the color if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // window.setStatusBarColor(Color.BLUE); window.setStatusBarColor(Color.rgb(r,g,b)); } } public void getClr(){ if(bar) { int r=sharedPreferences.getInt("a_r",0); int g=sharedPreferences.getInt("a_g",0); int b=sharedPreferences.getInt("a_b",0); seekBarRed.setProgress(r); seekBarGreen.setProgress(g); seekBarBlue.setProgress(b); textR.setText("= "+String.valueOf(r)); textG.setText("= "+String.valueOf(g)); textB.setText("= "+String.valueOf(b)); ActionBarClr(); }else{ int r=sharedPreferences.getInt("s_r",0); int g=sharedPreferences.getInt("s_g",0); int b=sharedPreferences.getInt("s_b",0); seekBarRed.setProgress(r); seekBarGreen.setProgress(g); seekBarBlue.setProgress(b); textR.setText("= "+String.valueOf(r)); textG.setText("= "+String.valueOf(g)); textB.setText("= "+String.valueOf(b)); StatusBarClr(); } } public void putClr(){ if (bar){ editor.putInt("a_r",seekBarRed.getProgress()); editor.putInt("a_g",seekBarGreen.getProgress()); editor.putInt("a_b",seekBarBlue.getProgress()); editor.commit(); ActionBarClr(); }else{ editor.putInt("s_r",seekBarRed.getProgress()); editor.putInt("s_g",seekBarGreen.getProgress()); editor.putInt("s_b",seekBarBlue.getProgress()); editor.commit(); StatusBarClr(); } } }
Now for the activity xml part
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.logicchip.blog_18_dynamic_action_bar_color.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioGroup android:id="@+id/idForRadioGroup" android:layout_margin="16dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Action bar" android:id="@+id/radio0"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Status Bar" android:id="@+id/radio1"/> </RadioGroup> <LinearLayout android:layout_margin="16dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="R" android:textSize="20sp" android:textStyle="bold" android:layout_weight="1"/> <TextView android:gravity="left" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="= 000" android:id="@+id/textR" android:layout_weight="1"/> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="G" android:textSize="20sp" android:textStyle="bold" android:layout_weight="1"/> <TextView android:gravity="left" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="= 000" android:id="@+id/textG" android:layout_weight="1"/> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="B" android:textSize="20sp" android:textStyle="bold" android:layout_weight="1"/> <TextView android:gravity="left" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="= 000" android:id="@+id/textB" android:layout_weight="1"/> </LinearLayout> <SeekBar android:layout_margin="16dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekBarRed" android:max="255" android:theme="@style/redTheme"/> <SeekBar android:layout_margin="16dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekBarGreen" android:theme="@style/greenTheme" android:max="255"/> <SeekBar android:layout_margin="16dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekBarBlue" android:theme="@style/blueTheme" android:max="255"/> </LinearLayout> </RelativeLayout>