views:

37

answers:

1

I use custom xml attributes for preferences. The preferences are inflated from xml.

I managed to create and read custom xml attributes for EditTextPreference, ListPreference and CheckBoxPreference by creating custom classes which inherit from the respective preference class.

In the constructor of the class I can read the attributes like so:

public class CustomTextPreference extends EditTextPreference {
    public CustomTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.PreferenceCustomAttrs);
        x = a.getString(R.styleable.PreferenceCustomAttrs_x);
        y = a.getString(R.styleable.PreferenceCustomAttrs_y);
    }
}

My problem is that I can't do this for the PreferenceScreen class, as it is a final class. So my question is: Is there any way I can read the custom attributes of a PreferenceScreen?

+1  A: 

Probably not by the same techniques that you are using. Remember, though, that preference XML files are just XML resources. You can get a parser for the file via getResources().getXml() from your PreferenceActivity. From there, you can read whatever you want.

CommonsWare
thank you for your answer! This would be an option, however with this approach the custom attributes wouldn't be member of the property class. I would have to maintain a manual mapping (eg. via a dictionary) from the property to its custom attributes, which i already do anyway and wanted to get rid of by the custom attributes.
pivotnig