I want my image to change the image as long as i press and hold on to the button . And when i release my finer i want it to revert back to the original image , is there a way to do this ?
bTen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bTen.setBackgroundResource(R.drawable.press);
s1+="E";
txtView.setText(s1);
}
});
i have used the above code ,but it does not revert back to original when i release my finger.
You should create your own selector and add it as style for your button (in xml) example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled -->
<item android:drawable="@color/..." android:state_enabled="false"/>
<!-- pressed -->
<item android:drawable="@color/..." android:state_pressed="true"/>
<!-- default -->
<item android:drawable="@color/..."></item>
</selector>
Create a xml in your drawable folder with name of image.xml with this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="@drawable/login" /> <!-- default -->
</selector>
Then use this image.xml in your layout xml as
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/image"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.