0

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?

Mando
  • 11,414
  • 17
  • 86
  • 167

5 Answers5

1

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 ツ

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

This answer refers to how you can handle button states in Android You can create a button layout file separately in put that in res > layout folder

For Instance:

if the layout file name is btn.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_pressed="true"  android:drawable="@drawable/btn_pressed" />   
  <item android:drawable="@drawable/btn_normal"/> 

</selector>

You can set BackgroundResource of a Button

yourButton.setBackgroundResource(R.layout.btn);

EDIT

In addition you can refer to this link which is more closer to answer your question

Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
0

You will have to use selector for android.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/normal" />
</selector>

 android:background="@drawable/selector_button" />

just have a look of this link.

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

For selected and pressed you can use an xml file with root for button's background but for disabled I guess you need to handle it in java code. I'm not sure about disabled state.

M. Erfan Mowlaei
  • 1,376
  • 1
  • 14
  • 25
  • could you show an example with "xml file with root button's background" – Mando Nov 18 '14 at 07:27
  • other users have already given you that xml file in their answers, if you are worried about making images you can use Android Asset Studio(google it and select first result) to produce suitable images fast and easy. – M. Erfan Mowlaei Nov 18 '14 at 07:31
  • Oh yes, if there a online tool that would be helpful. Here http://romannurik.github.io/AndroidAssetStudio I couldn't see any button states images generator – Mando Nov 18 '14 at 07:33
  • If my answer helped you please mark it as resolved and vote up, thanks – M. Erfan Mowlaei Nov 18 '14 at 07:34
  • use this tool:http://romannurik.github.io/AndroidAssetStudio/icons-generic.html plus tht xml with as other peopl have mentioned in their answers to implement what you want. – M. Erfan Mowlaei Nov 18 '14 at 07:38
  • thank you for the link but it is still not clear: https://www.dropbox.com/s/oe4q6459nq0gjkq/Screenshot%202014-11-18%2012.21.25.png?dl=0 how to get grayed icon for disabled state for example? – Mando Nov 18 '14 at 18:21
  • Maybe use color and set it's opacity to 100% ? – M. Erfan Mowlaei Nov 18 '14 at 18:24
0

As far as I know you need to handle this states in a new resource file called cursor: e.g:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edittext_selector" android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <!-- Image display in background in select state -->
    <item
       android:state_pressed="true"
       android:drawable="@drawable/edittextback1">
    </item>

    <!-- Image display in background in select state -->
    <item
        android:state_enabled="true"
        android:state_focused="true"
        android:drawable="@drawable/edittextback2">
    </item>

    <!-- Default state -->
    <!--<item android:state_enabled="true"-->
        <!--android:drawable="@drawable/your_ninepath_image">-->
    <!--</item>-->
</selector>
acostela
  • 2,597
  • 3
  • 33
  • 50
  • but here all the state displays the same image and no visual effects for press/disabled states will be applied – Mando Nov 18 '14 at 07:29
  • It's only an example, the image will be de drawable that you will use depending on which state you have. I will edit it so we avoid misunderstandings – acostela Nov 18 '14 at 07:30
  • in this scenario I have to have 3 different images (1 for each state supported) and I have the only image – Mando Nov 18 '14 at 07:31
  • You will need different images as I said before, as far as I know. I think that you need different images to achieve the effect of change depending on the state. – acostela Nov 18 '14 at 07:33