16

I am trying to make a fully custom spinner. I am running into difficulties with making the layout that pops up when you press on it. Here is my code for my adapter:

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.my_array, R.layout.spinnertext);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

From what I have read in the documentation, the layout used apears to be set by the line:

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Although every time I change it to a new layout that I make, it makes the app fail when I try and use the spinner. I have tried to look for what "android.R.simple_spinner_dropdown_item" looks like so as to figure out if I am maybe missing anything.

All of my layouts I have tried have been linear or relative layouts, with only a textView.

How can I make a custom layout pop up when the spinner is selected?

Flynn
  • 5,903
  • 7
  • 38
  • 55

1 Answers1

27

row.xml to set up the layout on each row (in this case: one image and text each row):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
       android:id="@+id/icon"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/icon"/>

    <TextView
       android:id="@+id/weekofday"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
</LinearLayout>

Java:

public class AndroidCustomSpinner extends Activity {

 String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
   "Wednesday", "Thursday", "Friday", "Saturday"};

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
         R.layout.row, R.id.weekofday, DayOfWeek);
       mySpinner.setAdapter(adapter);
   }
}
Anti Veeranna
  • 11,485
  • 4
  • 42
  • 63
Prexx
  • 2,959
  • 5
  • 31
  • 48
  • 2
    is the link broken? I cant see the tutorial. – alaeri Jul 24 '12 at 15:54
  • therefore i added a summary of the links' content. Just have a look on my post above. – Prexx Oct 14 '12 at 13:46
  • Ignore the link, the answer has all the details needed. Dead simple, thanks. – zeh Nov 01 '12 at 22:15
  • follow this link. this will link will be helpful. http://android-er.blogspot.com/2010/12/custom-spinner-with-icon.html – newday Dec 28 '12 at 16:22
  • I have another question, sorry for leave it in comments, what if we have to passe diffrent data structure such as list instead of string array to adapter? – newday Dec 28 '12 at 16:23