title: Android Switch set thumbResource programmatically
body: <p>I need to add customised Switch to my application. I know that usually to display it in a way you like you need to create a selector for thumb and track resources and set it in the XML, which I did and it works. Code for selectors is as follows:
  1. thumb_selector.xml</p>
  
  <p><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item android:drawable="@drawable/switch_thumb_disabled" android:state_enabled="false"/&gt;
      &lt;item android:drawable="@drawable/switch_thumb_pressed" android:state_pressed="true"/&gt;
      &lt;item android:drawable="@drawable/switch_thumb_activated" android:state_checked="true"/&gt;
      &lt;item android:drawable="@drawable/switch_thumb"/&gt;
  &lt;/selector&gt;</code></p>
  
  <ol>
  <li><p>bg_selector.xml</p>
  
  <p><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item android:drawable="@drawable/switch_bg_disabled" android:state_enabled="false"/&gt;
      &lt;item android:drawable="@drawable/switch_bg_focused" android:state_focused="true"/&gt;
      &lt;item android:drawable="@drawable/switch_bg_on" android:state_checked="true"/&gt;
      &lt;item android:drawable="@drawable/switch_bg"/&gt;
  &lt;/selector&gt;</code></p></li>
  </ol>
  
  <p>These selectors are located in drawable resource folder, so when I use </p>
  
  <pre><code>android:thumb="@drawable/thumb_selector"
  android:track="@drawable/bg_selector"
  </code></pre>
  
  <p>switch is displayed as expected. However, there are other properties and many Views I have to create dynamically, so it is better to add a custom control (which extends Switch), and I need to set ThumbResource and TrackResource. Documentation says there are two methods, which can set it: <a href="http://developer.android.com/reference/android/widget/Switch.html#setThumbResource%28int%29" rel="nofollow">setThumbResource(int resId)</a> and <a href="http://developer.android.com/reference/android/widget/Switch.html#setTrackResource%28int%29" rel="nofollow">setTrackResource(int resId)</a>, but when I try to reference selector as <code>setThumbResource(R.drawable.thumb_selector)</code> it doesn't recognise this selector as drawable. It's a silly problem and I can't seem to be able to solve it, did I add it to the wrong folder? One way to work around this is to extend layout and inflate xml version of Switch, but do I have to do this? How can I reference selector within the code? </p>