Mando
Mando

Reputation: 11712

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?

Upvotes: 0

Views: 204

Answers (5)

Jibran Khan
Jibran Khan

Reputation: 3256

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

Upvotes: 0

acostela
acostela

Reputation: 2727

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>

Upvotes: 0

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

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 ツ

Upvotes: 1

Bhavishya Prajapati
Bhavishya Prajapati

Reputation: 160

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.

Upvotes: 0

M. Erfan Mowlaei
M. Erfan Mowlaei

Reputation: 1436

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.

Upvotes: 0

Related Questions