There is a standard button:

<Button android:id="@+id/button_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:alpha="1" android:text="Button"/> 

When you hold the button, it programmatically changes the color. When squeezing should return to the original color:

 @Override public boolean onTouch(View v, MotionEvent event) { Button button1 = (Button) findViewById(R.id.button_1); if (event.getAction() == MotionEvent.ACTION_DOWN ){ button1.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN); return true; } else if (event.getAction() == MotionEvent.ACTION_UP) { button1.getBackground().setColorFilter(Color.parseColor("Как определить первоначальный цвет?"), PorterDuff.Mode.DARKEN); return true; } return false; } 

Where to see the default button color?

  • @RealKEK, more details please - Mr_FFFFFF
  • see the answer ._____ - Real KEK
  • one
    In general, for such things there is a StateList (xml <selector> ) , which allows a minimum of code (creating one xml file) to specify actions for clicks, selections and other actions on widgets, why write your crutch. - pavlofff

5 answers 5

You can get the background color of the button in Hex format like this:

 Button btn = (Button) findViewById(R.id.button); ColorDrawable btnBackgroundColor = (ColorDrawable) btn.getBackground(); //далее приводим цвет к `Hex` формату String hexColor = String.format("#%06X", 0xFFFFFF & btnBackgroundColor.getColor()); 

If you make a color change in your way using MotionEvent then the code will be as follows for this example:

 @Override public boolean onTouch(View v, MotionEvent event) { Button button1 = (Button) findViewById(R.id.button_1); ColorDrawable buttonColor = (ColorDrawable) button1.getBackground(); String firstColor = String.format("#%06X", 0xFFFFFF & buttonColor.getColor()); if (event.getAction() == MotionEvent.ACTION_DOWN) { button1.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN); return true; } else if (event.getAction() == MotionEvent.ACTION_UP) { button1.getBackground().setColorFilter(Color.parseColor(firstColor), PorterDuff.Mode.DARKEN); return true; } return false; } 

    Take the background from the button, bring it to ColorDrawable , and save this color in the field.

    In short, here:

     Button button = (Button) findViewById(R.id.my_button); ColorDrawable colorDrawable = (ColorDrawable) button.getBackground(); int colorId = colorDrawable.getColor(); 

    And you should get this code:

     int colorId; @Override public boolean onTouch(View v, MotionEvent event) { Button button1 = (Button) findViewById(R.id.button_1); if (event.getAction() == MotionEvent.ACTION_DOWN ) { ColorDrawable colorDrawable = (ColorDrawable) button.getBackground(); colorId = colorDrawable.getColor(); button1.getBackground().setColorFilter( Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN); return true; } else if (event.getAction() == MotionEvent.ACTION_UP) { button1.getBackground().setColorFilter( colorId, PorterDuff.Mode.DARKEN); return true; } return false; } 

    And I strongly advise you not to put bulky findViewById() in such methods as onTouch() - it can slow down.

    • And for what the string Drawable buttonBackground = button1.getBackground(); - V.March
    • Probably the idea was to render the Drawable buttonBackground = button1.getBackground(); before if . And in the future, use buttonBackground instead of button1.getBackground() ? - V.March
    • This is a good answer, but there are errors in the code that you published. - V.March
    • one
      Thanks, corrected. I can not imagine how I could write such nonsense - Real KEK
    • It happens to everyone without exception)) Now I can mark your answer as useful. After all, a good answer will help someone to save a couple of hours and maybe days! - V.March

    The default color will be what you assign in the xml file. For example:

     android:background="#4ade3d" 
    • 2
      And what will be the color if the background is not assigned? - Mr_FFFFFF pm

    Use the following:

     button1.setBackgroundResource(android.R.drawable.btn_default); 
    • btn_default is different from the original button. btn_default with a dark frame, and the default button without it. Picture for illustration: ru.files.fm/u/hxh9exwa - Mr_FFFFFF

    I solved the problem simply: I created my xml button in a drawable, set its original color and used it in the future.