I have implemented the spinner by populating the array list through database.I can get and show the array list in my spinner array adapter but if I select the item in spinne
add background color white for example and details will display for example
<Spinner
android:id="@+id/size_spinner"
style="@style/FormTextStyle"
android:drawSelectorOnTop="true"
android:layout_marginStart="@dimen/default_gap"
android:background="@android:color/white"
/>
try this code==>
ArrayAdapter<String> stateNameAdaptor = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stateNameList);
stateNameAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnState.setAdapter(stateNameAdaptor);
The issue from what I found was with the style sheet.Use this
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowNoTitle">false</item>
<item name="windowActionBar">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
For the xml layout use this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:paddingBottom="5dp"
style="@style/AppTheme">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spinner"
android:layout_marginTop="10dp"
android:textColor="@color/colorBlack"/>
<Spinner
android:id="@+id/Spinner"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:backgroundTint="@color/colorPrimary"
android:textColorHint="#05ab9a"
android:padding="15dp"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
tools:targetApi="lollipop" />
</LinearLayout>
</ScrollView>
And finally for the class
String [] NUMBERS= {"3 ","6 ","13 "};
Spinner s_spinner = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, NUMBERS);
// Specify the layout to use when the list of choices appears
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
s_spinner.setAdapter(spinnerAdapter );
Try This code
ArrayAdapter<String> arrayAdapte=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,states);
arrayAdapte.setDropDownViewResource(android.R.layout.simple_list_item_1 );
spinnerState.setAdapter(arrayAdapte);
String data=spinnerState.getSelectedItem().toString(); // this is select particular item from list;
int position=spinnerState.getSelectedItemPosition(); // this return position of data selected in list;
You should use the first Spinner to get the values.
Try the following code:
String provider = spinner1.getSelectedItem().toString();
What you are doing is getting the default value of the spinner.
Wierd. I had the same problem. What I did to make it work : 1. Add some initial data to the list ( ex.--- please select - -) 2. Load the rest of data and add it to the list 3. Call adapter.notifyDatasetChaged()