Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute set, get enum by name

Tags:

android

enums

xml

I am adding attributeset to a custom component like this:

<declare-styleable name="MySeekBarWidget">      
        <attr name="type">
            <enum name="money" value="1" />
            <enum name="percent" value="2" />
            <enum name="time" value="3" />
            <enum name="money_frequency" value="4" />
            <enum name="money_percent_or_fixed" value="5" />
            <enum name="money_month" value="6" />
            <enum name="time_year_only" value="7" />
        </attr>
</declare-styleable>

I can get the value of each enum by asking it from the typed array:

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

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MySpinnerContainer);

        type = array.getInt(R.styleable.MySeekBarWidget_type,1);

        array.recycle();

    }

But what if I wanted the actual name of the enum, not the value. How would I get that in code? Please help

UPDATE The main reason I am doing this is so that I can reuse this component under different circumstances and have different rules for each circumstances defined in the layout xml like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:app1="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


    <view
        android:id="@+id/autoCalculatorAmountMySeekBarWidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/autoCalculatorMyDateView"
        android:layout_marginLeft="@dimen/side_margin"
        android:layout_marginRight="@dimen/side_margin"
        android:layout_marginTop="@dimen/top_spacing"
        android:tag="@string/amount"
        app:max="@integer/a_million"
        app:type="money"
        class="customComponents.MySeekBarWidget"/>


    <view
        android:id="@+id/autoCalculatorInterestMySeekBarWidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/autoCalculatorAmountMySeekBarWidget"
        android:layout_marginLeft="@dimen/side_margin"
        android:layout_marginRight="@dimen/side_margin"
        android:layout_marginTop="@dimen/top_spacing"
        android:tag="@string/interest_rate"
        app:max="@integer/fifty"
        app:type="percent"
        class="customComponents.MySeekBarWidget"/>
</RelativeLayout>

As you can see, each of the components "MyseekBarWidget" , has a different type (percent vs. money). which will trigger different rules. But when I call to find out the type, I only get the value of the enum. I would like the name so that it makes for cleaner code. I can write:

if(type==money)...;

versus writting:

if(type==1)..;

which makes the code harder to debug/understand

like image 959
jlarrieux Avatar asked Jul 03 '14 03:07

jlarrieux


1 Answers

I'd definitely go the same way the Android team has been doing all the time: define constants in your MySeekBarWidget class, just like this one.

So in your case, you'd define:

class MySeekBarWidget... {
    public static final int TYPE_MONEY = 1;
    public static final int TYPE_PERCENT = 2;
    public static final int TYPE_TIME = 3;
...

And so and so. After that, it's just easy to refer to those ones:

type = array.getInt(R.styleable.MySeekBarWidget_type, TYPE_MONEY);

The TYPE_ prefix was just there in case you want to split them from other styleables. In fact, you could just use an enum for those:

class MySeekBarWidget... {
    public enum Type {
        MONEY(1),
        PERCENT(2),...
        TIME_YEAR_ONLY(7);

        private final int id;

        Type(int id) {
            this.id = id;
        }

        public int getValue() { 
            return id;
        }
    }

Then, it could be like this:

type = array.getInt(R.styleable.MySeekBarWidget_type, Type.MONEY);

And accessing from outside has turned really clean:

int outsideType = MySeekBarWidget.Type.MONEY;

After that, you could also check what type is on by simply doing this:

if (type == Type.MONEY) {
    ...
} else if (type == Type.PERCENT) {
    ...
}

Or even better, to avoid repetition, Enums can be used in switch statements:

switch (type) {
    case Type.MONEY:
        ...
        break;
    case Type.PERCENT:
        ...
        break;
}

This reduces the repetition of type == and provides a clean way to add new types to the conditional.

like image 74
Sergi Juanola Avatar answered Sep 19 '22 11:09

Sergi Juanola