title: How to dynamically populate Android spinner with text + image
body: <p><br/>
  <strong>This is what i am trying to achieve</strong>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>I am able to achieve this much:</strong><br/></p>
  
  <p><img src="https://i.stack.imgur.com/ZCOJz.jpg" alt="screenshot"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="https://i.stack.imgur.com/UVtvj.jpg" alt="enter image description here"></p>
  
  <hr/>
  
  <p>I was able to add text and image to a Spinner, but the images are not showing up in the drop-down menu next to the text. Its only shows up for the selected item of the spinner.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>spinner_layout.xml</strong><br/>
  The layout containing the main Spinner.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;Spinner android:id="@+id/mySpinner"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><br/>
  <strong>spinner_value_layout.xml</strong><br/>
  The layout for the elements in the Spinner. This will be inflated using an Adapter.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" 
      android:orientation="vertical"&gt;
  
      &lt;TableRow
          android:layout_width="match_parent"
          android:layout_height="match_parent" &gt;
  
          &lt;TextView
              android:id="@+id/spinnerTextView"
              android:layout_width="match_parent"
              android:layout_height="match_parent" /&gt;
  
          &lt;ImageView 
              android:id="@+id/spinnerImages"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:contentDescription="@string/app_name"/&gt;"
  
      &lt;/TableRow&gt;
  
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p><br/>
  <strong>SpinnerActivity.java</strong></p>
  
  <pre><code>package com.example.spinner;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.ImageView;
  import android.widget.Spinner;
  import android.widget.TextView;
  
  import com.example.tempspinner.R;
  
  public class SpinnerActivity extends Activity {
  
      String[] textArray = { "clouds", "mark", "techcrunch", "times" };
      Integer[] imageArray = { R.drawable.clouds, R.drawable.mark,
              R.drawable.techcrunch, R.drawable.times };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.spinner_layout);
  
          TextView text = (TextView) findViewById(R.id.spinnerTextView);
          ImageView imageView =(ImageView)findViewById(R.id.spinnerImages);
          Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
  
          SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_value_layout, textArray, imageArray);
          spinner.setAdapter(adapter);
  
      }
  
  }
  </code></pre>
  
  <p><br/>
  <strong>SpinnerAdapter.java</strong></p>
  
  <pre><code>package com.example.spinner;
  
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import com.example.tempspinner.R;
  
  public class SpinnerAdapter extends ArrayAdapter&lt;String&gt; {
  
      private Context ctx;
      private String[] contentArray;
      private Integer[] imageArray;
  
      public SpinnerAdapter(Context context, int resource, String[] objects,
              Integer[] imageArray) {
          super(context,  R.layout.spinner_value_layout, R.id.spinnerTextView, objects);
          this.ctx = context;
          this.contentArray = objects;
          this.imageArray = imageArray;
      }
  
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          if (convertView == null) {
              LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = inflater.inflate(R.layout.spinner_value_layout, null);
  
          }
  
          TextView textView = (TextView) convertView.findViewById(R.id.spinnerTextView);
          textView.setText(contentArray[position]);
  
          ImageView imageView = (ImageView)convertView.findViewById(R.id.spinnerImages);
          imageView.setImageResource(imageArray[position]);
  
          return convertView;
  
      }
  
  }
  </code></pre>