I'm searching for a way to find out how to change the color of a button in android by using java code when the button is clicked but not released,I would be glad to be helped out.
Asked
Active
Viewed 151 times
3 Answers
2
button.setOnTouchListener(new View.OnTouchListener() {
public void onTouch(View view, MotionEvent e) {
switch(e.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(" mouse pressed ");
break;
case MotionEvent.ACTION_UP:
System.out.println(" mouse released");
break;
}
}
});
you can use MotionEvent.ACTION_DOWN case to handle your button color change logic.
hope this will be help to you
if you got it inform me :)

MeshBoy
- 662
- 5
- 9
0
Check out the documentation at Android Documentation. It shows that button has different states and you can align color on each state. Also check this link

Community
- 1
- 1

user2430771
- 1,326
- 4
- 17
- 33
0
You can use the selector for that like below code
Create selet.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg_hover" />
</selector>
Now set the background of button like below
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector" />

Shadik Khan
- 1,217
- 1
- 8
- 18