replace selector images dynamically android

Android lets you  change the button’s image at runtime depending on it’s states like s focused or pressed.

This is achieved using selectors .The selector  xml is created in drawable folder ,which is a sub folder of res folder. In this example when the button is pressed the background image changes.

The selector.xml is given bellow


<?xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:drawable="@drawable/ok_pressed"

android:state_pressed="true" />

<item android:drawable="@drawable/ok"  />

</selector>

Code in layout  file :

Here, we set selector as the background of the button


<Button

android:id="@+id/b1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/selector"

android:text="1"

/>

 

Also, we can dynamically replace selector images  in the following manner.

 

StateListDrawable -Lets you assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value.


Button button=(button) findViewById(R.id.button_id);

StateListDrawable state = new StateListDrawable();

state = new StateListDrawable();

//add new image resource for a specific state

state.addState(new int[] {android.R.attr.state_pressed},

getResources().getDrawable(R.drawable.btn_up_cyan));

state.addState(new int[] {android.R.attr.state_focused},

getResources().getDrawable(R.drawable.btn_up_cyan));

button.setBackgroundDrawable(state);//set the background of button according to ‘state’

Hope this post is helpful .:)

Advertisements

Using ImageButton selector in android

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"  />

</selector>

  1. 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" />

Wasn’t it simple 🙂