0

I am trying to make a Button change its background color and launch a new activity on click. Below is the code I wrote, but I am getting error message saying to declare 'Button btn1' as 'final Button btn1'. If I do that, the button change its color on click and launch another activity as I wished, but if returning to Main activity from the new activity, the once changed background color stays permanently. How can I improve my code? Thank you.

public class Home extends ActionBarActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_home);

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

btn1.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {

   btn1.setBackgroundColor(Color.GRAY);

   Intent intent = new Intent(v.getContext(), activity_street.class);
   startActivityForResult(intent, 0);
}
});
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
denise
  • 29
  • 1
  • 4

1 Answers1

2

No need to make btn1 final just use v parameter of onClick method to change color of clicked Button background :

v.setBackgroundColor(Color.GRAY);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213