Search code examples
androidbuttonandroid-imagebuttonvisualstates

The only image for all button states for Android?


I have an image for my button. In order to make use of it I have to have 1 additional image for each state: 1. disabled 2. selected 3. pressed etc..

in iOS all those additional states are handled automatically and deferred from original image provided.

Is it possible to accomplish this for Android?


Solution

  • Is it possible to accomplish this for Android?

    No, It is NOT. You need to have all states's images with you to define the selector

    You can define button_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    
        <!-- Selected state -->
        <item android:drawable="@drawable/button_bg_selected" android:state_selected="true"></item>
    
        <!-- Pressed  state -->
        <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
    
         <!-- Disabled   state -->
        <item android:drawable="@drawable/button_bg_disabled" android:state_enabled="false"></item>
    
        <!-- Normal state -->
        <item android:drawable="@drawable/button_bg_normal"></item>
    
    
    
    </selector>
    

    Then simply assign this selector as a background of a button

    <Button
         android:id="@+id/button1"
         android:background="@drawable/button_selector"
         android:layout_width="200dp"
         android:layout_height="126dp"
         android:text="Hello" />
    

    Refer : Button Selector

    Hope it will help you ツ