EDIT 3
These examples are only from scratch, now there is no IDE to test.
If you do not need a special adapter, you can work with the standard ArrayAdapter. Then set the dropDownViewResource parameter for your adapter.
public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); Spinner s = (Spinner) this.findViewById(R.id.spinner); ArrayList<String> list = new ArrayList<String>(); list.add("Germany"); list.add("USA"); list.add("Nairobi"); list.add("Japan"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s.setAdapter(adapter); s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText(getApplicationContext(), "CLICKED:"+parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView parent) {
But if you need a customAdapter, then there is no other option but to build your own spinner element layout:
Create your main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Spinner android:drawSelectorOnTop="true" android:id="@+id/example_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" > </Spinner> </LinearLayout>
Create your instance of Spinner xml: spinner_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spinner_item_linear_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content " android:id="@+id/spinner_textView" > </TextView> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinner_radio_button" > </RadioButton> </LinearLayout>
Define an array of strings with some input, create your own adapter and install the adapter on your counter.
public class CustomSpinnerExample extends Activity { String []countries ={"Germany","USA","Nairobi","Japan"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner mySpinner = (Spinner)findViewById(R.id.example_spinner); mySpinner.setAdapter(new CustomAdapter(this, R.layout.spinner_item, countries)); } public class CustomAdapter extends ArrayAdapter<String> { public CustomAdapter(Context context, int resourceId, String[] objects) { super(context, resourceId, objects);