setImageResource( R.drawable.icon1)
package com.example.ib;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnTouchListener {
ImageButton button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(ImageButton)findViewById(R.id.button1);
button1.setImageResource(R.drawable.icon1);
button1.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent ME) {
// TODO Auto-generated method stub
if(ME.getAction()==MotionEvent.ACTION_DOWN)
button1.setImageResource(R.drawable.icon2);
else if(ME.getAction()==MotionEvent.ACTION_UP)
button1.setImageResource(R.drawable.icon1);
return false;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>