An ImageButton is an AbsoluteLayout which enables you to specify the exact location of its children. This shows a button with
an image (instead of text) that can be pressed or clicked by the user.
Add ImageButton
Open “your.xml” file, just add an ImageButton .
Creating a ImageButton in a Layout File
<!--?xml version= "1.0" encoding= "utf-8" ?--> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <imagebutton android:id= "@+id/imageButton1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/android_button" > </imagebutton></linearlayout> |
Here’s the code, add a click listener on image button.
public class MainActivity extends Activity { ImageButton imageButton; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnButton(); } public void addListenerOnButton() { imageButton = (ImageButton) findViewById(R.id.imageButton1); imageButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(MyAndroidAppActivity. this , "ImageButton is clicked!" , Toast.LENGTH_SHORT).show(); } }); } } |