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
For me the problem is that I was using getApplicationContext()
. When I replace getApplicationContext()
with this
it works fine.
ArrayAdapter<*> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, documentsCategories);
Check these links:
Else, try to store it on selecting the item:
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
}
});
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
This answer may be a little stupid but try it. It worked for me.
Spinner displays neither the default nor selected item value. But drop-down menu items are show when selected.
Background and text color both being white!!!
Write a custom layout for a spiner item and use it instead of the default,android.R.layout.simple_spinner_item
.
How to change spinner text size and text color?
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 take over...
Thanks Catluc
if you have a custom adapter you should change the TextView text color
@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 should just change spinner background