Tuesday, June 30, 2015

// //

Change color of Button in Android when Clicked

This is just a very simple illustration on how to change the button color when tap :
1

Button color change

2.
button color change


put this xml into your values folder
 Colors.xml

    #d73249
    #ffcb00
     #fff

MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

 
 private View start;
 private View stop;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  stop = findViewById(R.id.start);
  start = findViewById(R.id.stop);
 }

 public void start(View view) {

  stop.setBackgroundResource(R.color.red);
   start.setBackgroundResource(R.color.yellow);
 }

 public void stop(View view) {
  stop.setBackgroundResource(R.color.yellow);
   start.setBackgroundResource(R.color.red);
 }

 
}
activity_main.xml


    
You may also like: Style a button in android