views:

78

answers:

0

I want to have a button on my main screen (button1). When button1 is pressed, it takes me to a 2nd screen with multiple buttons. (this part is done). When a user presses a button on that screen, it goes back to the main screen and changes that button to the same image as the button they pressed.

Example

MAIN -speed_ability_button (set as speed_any - dynamic)

Screen 2 -speed_any_button (set as speed_any - static) -speed_boot (set as speed_boot - static)

If speed_boot is pressed, speed_ability_button will now be set as speed_boot

MAIN.JAVA

public static final int SPEED_ABILITY = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageButton speed_ability_button = (ImageButton)findViewById(R.id.speed_ability_button);
  speed_ability_button.setBackgroundDrawable(getResources().getDrawable(R.drawable.speed_any));
  speed_ability_button.setOnClickListener(new View.OnClickListener(){
    public void onClick(View view){
      Intent get_speed_ability = new Intent(view.getContext(), Speed_ability.class);
      startActivityForResult (get_speed_ability, SPEED_ABILITY);
    }
  });

  protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch(requestCode) {
      case SPEED_ABILITY:
      String speeda = data.getStringExtra("speeda_choice");
      if (resultCode == RESULT_OK && speeda == "any"){
        ImageButton speed_ability_button = (ImageButton)findViewById(R.id.speed_ability_button);
        speed_ability_button.setBackgroundDrawable(getResources().getDrawable(R.drawable.speed_any));
      }
      if (resultCode == RESULT_OK && speeda == "boot"){
        ImageButton speed_ability_button = (ImageButton)findViewById(R.id.speed_ability_button);
        speed_ability_button.setBackgroundDrawable(getResources().getDrawable(R.drawable.speed_boot));
      }
    }
  }
}

Speed_ability.JAVA

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.speed_ability);
  ImageButton speed_any = (ImageButton) findViewById(R.id.speed_any_selection);
  speed_any.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      Intent set_as_any_speed = new Intent();
      String speeda_choice = "any";
      set_as_any_speed.putExtra("speeda_choice", speeda_choice);
      setResult(RESULT_OK, set_as_any_speed);
      finish();
    }
  });
  ImageButton speed_boot = (ImageButton) findViewById(R.id.speed_boot_selection);
  speed_boot.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      Intent set_as_boot_speed = new Intent();
      String speeda_choice = "boot";
      set_as_boot_speed.putExtra("speeda_choice", speeda_choice);
      setResult(RESULT_OK, set_as_boot_speed);
      finish();
    }
  });
}

}