Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom attributes for a DialogPreference

I've created new TimeDialogPreference that extends DialogPreference.

In preferences.xml I have:

<info.chrzanowski.project.preference.TimeDialogPreference
    android:key="recordTime"
    android:id="@+id/recordTime"
    android:title="title"
    android:summary="summary"
    step="5"
    />

How can I access from TimeDialogPreference class that step attribute ?

like image 622
hsz Avatar asked Apr 10 '11 19:04

hsz


People also ask

What are custom attributes?

Custom attributes. A custom attribute is a property that you can define to describe assets. Custom attributes extend the meaning of an asset beyond what you can define with the standard attributes. You can create a custom attribute and assign to it a value that is an integer, a range of integers, or a string.

How do I set custom preferences on Android?

It's still possible to customise the appearance of a Preference item though. In your XML you have to declare the root element as android:id="@android:id/widget_frame , and then declare TextView as android:title and android:summary . You can then declare other elements you want to appear in the layout.


1 Answers

The attributes are passed to the constructor of your custom preference:

Check out the AttributeSet class for other ways to dig out the value, for example:

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

    for (int i=0;i<attrs.getAttributeCount();i++) {
        String attr = attrs.getAttributeName(i);
        String val  = attrs.getAttributeValue(i);
        if (attr.equalsIgnoreCase("step")) {
            Log.i("TimeDialogPreference", "step = "+val);
        }
    }
}
like image 140
slund Avatar answered Sep 25 '22 14:09

slund