I have a spinner widget in my activity. If I run the app on my device (Pie version) the spinner works fine but if I run it on emulator (which has lollipop version- requirements of work)- spinner doesn't show any items at all.
So this is my activity code for the spinner (inside onCreate):
spinner = (Spinner) findViewById(R.id.spinner_reminder_times);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.reminder_times_array, android.R.layout.simple_spinner_dropdown_item);
// Specify the layout to use when the list of choices appears
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the spinnerAdapter to the spinner
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(this);
And this is the implemented method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getSelectedItemPosition()) {
case 0:
// do stuff
break;
case 1:
// do stuff
break;
case 2:
// do stuff
break;
}
}
The spinner xml in activity_layout:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_reminder_times"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:spinnerMode="dropdown">
</Spinner>
What may be the problem? how comes it works perfectly on my device but on android lollipop emulator doesn't show anything?
Thanks!
Create a new layout file, called my_spinner_dropdown_list_item.xml (probably change textColor once you confirm it works):
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:textColor="#FF0000"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
And assign this layout as your Spinner's new dropdown resource:
spinner = (Spinner) findViewById(R.id.spinner_reminder_times);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.reminder_times_array, R.layout.my_spinner_dropdown_list_item);
// Specify the layout to use when the list of choices appears
spinnerAdapter.setDropDownViewResource(R.layout.my_spinner_dropdown_list_item);
// Apply the spinnerAdapter to the spinner
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(this);
enter code here
android:popupBackground="#59b0b9"
// try this to change the popupBackground color
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_reminder_times"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:popupBackground="#59b0b9"
android:spinnerMode="dropdown"/>
Related
as you can see from the code above, the color of the text is set to blue only when I open the drop-down menu while if the menu is not opened the spinner text remains white, I want to make sure that the color of the spinner text is blue even if it is not opened and not white, how can I go about implementing this? the code below is xml and kotlin
file.xml:
<Spinner
android:id="#+id/spinner"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignBottom="#id/textviewtipologia"
android:layout_marginLeft="100dp"
android:layout_marginTop="-40dp"
android:background="#F5F5F5"
android:popupBackground="#F5F5F5"
android:theme="#style/spinnerstyle" />
<style name="spinnerstyle" parent="Base.Widget.AppCompat.Spinner">
<item name="android:textColor">#color/customSpinner</item>
<item name="android:textColorHighlight">#color/customSpinner</item>
<item name="android:textColorHint">#color/customSpinner</item>
</style>
file.kt
spinner = findViewById<Spinner>(R.id.spinner)
var image: ImageView = findViewById<ImageView>(R.id.imgauto)
var listaTipologia: List<TipologiaVeicolo> =
APISupport.getListaTipologieVecioli(firebaseid, email, key)
val tipologieAuto: ArrayList<String> = ArrayList()
var count = 0
for (i in listaTipologia.indices) {
if (count == 0) {
idVeicoloSelect = listaTipologia[i].ID
}
tipologieAuto.add(listaTipologia[i].DESCRIZIONE)
count++
}
You can try to create the spinner in the following way:
First add the spinner in your layout file:
layout.xml
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Now create an spinner_item.xml layout file adding a TextView in that which will work as the selected item of the spinner:
spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- You can change content of the textview as your need-->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#F5F5F5"
android:textSize="18sp"
android:gravity="left"
android:padding="5dip"
/>
Now create a string-array in your strings.xml file which will be the content of the spinner:
strings.xml
<string-array name="languages">
<item>Hindi</item>
<item>English</item>
<item>Spanish</item>
<item>German</item>
<item>Japanese</item>
</string-array>
Now the final thing is to add code in your activity class:
activity.java
// call this method in onCreate() method of activity class
public void setSpinner(){
Spinner spinner = findViewById(R.id.spinner1); // get the spinner in the layout file
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.languages,
R.layout.spinner_item); // create adapter for spinner and set the item layout
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // set the drop down view to the adapter
spinner.setAdapter(adapter); // set adapter to the spinner
spinner.setOnItemSelectedListener((OnItemSelectedListener) this); // set listener to the spinner to get the data whenever item is selected from spinner
}
// override this method to get the selected item of the spinner whenever item of the spinner is selected
// also implements OnItemSelectedListener to your activity class
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// this is the text selected from the spinner
String text = parent.getItemAtPosition(position).toString();
// do whatever you want from the text
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
Hope this helps 🙏
I'm trying to show simple spinner
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = new String[]{Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerHeaderType.setAdapter(adapter);
In layout xml
<Spinner
android:id="#+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:spinnerMode="dropdown"></Spinner>
But when I click on spinner,
either 1. the dropdown list width is almost zero
OR 2. there are no itmes in dropdown
I tried, 1. giving spinner width as match_parent in xml layout and 2. using dropDownWith property for spinner etc, but nothing working
See image below:
What wrong I'm doing?
try this.
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = {Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
mSpinnerHeaderType.setAdapter(adapter);
xml file
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"></Spinner>
I need spinner in Action Bar for filtering content, looking something similar to this:
But spinner should fill full length of action bar.
What i tried:
1) actionBar.setListNavigationCallbacks(navigationAdapter, this);
Yes, it works. But i need this filter only in 1 fragment. Yes, i can use ActionBar.NAVIGATION_MODE_LIST only in this fragment, and change it to ActionBar.NAVIGATION_MODE_STANDARD in others. But if i will want to add differenat filter in another fragment. So, i should to setting type, adapter and etc in every fragment. It's very very awfully.
2) Better solution is to use menu. In onCreateOptionsMenu of fragment i can add item with actionViewClass="android.widget.Spinner" only in fragment where i need this. Almost good solution except that this item will align right and doesn't fill full action bar.
So, my question is: how to make menu item fill full action bat length?
Or can you advise better solution?
Btw, setCustomView for ActionBar - bad solution for the same reasons that setListNavigationCallbacks...
Found solution. You should user your own layout for spinner item.
Instead of:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.projects_filteres, android.R.layout.simple_spinner_item);
Use:
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.projects_filter_item,
android.R.id.text1, getActivity().getResources().getStringArray(R.array.projects_filteres));
Where R.array.projects_filteres - just simple array from resources, and R.layout.projects_filter_item is RelativeLayout with android:layout_width="match_parent":
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill_horizontal"
android:orientation="vertical" >
<TextView
android:textColor="#000"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
So, whole code of onCreateOptionsMenu in fragment looks something like this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.action_bar_context_menu, menu);
android.view.MenuItem filter = menu.findItem(R.id.context_menu_filter);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.projects_filter_item,
android.R.id.text1, getActivity().getResources().getStringArray(R.array.projects_filteres));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner)MenuItemCompat.getActionView( filter);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
I am customizing spinner in Android / Xamarin project. It's set to SimpleSpinnerDropDownItem. I would like to add icon which is with selected / default item. What I've already did with style is Spinner background (it's white and rounded), also there is style for background in drop down list.
Spinner implementation
Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);
var adapter = ArrayAdapter.CreateFromResource(
this, Resource.Array.organisation_array, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.SetSelection(1);
spinner.Adapter = adapter;
XML in Activity
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:background="#drawable/rounded_shape"
style="#style/DropDownSpinner.My"
android:minHeight="50dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
XML Styles
<style name="DropDownSpinner.My" parent="#android:style/Widget.Holo.Light.Spinner">
<item name="android:popupBackground">#drawable/menu_dropdown_panel_spinner</item>
</style>
I hope this code work for you :
private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
spinner.GetItemAtPosition(e.Position)
.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.favicone,0);
// Reset the others
}
create a custom adapter with custom layout.
public class MyAdapter extends ArrayAdapter<String>{
you should have a special layout for selected item (layout with text and a start icon)
below is the psudo code
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt){
...
int selected = mySpinner.getSelectedItemPosition();
if(position == selected){
return special layout with the start icon
}
return normal layout
}
Use android.R.layout.simple_spinner_item.
I use this class java:
final Spinner s = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.seleziona, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(
new NothingSelectedSpinnerAdapter(
adapter,
R.drawable.contact_spinner_row_nothing_selected,
// R.layout.contact_spinner_nothing_selected_dropdown, // Optional
this));
s.setPrompt("Seleziona");
s.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String voceSelezionata = (String) s.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
By default, the color is blue.
How change only color in red?`
You mean textcolor?
Make custom xml file for your spinner item.
spinner_item.xml:
give your customized color and size to text in this file.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
Now use this file to show your spinner items like:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
You don't need to set drop down resource.it will take spinner_item.xml only to show your items in spinner.