title: Android in Eclipse: Programmatically Assigning Nine-Patch to View
body: <blockquote>
    <p><strong>SOLVED and contents edited for clarity.</strong></p>
  </blockquote>
  
  <p><strong>Relevant References:</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/16585528/android-adt-version-22-r-java-files-not-generated">Android ADT version 22, R.java files not generated</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2757107/developing-for-android-in-eclipse-r-java-not-generating">Developing for Android in Eclipse: R.java not regenerating</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5079868/create-a-ninepatch-ninepatchdrawable-in-runtime">Create a NinePatch/NinePatchDrawable in runtime</a></p>
  
  <p><a href="https://code.google.com/p/android-apktool/wiki/9PatchImages" rel="nofollow noreferrer">https://code.google.com/p/android-apktool/wiki/9PatchImages</a></p>
  
  <p><strong>Question:</strong> How do I programmatically (i.e. runtime and not via an XML define) get a reference of my nine-patch into R.java so that I can call it from <code>EmployeeAdapter::getView()</code>?</p>
  
  <blockquote>
    <p><strong>Answer:</strong> </p>
    
    <ul>
    <li>To <strong>runtime</strong>/programmatically set the nine patch background <strong>identically</strong> for <strong>ALL</strong> View objects, execute
    <code>view.setBackgroundResource(R.drawable.blue_fill)</code> inside of <code>Adapter::getView(...)</code> before returning the object</li>
    <li>To <strong>runtime</strong>/programmatically set the nine patch background <strong>individually</strong> for <strong>EACH</strong> View object, execute <code>setBackgroundResource(R.drawable.blue_fill)</code> inside of <code>View::onDraw(...)</code>.</li>
    <li>Be sure that your implementation of your <code>View::onDraw(Canvas c)</code> <strong>does NOT draw over</strong> your 9.png, e.g. <code>c.drawColor(color)</code>.  </li>
    <li>Place your file (blue_fill.9.png) in the <strong>correct</strong> <code>/res/drawable-*/</code> folder (to be safe!)</li>
    <li>I have read that nine patch files need to be all <strong>lowercase</strong>.  (be safe, and do it!)</li>
    <li>There is syntax to <strong>define a nine patch inside an XML file</strong> (I am unsure of exactly how to leverage this and is beyond this scope) but I can assure you that <strong>I did NOT need it</strong>.  In fact, I strongly suspect that my workspace woes ensued after I created <code>/res/drawable-mdpi/blue_fill.xml</code> and then linked it to my <code>/res/drawable-mdpi/blue_fill.9.png</code> with XML syntax.  This is probably wrong!</li>
    <li>Feel free to borrow my <strong>working code below</strong>.</li>
    <li>Because errors with /res/, /layout/ will cause all sorts of problems and possibly a hosed workspace, <strong>be careful</strong>  when editing
    XML or other non-.java files.  <strong>(1)</strong> ALWAYS check LogCat, Problems,
    Console output, <strong>(2)</strong> look in Eclipse's error log, and finally
    <strong>(3)</strong> exhaustively look through your build config to see if problem persists (double check the new in ADT v22 "Project>Properties>Java
    Build Path><strong>Order and Export</strong>" and make sure that your source code
    does not shadow an existing library <strong>higher up the Order</strong>!</li>
    </ul>
    
    <p><strong>Answer to my problem:</strong></p>
    
    <p>Due to a reason completely independent of ADT 22, My workspace was no
    longer regenerating <code>R.java</code>.  I suspect that my messing around with
    <code>/res/drawable-*/</code> folders caused my problems.  I removed all files
    except <code>ic_launcher.png</code> from <code>/res/drawable-*/</code> to try to bring my
    workspace back to a virgin state.  I also suspect that I imported the
    incorrect R library (The <strong>ONLY</strong> one you should import is <code>import
    com.example.yourproject.R</code>)</p>
  </blockquote>
  
  <p><strong>Original Problem</strong>:
  I am able to use <code>bl_fill.9.png</code> with my XML-defined <code>GridView</code>.  However, when I try to programmatically assign the same <code>bl_fill.9.png</code> to my <code>View</code> object inside of <code>EmployeeAdapter::getView(...)</code> I get a compile-time error that <code>bl_fill cannot be resolved or is not a field</code>.</p>
  
  <p>I have spent many days trying to fix this.  Any help is much appreciated!</p>
  
  <p><strong>Environment:</strong></p>
  
  <ul>
  <li><strong>ADT</strong>: v22</li>
  <li><strong>Android SDK Tools</strong>: rev22</li>
  <li><strong>Android SDK Platform-tools</strong>: rev17</li>
  <li><strong>Android SDK Build-tools</strong>: rev17</li>
  <li><strong>Target API</strong>: Android 4.1 Jelly Bean (API level 16)</li>
  <li><strong>Highest API</strong>: Android 4.2 Jelly Bean (API level 17)</li>
  </ul>
  
  <p><strong>Attempts:</strong></p>
  
  <ul>
  <li>Wrong /res/drawable-*/ folder?</li>
  </ul>
  
  <p>I tried all pictures in one folder.  I tried all pictures in all folders.  The pictures seems to be sourced just fine wherever they are because <code>GridView</code> never hiccups about not locating a picture resource.</p>
  
  <ul>
  <li>Bad nine-patch?</li>
  </ul>
  
  <p>To debunk that it may be a poorly formatted *.9.png, I have tried several of them (all of which I downloaded) and every one works on my XML-defined GridView.</p>
  
  <ul>
  <li>Successful Code:</li>
  </ul>
  
  <p>/res/layout/<strong>activity_main.xml</strong> : GridView is defined in XML here, and I am successful in assigning the 9 patch <code>bl_fill</code></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;GridView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/gridview"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"
  
      android:numColumns="5"
      android:columnWidth="90dp"
      android:verticalSpacing="10dp"
      android:horizontalSpacing="10dp"
      android:stretchMode="columnWidth"
      android:gravity="center"
  
      android:background="@drawable/bl_fill"
  /&gt;
  </code></pre>
  
  <p>/res/drawable-xxhdpi/<strong>xxhdpi-9p.xml</strong>: I understand that this file is needed by the compiler to transform a "sourced" nine-patch (contains 1px border of control pixels) into a "compiled" nine-patch (adds a "chunk" metadata and strips all control pixels).  But more importantly, <strong>I understand that <code>int R.java.drawable.bl_fill</code> should be generated from this file definition</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;nine-patch 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:src="@drawable/bl_fill"/&gt;
  </code></pre>
  
  <ul>
  <li>Unsuccessful Code (has been updated and now works):</li>
  </ul>
  
  <p>However, it appears that <code>R.java</code> is generating <code>int</code> references for other resources, but just stubbornly won't generate one for <code>bl_fill</code>.</p>
  
  <p><strong>EmployeeAdapter.java</strong></p>
  
  <pre><code>package com.stackoverflow.signinboard;
  
  import java.util.List;
  
  import android.R;
  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.drawable.Drawable;
  import android.graphics.drawable.NinePatchDrawable;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.GridView;
  
  // useful "Good Practices" for Adapters referenced at
  // http://www.piwai.info/android-adapter-good-practices/
  //
  // also
  // http://androidadapternotifiydatasetchanged.blogspot.in/
  /**
   * An adapter that abstracts the structure of a database and safely exposes
   * information to consumers.
   * 
   * {@link GridView} uses this to obtain self-draw information for itself and
   * {@link EmployeeView} children
   */
  public class EmployeeAdapter extends BaseAdapter {
      private Context mContext;
      private List&lt;Employee&gt; mEmployees;
  
      public EmployeeAdapter(Context c, List&lt;Employee&gt; employees) {
          mContext = c;
          mEmployees = employees;
      }
  
      @Override
      /**
       * Returns the total number of employees
       */
      public int getCount() {
          return mEmployees.size();
      }
  
      /**
       * Returns the specified employee
       * 
       * @param arg0
       *            Index of the employee in database
       */
      @Override
      public Object getItem(int arg0) {
          return mEmployees.get(arg0);
      }
  
      /**
       * Not implemented.
       */
      @Override
      public long getItemId(int arg0) {
          return 0;
      }
  
      /**
       * Gets the EmployeeView specified by 'index'.
       * 
       * @param index
       *            Index of this employee in the database
       * @param convertView
       *            Reference to a View that may or may not be initialized
       * @param parent
       *            Viewgroup that this View is associated with
       * @return An initialized EmployeeView at the specified index
       */
  @Override
  public View getView(int index, View convertView, ViewGroup parent) {
      EmployeeView employeeView;
  
      // Recylce reference if possible, else instantiate a new EmployeeView
      if (convertView == null) {
  
          // Instantiate an Employeeview with the ID and state
          // and define its parameters
          employeeView = new EmployeeView(mContext, mEmployees.get(index)
                  .getmEmployeeId(), mEmployees.get(index).getState());
  
          employeeView.setLayoutParams(new GridView.LayoutParams(150, 85));
          employeeView.setPadding(8, 8, 8, 8);
  
          employeeView.setBackgroundResource(R.drawable.bl_fill);
      } else {
          // Set new values for the ID and state of this EmployeeView
          employeeView = (EmployeeView) convertView;
          employeeView.initEmployee(mEmployees.get(index).getmEmployeeId(),
                  mEmployees.get(index).getState());
      }
  
      return employeeView;
  }
  }
  </code></pre>