Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>EDIT 3</h2> <p>This examples are just from scratch, got no IDE to test it now.</p> <p>If you don´t need a custom adapter, you can work with standard ArrayAdapter. Then set the dropDownViewResource for Your adapter.</p> <pre><code>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&lt;String&gt; list = new ArrayList&lt;String&gt;(); list.add("Germany"); list.add("USA"); list.add("Nairobi"); list.add("Japan"); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(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&lt;?&gt; parent, View view, int pos, long id) { Toast.makeText(getApplicationContext(), "CLICKED:"+parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView parent) { // Do nothing. } }); } </code></pre> <p>But if You need a customAdapter, then there is no other than build Your own spinner item layout:</p> <p>Build Your main.xml</p> <pre><code> &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;Spinner android:drawSelectorOnTop="true" android:id="@+id/example_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;/Spinner&gt; &lt;/LinearLayout&gt; </code></pre> <p>Build Your Spinner item xml: spinner_item.xml:</p> <pre><code> &lt;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”&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content " android:id="@+id/spinner_textView" &gt; &lt;/TextView&gt; &lt;RadioButton android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:id=”@+id/spinner_radio_button” &gt; &lt;/RadioButton&gt; &lt;/LinearLayout&gt; </code></pre> <p>Define a string array with some input, create your custom adapter and set the adapter to your spinner.</p> <pre><code> 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&lt;String&gt; { public CustomAdapter(Context context, int resourceId, String[] objects) { super(context, resourceId, objects); // TODO Auto-generated constructor stub } @Override public View getDropDownView(int position, View convertView,ViewGroup parent) { return getCustomView(position, convertView, parent); } @Override public View getView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } public View getCustomView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater=getLayoutInflater(); View row=inflater.inflate(R.layout.spinner_item, parent, false); TextView label=(TextView)row.findViewById(R.id.spinner_textView); label.setText(countries[position]); RadioButton radioButton =(RadioButton)row.findViewById(R.id.spinner_radio_button); radioButton.setOnClickListener(new OnClickListener(){ public void onClick(View v){ Toast.makeText(CustomAdapter.this,”CLICKED:”+label.getText(),Toast.LENGTH_LONG).show(); } }); return row; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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