Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The simple way to do this is to use an ArrayAdapter created using <a href="http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20int,%20java.util.List%3CT%3E%29" rel="nofollow">this constructor</a>. The second and third parameters are the ID of the layout of each row (which you can define in a XML file) and respectively the ID of the text field from that layout which can represent your items.</p> <p>For example you can have a layout defined like this (row.xml):</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="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"&gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"/&gt; &lt;TextView android:id="@+id/weekofday" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>Then you could create your ArrayAdapter like this:</p> <pre><code>package com.exercise.AndroidCustomSpinner; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Spinner; public class AndroidCustomSpinner extends Activity { String[] DayOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner mySpinner = (Spinner)findViewById(R.id.spinner); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.row, R.id.weekofday, DayOfWeek); mySpinner.setAdapter(adapter); } } </code></pre> <p>Note the call <code>ArrayAdapter&lt;String&gt;(this, R.layout.row, R.id.weekofday, DayOfWeek);</code> which defines the row layout as <code>R.layout.row</code>, the textfield that shows the data <code>R.id.weekofday</code> and the data to the array <code>DayOfWeek</code>.</p> <p>The only downside to this is that you cannot change the icon for each item... if you want to do that, you want to override the BaseAdapter.getView() to create (or reuse) a view for each row of the spinner in pretty much the same way you would for a ListView. There are literally hundreds of tutorials on how to do that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload