The Image Button Control

Android provides an image button via android.widget.ImageButton. Using an image button is similar to using the basic button (see Listing 4-10).

Listing 4-10. Using an ImageButton

<ImageButton android:id="@+id/imageBtn" android:layout_width="wrap_content" android:layout_height="wrap_content"

ImageButton btn = (ImageButton)this.findViewById(R.id.imageBtn); btn.setImageResource(R.drawable.icon);

You can set the button's image dynamically by calling setImageResource or modifying the XML layout file (by setting the android:src property to the image ID), as shown in Listing 4-11.

Listing 4-11. Setting the ImageButton Image via XML

<ImageButton android:id="@+id/imageBtn" android:src="@drawable/btnImage" android:layout_width="wrap_content" android:layout_height="wrap_content"

0 0

Post a comment