title: Programmatically-inflated Button not accepting XML style
body: <p>I'm trying to apply a custom Button background across my entire application, but it only seems to work when I do it programmatically.</p>
  
  <p>Here is <strong>styles.xml</strong>. I use <code>&lt;item name="android:buttonStyle"&gt;@style/OverlayButton&lt;/item&gt;</code> to indicate the custom style "OverlayButton" (by the way, <code>&lt;item name="buttonStyle"&gt;@style/OverlayButton&lt;/item&gt;</code> gives "No resource found that matches the given name: attr 'buttonStyle'.") and then <code>&lt;item name="android:background"&gt;@drawable/overlay_button&lt;/item&gt;</code> to call the custom <code>Drawable</code>.</p>
  
  <pre><code>&lt;resources xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
      &lt;!--
          Base application theme, dependent on API level. This theme is replaced
          by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
      --&gt;
      &lt;style name="AppBaseTheme" parent="android:Theme.Light"&gt;
          &lt;!--
              Theme customizations available in newer API levels can go in
              res/values-vXX/styles.xml, while customizations related to
              backward-compatibility can go here.
          --&gt;
      &lt;/style&gt;
  
      &lt;!-- Application theme --&gt;
      &lt;style name="AppTheme" parent="AppBaseTheme"&gt;
          &lt;!-- All customizations that are NOT specific to a particular API-level can go here. --&gt;
          &lt;!-- http://stackoverflow.com/questions/6159113/android-where-is-the-spinner-widgets-text-color-attribute-hiding --&gt;
          &lt;item name="android:spinnerItemStyle"&gt;@style/SpinnerItem&lt;/item&gt;
          &lt;item name="android:spinnerDropDownItemStyle"&gt;@style/SpinnerItem.DropDownItem&lt;/item&gt;
          &lt;!-- http://stackoverflow.com/questions/8922924/how-to-change-android-spinner-popupbackground --&gt;
          &lt;!-- http://www.ezzylearning.com/tutorial.aspx?tid=1763429 --&gt;
          &lt;item name="android:listViewStyle"&gt;@style/myListView&lt;/item&gt;
          &lt;item name="android:buttonStyle"&gt;@style/OverlayButton&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="SpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem"&gt;
          &lt;item name="android:textColor"&gt;#ADD8E6&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="SpinnerItem.DropDownItem" parent="@android:style/Widget.DropDownItem.Spinner"&gt;
          &lt;item name="android:textColor"&gt;#ADD8E6&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="myListView" parent="@android:style/Widget.ListView"&gt;
          &lt;item name="android:background"&gt;@drawable/intro_spinner&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;!-- Style for Overlay Buttons --&gt;
      &lt;style name="OverlayButton" parent="@android:style/Widget.Button"&gt;
          &lt;item name="android:background"&gt;@drawable/overlay_button&lt;/item&gt;
          &lt;item name="android:gravity"&gt;center_vertical|center_horizontal&lt;/item&gt;
          &lt;item name="android:layout_width"&gt;match_parent&lt;/item&gt;
          &lt;item name="android:layout_height"&gt;wrap_content&lt;/item&gt;
          &lt;item name="android:textSize"&gt;12sp&lt;/item&gt;
          &lt;item name="android:textColor"&gt;#FF000000&lt;/item&gt;
          &lt;item name="android:layout_marginLeft"&gt;0dp&lt;/item&gt;
          &lt;item name="android:layout_marginRight"&gt;0dp&lt;/item&gt;
          &lt;item name="android:layout_marginTop"&gt;10dp&lt;/item&gt;
          &lt;item name="android:layout_marginBottom"&gt;10dp&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;!-- Style for Confirmation Dialog --&gt;
      &lt;style name="AlertDialogCustom" parent="@android:style/Theme.Dialog"&gt;
          &lt;item name="android:windowBackground"&gt;@android:color/transparent&lt;/item&gt;
          &lt;item name="android:background"&gt;@drawable/dialog_background&lt;/item&gt;
          &lt;item name="android:textColor"&gt;#FF000000&lt;/item&gt;
          &lt;item name="android:textSize"&gt;12sp&lt;/item&gt;
          &lt;item name="android:typeface"&gt;monospace&lt;/item&gt;
      &lt;/style&gt;
  
  &lt;/resources&gt;
  </code></pre>
  
  <p>The custom Drawable, <strong>overlay_button.xml</strong>, is here.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector
      xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
      &lt;item android:state_pressed="true" &gt;
          &lt;shape&gt;
              &lt;gradient
                  android:startColor="#999999"
                  android:endColor="#c0c0c0"
                  android:angle="270" /&gt;
              &lt;stroke
                  android:width="3dp"
                  android:color="#202619" /&gt;
              &lt;corners
                  android:radius="3dp" /&gt;
              &lt;padding
                  android:left="1dp"
                  android:top="1dp"
                  android:right="1dp"
                  android:bottom="1dp" /&gt;
              &lt;margin
                  android:left="5dp"
                  android:top="5dp"
                  android:right="5dp"
                  android:bottom="5dp" /&gt;
          &lt;/shape&gt;
      &lt;/item&gt;
  
      &lt;item android:state_focused="true" &gt;
          &lt;shape&gt;
              &lt;gradient
                  android:endColor="#999999"
                  android:startColor="#c0c0c0"
                  android:angle="270" /&gt;
              &lt;stroke
                  android:width="3dp"
                  android:color="#202619" /&gt;
              &lt;corners
                  android:radius="3dp" /&gt;
              &lt;padding
                  android:left="1dp"
                  android:top="1dp"
                  android:right="1dp"
                  android:bottom="1dp" /&gt;
              &lt;margin
                  android:left="5dp"
                  android:top="5dp"
                  android:right="5dp"
                  android:bottom="5dp" /&gt;
          &lt;/shape&gt;
      &lt;/item&gt;
  
      &lt;item&gt;        
          &lt;shape&gt;
              &lt;gradient
                  android:endColor="#aa393939"
                  android:startColor="#aa737373"
                  android:angle="270" /&gt;
              &lt;stroke
                  android:width="3dp"
                  android:color="#202619" /&gt;
              &lt;corners
                  android:radius="3dp" /&gt;
              &lt;padding
                  android:left="1dp"
                  android:top="1dp"
                  android:right="1dp"
                  android:bottom="1dp" /&gt;
              &lt;margin
                  android:left="5dp"
                  android:top="5dp"
                  android:right="5dp"
                  android:bottom="5dp" /&gt;
          &lt;/shape&gt;
      &lt;/item&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p>When I create a Button with <code>final Button b = new Button(context);</code> I do not get the custom Drawable. This, however, works:</p>
  
  <pre><code>final Button b = new Button(context);
  Drawable buttonStates; // get Drawable to set button states
  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
      buttonStates = context.getResources().getDrawable(R.drawable.overlay_button, context.getTheme());
  } else {
      buttonStates = context.getResources().getDrawable(R.drawable.overlay_button);
  }
  b.setBackground(buttonStates);
  </code></pre>
  
  <p>Why should this work programmatically but not in the XML?</p>
  
  <p><strong>Update</strong>: @r-zagórski had a good clue. The XML-inflated buttons take the custom style and work as expected. The buttons that don't work are created programmatically and inserted via <code>LinearLayout.addView(b)</code>. (By the way, <code>final Button b = new Button(context, null, R.drawable.overlay_button_selector)</code> doesn't work.) I don't see why this is not working like the XML-inflated buttons.</p>