Skip to content Skip to sidebar Skip to footer

Android Custom State Button Does Not Work

I have found this interesting answer about implementing custom drawable states using selectors. I copied the source code and added my activity that use custom button. But it does n

Solution 1:

It works finally. The first problem was missing background attribute, the second was missing attribute setting in constructor.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent">

<sandbox.lelisoft.com.dressup.FoodButton
    android:id="@+id/foodButton"
    android:layout_height="60dp"
    android:layout_width="150dp"
    android:layout_margin="@dimen/activity_horizontal_margin"
    app:state_baked="false"
    app:state_fried="true"
    android:background="@drawable/food_button"
    />

And

publicFoodButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    for (int i=0;i<attrs.getAttributeCount();i++) {
        switch (attrs.getAttributeName(i)) {
            case"state_baked": mIsBaked = attrs.getAttributeBooleanValue(i, false); break;
            case"state_fried": mIsFried = attrs.getAttributeBooleanValue(i, false); break;
        }
    }
}

And

<resources><attrname="state_fried"format="boolean" /><attrname="state_baked"format="boolean" /></resources>

Complete code is at https://github.com/literakl/DressUp/tree/states_branch, diff at https://github.com/literakl/DressUp/commit/5995445fc66d1e31abe68d06ee556fdf6416da26

Post a Comment for "Android Custom State Button Does Not Work"