This code is written in the layout / xml file for spinner:
<Spinner android:id="@+id/planets_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/planets_array" />
Java code inside onCreate (Bundle savedInstanceState) {} in MainActivity.java:
ArrayList<String> spinnerArray = new ArrayList<String>(); spinnerArray.add("one"); spinnerArray.add("Jupiter"); spinnerArray.add("Mercury"); spinnerArray.add("Venus"); spinnerArray.add("Earth"); Spinner spinner = new Spinner(this); ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray); spinner.setAdapter(spinnerArrayAdapter);
What could be wrong?
source share