I want to change imageview's image when user is holding button and change the image back to the default when user releases button.
5 Answers
Use OnFocusChangeListener(view) method & override OnFocusChange() method to this.

- 159
- 5
-
button.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) imageView1.setImageResource(R.drawable.alarm); if (event.getAction() == MotionEvent.ACTION_UP) imageView1.setImageResource(R.drawable.charging_1); return false; } }); – Sourabh Dec 27 '12 at 14:33
You need to use XML Selectors for this. You can specify whatever drawable you want for whatever state you want.
The docs are here: http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
You will create an XML, that has references to each of the states you want to apply styles to, then you will reference when you assign a drawable to a view (a button for instance), instead of a particular drawable.
Check this SO question for more details: Android selector & text color
You can set an OnTouchListener
to the ImageView
and in the OnTouchListener
you can set the image you want to display in MotionEvent.ACTION_DOWN
and revert the old image in MotionEvent.ACTION_UP
.

- 6,770
- 9
- 33
- 62
You can create a custom Button class like this
public class MButton extends Button {
public MButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
// define style
/**
* Send resource ids for normal, selected, pressed in respective order
*
* @param mImageIds
*/
public void setDrawablePressedBg(Integer... mImageIds) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = this.getResources().getDrawable(mImageIds[0]);
Drawable selected = this.getResources().getDrawable(mImageIds[1]);
Drawable pressed = this.getResources().getDrawable(mImageIds[2]);
bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
bg.addState(View.ENABLED_STATE_SET, normal);
bg.addState(View.FOCUSED_STATE_SET, selected);
bg.addState(View.EMPTY_STATE_SET, normal);
this.setBackgroundDrawable(bg);
}
// define style
public void setBackgroundPressedBg(Integer p1, Integer p2, Integer p3) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = this.getResources().getDrawable(p1);
Drawable selected = this.getResources().getDrawable(p2);
Drawable pressed = this.getResources().getDrawable(p3);
bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
bg.addState(View.ENABLED_STATE_SET, normal);
bg.addState(View.FOCUSED_STATE_SET, selected);
bg.addState(View.EMPTY_STATE_SET, normal);
this.setBackgroundDrawable(bg);
}
}
And you can use it like this in your onCreate Method
this.book_appointment = (MButton) this._view
.findViewById(R.id.btn_book);
this.book_appointment.setBackgroundPressedBg(R.drawable.btnnormal,
R.drawable.btnsel, R.drawable.btnsel);
This will go like this in your xml layout file
<com.packageName.custom.MButton
android:id="@+id/btn_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_time1"
android:layout_marginTop="20dp"
android:background="@drawable/bookapointment" />

- 1,924
- 4
- 21
- 38
here in this post you can find the solution:
https://stackoverflow.com/a/18196156/2000517
Take care! ;)

- 1
- 1