-3

I would like to create a new activity that includes more buttons. Each button represents a color. How can I change the action bar (and save) if I click on a button that represents a color (red for example)?

I did something that changes the color of the action bar, but if I go on the home page, the color will change again. Do I need to serialize something? Thanks!

pink.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
    getSupportActionBar().setTitle("Pink");
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPink)));
    if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP){
        Window window =getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.colorAccent));

    }
}

});

Button xml.

   <Button android:id="@+id/button_pink" android:layout_width="296dp" 
    android:layout_height="49dp" android:layout_marginTop="26dp" 
    android:background="@color/colorPink" android:text="pink" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" />
   </android.support.constraint.ConstraintLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

1

Read the documentation first. As written in the documentation.

<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.

farhan
  • 458
  • 2
  • 8
  • 24
0

This is happening because when you restart your activity then according to activity life cycle onReStart() and then onStart() is called so the action bar color becomes default. As in your code you are setting action bar color on a click event.

Possible solution is that in your button click event you can save the color value in a shared preference and then use that shared preference in your onCreate() with the color changing code. This will make your app to set the color that is saved previously.

Android Lifecycle

Shared Preference

onCreate(){
sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
int color = sharedPreferences.getInt("color", "DEFAULT_COLOR");
String title = sharedPreferences.getString("text", "DEFAULT_TEXT");
getSupportActionBar().setTitle(title);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(USE_THE_COLOR_VALUE_HERE)));//
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP){
  Window window =getWindow();   
  window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  window.setStatusBarColor(getResources().getColor(R.color.colorAccent));

    }
}

And in your button click store the color and title

onclick(){
SharedPreferences.Editor editor = sharedPreferences.edit();
            editor = sharedPreferences.edit();
            editor.putString("text","YOUR_TEXT");
            editor.putInt("color","YOUR_COLOR");
            editor.commit();
}

If you still face any problem in saving your color in shared preference you can see

How to store color value in SharedMemory?

Community
  • 1
  • 1
Swarnveer
  • 490
  • 5
  • 23
  • Thanks a lot! Works, but I would like to change the color of all action bars from the project. Have you an ideea how can I do this? (home page, settings etc.). Thank you – Vasilescu Catalin May 14 '17 at 22:52
  • You can paas the color value and title as a intent using intent.putextra() and get the intent value on next activity. So suppose if you want to go to settings from home then in home activity start a intent to settings and use intent.putextra() and send the color value and title text through it and on settings activity onCreate() you can use getIntent().getExtra – Swarnveer May 14 '17 at 23:06
  • See the post here to know how intent.putExtra() works. http://stackoverflow.com/questions/4233873/how-do-i-get-extra-data-from-intent-on-android – Swarnveer May 14 '17 at 23:09
0

onClick button color changer

int color = R.color.colorPink;

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(color)));

And save onClick

SharedPreferences preferences = getSharedPreferences ("prefKey", MODE_PRIVATE);

SharedPreferences.Editor editor = preferences.edit();

editor.putInt ("colorValue", color);

editor.apply ();

onCreate

SharedPreferences preferences = getSharedPreferences ("prefKey", Mode_Private);

int color = preferences.getInt ("colorValue", 0);

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(color)));
  • Thanks a lot! Works, but I would like to change the color of all action bars from the project. Have you an ideea how can I do this? (home page, settings etc.). Thank you! – Vasilescu Catalin May 14 '17 at 22:45