3

I have a custom control which extends the DialogPreference where I have custom attributes and I want to define default values for them.

Here is the relevent part of my attrs.xml:

<!-- definition of my custom attributes -->
<declare-styleable name="MyPreference">
    <attr name="myAttr1" format="string" />
    <attr name="myAttr2" format="reference" />
</declare-styleable>
<!-- declatation of my style for my AppTheme -->
<declare-styleable name="AppTheme">
    <attr name="myPreferenceStyle" format="reference" />
</declare-styleable>

themes.xml:

<style name="AppTheme" parent="@style/Theme.Sherlock.Light.DarkActionBar">
    <!-- try of replacing the default text color -->
    <item name="android:textAppearance">@style/WhiteText</item>
    <item name="myPreferenceStyle">@style/Preference.My</item>
</style>

styles.xml:

<style name="WhiteText" parent="@android:style/TextAppearance">
    <!-- set the default color to white... however it doesn't work -->
    <item name="android:textColor">#fff</item>
</style>

<style name="Preference">
    <item name="android:positiveButtonText">@android:string/ok</item>
    <item name="android:negativeButtonText">@android:string/cancel</item>
</style>

<style name="Preference.My">
    <item name="android:dialogLayout">@layout/preferences_my_picker</item>
    <item name="myAttr1">@string/unknown</item>
    <item name="myAttr2">@array/bits</item>
</style>

So I have defined that I want that the class MyPreference should have the default values like this:

  • android:positiveButtonText = "OK"
  • android:negativeButtonText = "Cancel"
  • android:dialogLayout = <ref to a layout>
  • myAttr1 = "Unknown"
  • myAttr2 = [1, 2, 4]

But when I try to access them I get nothing:

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

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyPreference, defStyle, 0);

    String txt = a.getText(R.styleable.MyPreference_myAttr1);
    // txt == null :(

    int bitsResId = a.getResourceId(R.styleable.MyPreference_myAttr2, -1);
    // next line will crash bitsResId == -1
    int[] bits = res.getIntArray(bitsResId);

    a.recycle();
}

public MyPreference(Context context, AttributeSet attrs) {
    this(context, attrs, R.attr.myPreferenceStyle);
}

I would be really helpful if somebody could explain me what I do wrong. And also why I cannot change the default text color to white.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • Ahrgs dammed yeah a typo. I would be happy if you could remove that part from the comment^^ By the way I pass the style to the third parameter I checked it in the debugger the other constructors are not called. – rekire Jun 26 '13 at 20:31
  • They aren't called? And then what constructors are used when you use your custom `DialogPreference`? – user Jun 26 '13 at 20:41
  • The two parameter constructor is called so I insert automatically the third parameter. – rekire Jun 27 '13 at 05:32
  • Hey, did you solve this? Your question is a duplicate with this one http://stackoverflow.com/questions/4493947/how-to-define-theme-style-item-for-custom-widget – user Jun 30 '13 at 08:23

0 Answers0