I have created a Button b with its background colour as black. When i click on it I want its colour to change to green only for the time I have my finger on it, i.e. I keep focus on it.
            Asked
            
        
        
            Active
            
        
            Viewed 6,063 times
        
    3
            
            
         
    
    
        Diptangsu Goswami
        
- 5,554
- 3
- 25
- 36
- 
                    Please provide the work you have done, and show us where you are having problems. – Flummox - don't be evil SE May 27 '16 at 07:52
- 
                    See this: http://stackoverflow.com/a/1726352/5250273 – Ugurcan Yildirim May 27 '16 at 07:53
- 
                    1here's the same question answered clearly http://stackoverflow.com/questions/3882064/how-to-change-color-of-button-in-android-when-clicked – JhesterMag May 27 '16 at 08:01
4 Answers
5
            Use a selector in your button.
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:gravity="center" android:focusable="true"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:background="@android:drawable/list_selector_background" />
here is code for list_selector_background XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/green"/> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
    <item android:drawable="@color/black"/> <!-- default -->
</selector>
 
    
    
        Lips_coder
        
- 686
- 1
- 5
- 17
2
            
            
        You need to use onTouchListener to listen when user presses the button (ACTION_DOWN) and when the user releases it (ACTION_UP)
b.setOnTouchListener(new OnTouchListener() { 
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
       // reset the background color here
        b.setBackgroundColor(Color.GREEN);
    }else{ 
       // Change the background color here 
        b.setBackgroundColor(Color.RED);
    } 
    return false; 
   } 
}); 
 
    
    
        Dave Ranjan
        
- 2,966
- 24
- 55
- 
                    Instead of registering touch event like this, we can make selector drawable using multiple colors/drawables for different states. – seema May 27 '16 at 09:43
0
            
            
        ACTION_UP --> A pressed gesture has finished, the motion contains the final release location as well as any intermediate points since the last down or move event.
ACTION_DOWN --> A pressed gesture has started, the motion contains the initial starting location.
You should set an OnTouchListener on your button.
button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            btn.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            btn.setBackgroundColor(getResources().getColor(R.color.white));
        }
    }
};
check motion events
 
    
    
        Amit Vaghela
        
- 22,772
- 22
- 86
- 142
- 
                    as getColor() is deprecated you can use, btn.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));@Diptangsu Goswami – Amit Vaghela May 27 '16 at 08:10
0
            
            
        make this xml drawable and use as background of button.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/green"/> 
<item android:drawable="@color/black"/>
</selector>
 
    
    
        seema
        
- 991
- 1
- 13
- 27