Using ImageButton selector in android

August 24, 2012 | Rishabh Agrawal | General

If we wish to display another image on the ImageButton after it has been clicked then we use selectors.

Selector is an  xml file which is created in the drawable folder , helps us set a default image and a image that is set on of the following events:-

1. android:state_focused

2. android:state_enabled

3. android:state_checkable

4. android:state_selected

5. android:state_pressed

6. android:state_active

7. android:state_single

8. android:state_accelerated

9. android:state_hovered

10. android:state_drawable

11. android:state_first

12. android:state_last

13. android:state_middle

When the image button is used in layout ,then instead of selecting a image ,select the selector xml file in the following way:

STEPS:

1. Create a xml file for selector in the drawable folder.

Syntax:

<selector xmlns:android=”http://schemas.android.com/apk/res/android” >
<item android:drawable=”@drawable/camera_pressed”
android:state_pressed=”true” />
<item android:drawable=”@drawable/camera_normal” android: />

</selector>

2. Set android:background attribute in the following way

android:background=”@drawable/review_selector”

Example:

<ImageButton
android:id=”@+id/reviewBtn_id”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignTop=”@+id/playBtn_id”
android:layout_marginTop=”63dp”
android:layout_toRightOf=”@+id/playBtn_id”
android:background=”@drawable/review_selector” />

 

By Rachita Nanda


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal