title: How to define default styles for a custom control?
body: <p>I have a custom control which extends the <a href="http://developer.android.com/reference/android/preference/DialogPreference.html" rel="nofollow">DialogPreference</a> where I have custom attributes and I want to define default values for them.</p>
  
  <p>Here is the relevent part of my <strong>attrs.xml</strong>:</p>
  
  <pre><code>&lt;!-- definition of my custom attributes --&gt;
  &lt;declare-styleable name="MyPreference"&gt;
      &lt;attr name="myAttr1" format="string" /&gt;
      &lt;attr name="myAttr2" format="reference" /&gt;
  &lt;/declare-styleable&gt;
  &lt;!-- declatation of my style for my AppTheme --&gt;
  &lt;declare-styleable name="AppTheme"&gt;
      &lt;attr name="myPreferenceStyle" format="reference" /&gt;
  &lt;/declare-styleable&gt;
  </code></pre>
  
  <p><strong>themes.xml</strong>:</p>
  
  <pre><code>&lt;style name="AppTheme" parent="@style/Theme.Sherlock.Light.DarkActionBar"&gt;
      &lt;!-- try of replacing the default text color --&gt;
      &lt;item name="android:textAppearance"&gt;@style/WhiteText&lt;/item&gt;
      &lt;item name="myPreferenceStyle"&gt;@style/Preference.My&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p><strong>styles.xml</strong>:</p>
  
  <pre><code>&lt;style name="WhiteText" parent="@android:style/TextAppearance"&gt;
      &lt;!-- set the default color to white... however it doesn't work --&gt;
      &lt;item name="android:textColor"&gt;#fff&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name="Preference"&gt;
      &lt;item name="android:positiveButtonText"&gt;@android:string/ok&lt;/item&gt;
      &lt;item name="android:negativeButtonText"&gt;@android:string/cancel&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name="Preference.My"&gt;
      &lt;item name="android:dialogLayout"&gt;@layout/preferences_my_picker&lt;/item&gt;
      &lt;item name="myAttr1"&gt;@string/unknown&lt;/item&gt;
      &lt;item name="myAttr2"&gt;@array/bits&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>So I have defined that I want that the class MyPreference should have the default values like this:</p>
  
  <ul>
  <li>android:positiveButtonText = "OK"</li>
  <li>android:negativeButtonText = "Cancel"</li>
  <li>android:dialogLayout = &lt;ref to a layout&gt;</li>
  <li>myAttr1 = "Unknown"</li>
  <li>myAttr2 = [1, 2, 4]</li>
  </ul>
  
  <p>But when I try to access them I get nothing:</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>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.</p>