title: Programmatically tint a Support Vector
body: <p>Android Studio  version 2.1, gradle version 2.1.0, please correct me if you spot any misinterpretations :)</p>
  
  <p>I am confused about support vectors in the support library 23.3.0. Specifically what I would like to do is tint an image button programmatically, whose src is defined is a vector drawable. From what I can tell this is not possible on pre-lollipop now.</p>
  
  <p>I have read several related posts about the changes:
  <a href="http://android-developers.blogspot.ie/2016/02/android-support-library-232.html" rel="noreferrer">23.2.0 announcement and changes</a>: </p>
  
  <blockquote>
    <p>As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat or setImageResource().</p>
  </blockquote>
  
  <p>Does the above mean that vector xmls can only be used pre-Lollipop via srcCompat or setImageResource(), and therefore <strong>cannot be dynamically tinted</strong>?</p>
  
  <p>Here is my basic image button:</p>
  
  <pre><code>&lt;ImageButton
      android:id="@+id/nav_header_exit_community_button"
      android:layout_width="48dp"
      android:layout_height="48dp"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:background="@null"/&gt;
  </code></pre>
  
  <p><em>Works on Lollipop and above only:</em></p>
  
  <pre><code>    Drawable bg = ContextCompat.getDrawable(a, R.drawable.ic_exit_to_app_24dp);
      DrawableCompat.setTint(bg, headerTitleColor);
      exitButton.setImageDrawable(bg);
  </code></pre>
  
  <p>Attempting this pre-lollipop throws:
  <code>android.content.res.Resources$NotFoundException: File res/drawable/ic_exit_to_app_24dp.xml from drawable resource ID #0x7f0200bf</code></p>
  
  <p><em>Also works on Lollipop and above only</em></p>
  
  <pre><code>    Drawable bg = ContextCompat.getDrawable(a, R.drawable.ic_exit_to_app_24dp);
      DrawableCompat.setTint(bg, headerTitleColor);
      exitButton.setImageResource(R.drawable.ic_exit_to_app_24dp);
  </code></pre>
  
  <p>This throws the same error on pre-Lollipop.</p>
  
  <p>However if I remove <code>vectorDrawables.useSupportLibrary = true</code> as pointed out <a href="https://stackoverflow.com/a/36484749/1866373">by Ian Lake here</a>, with the intent of having the build tools auto-generate pngs for pre-Lollipop devices, <strong>the pngs do not tint on pre-lollipop</strong>, so I'm back to square one.</p>
  
  <p>I have also tried specifying the vector via <code>srcCompat</code> and retrieving it programmatically but I don't think I've been able to achieve that, even though it works on post-Lollipop if the vector is specified using <code>src</code> instead.</p>
  
  <p>So the situation for 23.3.0 seems to be: </p>
  
  <ul>
  <li><p>Post-Lollipop: <code>src</code> and <code>srcCompat</code> accept vectors, only <code>src</code> can be
  retrieved from the view as a drawable for tinting programmatically.
  Referencing vectors in code is possible using getDrawable, and they
  can be tinted.</p></li>
  <li><p>Pre-Lollipop: srcCompat only can accept vectors, cannot be retrieved
  programmatically from the view for tinting.  <code>setImageResource</code> can
  accept vectors, but only if <code>vectorDrawables.useSupportLibrary = false</code>, and tinting does not work. Similarly referencing vectors in code is not
  possible unless <code>vectorDrawables.useSupportLibrary = false</code> and tinting
  does not work.</p></li>
  </ul>
  
  <p><em>Working on all versions using pngs:</em></p>
  
  <pre><code>   Drawable bg = ContextCompat.getDrawable(a, R.drawable.ic_nav_exit_community);
     DrawableCompat.setTint(bg, headerTitleColor);
     exitButton.setImageDrawable(bg);
  </code></pre>
  
  <p><em>Addendum:</em></p>
  
  <p>This technique also works on post-Lollipop, but like the others on pre-Lollipop I get the drawable, but no tinting:</p>
  
  <pre><code>    Drawable bg = VectorDrawableCompat.create(a.getResources(), R.drawable.ic_exit_to_app_24dp, null);
      DrawableCompat.setTint(bg, headerTitleColor);
      exitButton.setImageDrawable(bg);
  </code></pre>
  
  <p><strong>KIND OF SOLUTION:</strong></p>
  
  <p>Thanks to <a href="https://stackoverflow.com/a/36898394/1866373">John's</a> answer so far the only fool-proof way I can come up with to tint a support vector is to set a color filter on it - this means the <code>DrawableCompat.setTint()</code> function is seemingly not functional for me if the drawable in question is a support vector. I'm not sure if this is a legit bug, expected behavior or if I'm just doing something wrong! </p>
  
  <p>Here is the solution I'm going with for the moment:</p>
  
  <pre><code>    Drawable bg;
      if(Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.LOLLIPOP) {
          bg = VectorDrawableCompat.create(a.getResources(), R.drawable.ic_exit_to_app_24dp, null);
          exitButton.setColorFilter(headerTitleColor, PorterDuff.Mode.MULTIPLY);
      }
      else {
          bg = ContextCompat.getDrawable(a, R.drawable.ic_exit_to_app_24dp);
          DrawableCompat.setTint(bg, headerTitleColor);
      }
      exitButton.setImageDrawable(bg);
  </code></pre>