Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Custom Spinner Layout

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?

like image 426
Flynn Avatar asked Jan 20 '12 19:01

Flynn


1 Answers

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);
   }
}
like image 131
Prexx Avatar answered Oct 05 '22 12:10

Prexx