ADAM9611222
ADAM9611222

Reputation: 11

Click Button and change background color of another button

I am making an app in Android Studio. On one of my activities, there are a bunch of buttons, and when you click one, a PopupWindow class appears which has 4 different coloured buttons on it.

What I am having trouble with: Once the PopupWindow appears, I want the user to select one of the 4 buttons and based on the one colour they chose, the original button that was first clicked on the activity will change its background colour to the one selected in the Popup.

POP UP CODE:

public class Pop extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.popupwindow);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int)(width*.7), (int)(height*.7));

   final Button color1Button = (Button) findViewById(R.id.redbutton);
    color1Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    }
}

MAIN ACTIVITY CODE:

Button a = (Button) findViewById(R.id.button);

    a.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            startActivity(new Intent(LambertsLaneSection1Activity.this, Pop.class));

        }
    });
    Button b = (Button) findViewById(R.id.button3);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LambertsLaneSection1Activity.this, Pop.class));


        }
    });
    Button c = (Button) findViewById(R.id.button4);
    c.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LambertsLaneSection1Activity.this, Pop.class));


        }
    });

Upvotes: 1

Views: 581

Answers (1)

Andrea Dusza
Andrea Dusza

Reputation: 2110

If I understand your goal correctly, one possible way to accomplish this is by using SharedPreferences and Extras.

A Shared Preferences file is a file stored in the device's file system locally. It allows you to store information as key-value pairs, and the information remains there even when you exit your application. It is mainly used for storing your application-specific settings. This way, you can permanently save the colors for each button selected by the user.

Extras are for transporting information between Activites when you start an Activity from another using an Intent.

You need to add this to your MainActivity's onCreate():

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences sharedPref = getSharedPreferences(
                "MY_PREFS", Context.MODE_PRIVATE);

        ...
}

Code for button initializing and handling press (e.g button2):

//-1 is a default value in case no color was selected yet for the particular button
int button2Color = sharedPref.getInt("button2Color", -1);
Button button2 = (Button) findViewById(R.id.button2);
if (button2Color != -1) {
      button2.setBackgroundColor(button2Color);
}
button2.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
           Intent i = new Intent(LambertsLaneSection1Activity.this, Pop.class);
           //2 here identifies your button, because it is button2
           i.putExtra("buttonPressed",2)
           startActivity(i);
     }
});

In your PopUp:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.popupwindow);

    //here you get which button was pressed as an Extra
    int buttonPressed = getIntent().getExtras().getInt("buttonPressed");
    SharedPreferences sharedPref = getSharedPreferences(
                "MY_PREFS", Context.MODE_PRIVATE);
    ...

    final Button color1Button = (Button) findViewById(R.id.redbutton);
    color1Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             sharedPref.edit().putInt("button"+buttonPressed+"Color", Color.RED).apply();
        }
    }
}

Upvotes: 1

Related Questions