Spinner does not display the selected value
I have applied a spinner filling a list of arrays through the database. I can get and show a list of arrays in my spinner array adapter, but if I select an item in the spinner it won't show in the spinner? That I was wrong here?
Here is my code,
Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
I am getting the selected row of an item using this,
Spinner provid = (Spinner)findViewById(R.id.prospin);
String provider =provid.getSelectedItem().toString();
Can anyone help me with pls !!!
source to share
I got the same problem and solved it by adding notifyDataSetChanged()
after data binding in Spinner.
First of all I have an empty list bind adapter ArrayList
then I get the list of items from the server and add it to this list but forgot notifyDataSetChanged()
after updating the list.
just add adapter.notifyDataSetChanged();
after updating the list.
Hope this will be helpful.
source to share
Problem:
Spinner does not display default or selected value. But dropdown menu items are displayed when selected.
Cause:
The background and text color are both white.
Decision:
XML (Preferred):
Write a custom layout for the spiner element and use it instead of the default one android.R.layout.simple_spinner_item
.
How do I change the font size and text color?
Code (less reliable):
your_spinner_instance.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) view).setTextColor(Color.RED);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Android needs some major update, or maybe dart and flutter should carry over ...
Thanks Catluc
source to share
if you have a custom adapter you should change the text color of the TextView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setTextColor(Color.parseColor("#000000"));
return view;
}
and if you don't have a custom adapter, you just need to change the background of the spinner
source to share
Check out these links:
- http://www.technotalkative.com/android-spinner-example/
- http://www.mkyong.com/android/android-spinner-drop-down-list-example/
Please try again to save it on item selection:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(getApplicationContext(),
"Selected Country : " + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
source to share
Try it.
final ArrayList<String> providerlist= new ArrayList<String>();
Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = providerlist.get(position);
// Showing selected spinner item
Toast.makeText(this,
"Selected Country : " + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
source to share
The problem with what I found is with the stylesheet. 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 layout xml 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 );
source to share
This answer might be a little silly, but if you make the same mistake try ... set the values ββto your ArrayList first, and then assign that arrayList to rotate. I declared a global array list and set it first and then added values ββto it from another method ... at that time I ran into the same problem. Otherwise, you can do notifyDataSetChanged () on your arrayList.
source to share
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;
source to share
You have to make some changes like:
Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
instead of "android.R.layout.simple_spinner_dropdown_item"
adapter1.setDropDownViewResource(android.R.layout.simple_list_item_activated_1);
Use "android.R.layout.simple_list_item_activated_1"
spinner1.setAdapter(adapter1);
It will display the selected value appropriately. The selected color is the default for your applications.
See below link for more information: https://developer.android.com/reference/android/R.layout.html#simple_list_item_activated_1
source to share
out_marginLeft="50dp"
android:layout_marginTop="50dp"
android:layout_marginRight="50dp"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/txtProgress"
android:layout_centerHorizontal="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:progress="50"
android:progressTint="@android:color/black" />
<TextView
android:id="@+id/txtProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Progress"
android:textColor="@android:color/black" />
</LinearLayout>
Java
source to share