Android Custom States for State List

In this tutorial I will show you how to make some custom states which will be used on an ImageButton. Of course, this states could be used for edit text and other too, but in my example I will use ImageButton 🙂

1. After you create a new project with the main activity called “MyActivity” go to res – values and create a new xml file called “attributes”.
2. Now put the following code in the attributes.xml:

    <declare-styleable name="CustomStates">
        <attr name="state_has_new_data" format="boolean"/>
        <attr name="has_new_data" format="boolean"/>

NOTE: The first attribute (the one with “state” prefix) is used in the selector drawable when you set the states and images for each state (see step 3) and the second attribute is used in the layout where you declare your custom image button (see step 5).

3. Now, if you don’t have the drawable folder in res, you should create one. After you create the folder you have to create a image_button_selector.xml file inside the drawable folder. Before to put the code from below you should be sure you have the images which you want to use for image button (if you are doing the tutorial from scratch and want to use my images you can save them on your computer from here by right-clicking on each image and select Save image as)

Now put the following code in the image_button_selector.xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=""
    <item android:state_enabled="true"
    <item android:state_enabled="false"
    <item app:state_has_new_data="true"

NOTE: Notice that in order to use your custom state you will have declare a second xmlns xmlns:app=” which has at the end the name of you activity (mine is com.example).

4. Now you will have to create a new class called CustomStatesImageButton which extends ImageButton class and put the following code:

package com.example;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageButton;

public class CustomStatesImageButton extends ImageButton {

    // this is used when we want to merge our state with the ones from the system
    private static final int[] HAS_NEW_DATA_STATE_SET = {R.attr.state_has_new_data};

    private boolean hasNewData;

    public CustomStatesImageButton(Context context) {

    public CustomStatesImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        loadAttributes(context, attrs);

    public CustomStatesImageButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        loadAttributes(context, attrs);

    private void loadAttributes(Context context, AttributeSet attributeSet) {

        // R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable
        // from attributes.xml
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CustomStates, 0, 0);

        // R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your
        // custom state list in attributes.xml
        hasNewData = typedArray.getBoolean(R.styleable.CustomStates_has_new_data, false);


    public int[] onCreateDrawableState(int extraSpace) {

        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (hasNewData) {
            mergeDrawableStates(drawableState, HAS_NEW_DATA_STATE_SET);
        return drawableState;

    public void setEnabled(boolean enabled) {

        // If the image button was set to Has New Data state and then the user changed the state to Enabled
        // then we have to make the hasNewData flag to false
        if (enabled) {
            hasNewData = false;


        // Call this to force a view to update its drawable state. This will cause drawableStateChanged to be
        // called on this view. Views that are interested in the new state should call getDrawableState.

    public boolean hasNewData(){
        return hasNewData;

    // this method will be used when you will need to set your image button state in the code
    public void setNewData(boolean hasNewData){
        this.hasNewData = hasNewData;

        // Call this to force a view to update its drawable state. This will cause drawableStateChanged to be
        // called on this view. Views that are interested in the new state should call getDrawableState.

5. Go to res – layout – main.xml (or whatever you name the generated xml file from layouts) and put the following code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    <Button android:layout_width="wrap_content"
            android:text="Change State"/>

As you can see from the above code you will have to put the name of your package and the name of the custom image button  instead of ImageButton class. Also notice that you will have to use the second xmlns like you have seen at the step 3 in order to use our custom state app:has_new_data=”false”

6. Now go to MyActivity class and put the following code:

package com.example;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyActivity extends Activity {
    // create an enum to hold the states of the image button
    enum States{

    // initialize the enum with the Enabled state
    States enumStates = States.ENABLED;

    public void onCreate(Bundle savedInstanceState) {

        Button button = (Button)findViewById(;
        final CustomStatesImageButton imageButton = (CustomStatesImageButton)findViewById(;

        // when you click on the Change State button your image from the image button will change
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                if (enumStates == States.ENABLED){
                    enumStates = States.DISABLED;
                } else if (enumStates == States.DISABLED){
                    enumStates = States.NEW_MAIL;
                } else if (enumStates == States.NEW_MAIL){
                    enumStates = States.ENABLED;


And that’s all. Now when you will click the Change State button you will see how the image button will change depending on its states.

NOTE: Don’t laugh too much of my mail buttons :P.  I created them myself and I didn’t pay very much attention to their design and size because I thought that the purpose of this tutorial is to show you how to make custom states not beautiful buttons :P.