I don't need anything fancy, just bigger items in a list (it's too tiny) and different background. Currently it looks like so:
declaration done like this:
<Spinner android:id="#+id/sp_serverName" android:layout_width="fill_parent" android:layout_height="wrap_content" />
And I use code to bind items:
String[] items = new String[] { "Chai Latte", "Green Tea", "Black Tea" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
spinnerServerName.setAdapter(adapter);
spinnerServerName.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Log.v("item", (String) parent.getItemAtPosition(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
In place of android default layout give yours like :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
replace with :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_spinner_item, items);
Related
Appreciate some help here on a spinner issue I'm getting.
The list appears fine when clicking on the drop-down arrow. However, when clicking on the selection, the spinner view still appears as blank. The selection's text does not appear. What gives?
On Android Studio's preview, it appears fine from my assigned android:entries. Screenshot here: (https://imgur.com/a/vmdPA)
As you can see, the background is grey, and everything else is white background as well. So I don't think the color is the issue here.
I've checked and changed background colors, and even removed some widget so that I can see what if anything was blocking the selection to appear.
Is there something aside from the normal declaration of Spinner, Arraylist, creating a new arrayadapter, setDropDwonViewResource, setting the arrayadapter to the spinner that I need to do?
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mStoreSpinner.setAdapter(adapter);
The XML for the spinner is also as "simple" as can be:
<Spinner
android:id="#+id/s_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/recyclerord"
app:layout_constraintLeft_toRightOf="#+id/orderID"
app:layout_constraintTop_toBottomOf="#+id/header"
app:layout_constraintRight_toRightOf="#+id/ConstraintLayout"
android:visibility="visible"
android:layout_marginStart="0dp"
android:entries="#array/array_test"
>
</Spinner>
Thank you.
Edited:
This is what I've added.
mStoreSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int postion, long arg3) {
// TODO Auto-generated method stub
String spinnerValue= parent.getItemAtPosition(postion).toString();
Log.d(TAG, "test");
Toast.makeText(getBaseContext(), "Selected item" + spinnerValue, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mStoreSpinner.setAdapter(adapter);
I have not implemented the onClickListeners yet - do they need to be there before the spinner will work fine?
I guess yes.
Add a setOnItemSelectedListener to your Spinner like this :
mStoreSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String spinnerValue= parent.getItemAtPosition(postion).toString();
Toast.makeText(getBaseContext(),
"Selected item" + spinnerValue,
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Also feel free to see this tutorial to understand it a little bit more
EDIT
You should follow steps :
Declare your Spinner
Spinner spinner = (Spinner) findViewById(R.id.s_spinner);
Create the ArrayAdapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(),
spinerArray, android.R.layout.simple_spinner_item);
Set the DropDown
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Set the adapter
spinner.setAdapter(adapter);
There are two ways to implement the setOnItemSelectedListener()
Implementing its interface : implements OnItemSelectedListener
Using setOnItemSelectedListener(new OnItemSelectedListener() {...}
first of remove in xml in spinner control in this line android:entries="#array/array_test" because if you are passing in adapter in list then already spinner control containing arraylist there for remove it and used below code ...
List<String> spinnerArray=new ArrayList<>(); // hear you can add in any array.
spinnerArray.add("Color");
spinnerArray.add("abd");
spinnerArray.add("cde");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
This is working code, may this helps you:
<Spinner
android:id="#+id/spinner"
style="?android:attr/textViewStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:spinnerMode="dialog"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/colorBlack"
android:textColorHint="#color/colorGray"
android:textSize="#dimen/_14sdp" />
Make custom R.layout.list_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/_4sdp"
android:paddingLeft="#dimen/_14sdp"
android:paddingRight="#dimen/_14sdp"
android:paddingTop="#dimen/_4sdp"
android:text="Test"
android:textColor="#color/colorGray"
android:textSize="#dimen/_14sdp" />
Set adapter like this:
SpinnerAdapter adapter = new SpinnerAdapter(mActivity, R.layout.list_item,
android.R.id.text1, yourListHere);
spinner.setAdapter(adapter);
SpinnerAdapter code:
public class SpinnerAdapter extends ArrayAdapter {
public SpinnerAdapter(#NonNull Context context, #LayoutRes int resource, #IdRes int textViewResourceId, #NonNull Object[] objects) {
super(context, resource, textViewResourceId, objects);
}
public SpinnerAdapter(#NonNull Context context, #LayoutRes int resource, #IdRes int textViewResourceId, #NonNull List objects) {
super(context, resource, textViewResourceId, objects);
}
#Override
public int getCount() {
return super.getCount();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setPadding(0, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
return view;
}
}
I had same problem. As per my experience with this:
If we create ArrayList suppose of String type and we used ArrayAdapter to Bind list. Then Please make sure you have converted your ArrayList to String Array.
ArrayAdapter<String> yourAdapter =
new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
yourArrayList.toArray(new String[yourArrayList.size()]));
This works!
I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I have activity with 3 spinners and selection of 1st spinner is related to 2nd and 2nd selection is related to the 3rd, so I have the spinner code one inside one.
CrustSp, SizeSp and extraDescriptionOneSP are the 3 spinners I have. I have loaded the items to the Spinners and I can select a value as well. But my problem is whenever i made a selection in CrustSp and SizeSp doesn't show in MyOnItemSelectedListener(). But if I made a selection in extraDescriptionOneSP it shows inside MyOnItemSelectedListener().
I can't figure where I have gone wrong, anyone can point out where I have gone wrong it will be really helpful.
This the code
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
List<String> resultSizes = getFilteredSizes(crustSelectedItem);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultSizes);
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String sizeSelectedItem = sizeSP
.getSelectedItem().toString();
List<String> resultTopping = getFilteredToppings(sizeSelectedItem);
extraDescriptionOneSP
.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this));
ArrayAdapter<String> dataAdapterExtraDesOne = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultTopping);
dataAdapterExtraDesOne
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
extraDescriptionOneSP
.setAdapter(dataAdapterExtraDesOne);
extraDescriptionOneSP
.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterExtraDesOne,
R.layout.contact_spinner_row_nothing_selected,
ActivityPizzaCustomize.this));
}
});
}
});
MyOnItemSelectedListener method,
public class MyOnItemSelectedListener implements OnItemSelectedListener {
Activity activity;
String StrOneSPPosition;
public MyOnItemSelectedListener(Activity p_activity) {
activity = p_activity;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// check which spinner triggered the listener
switch (parent.getId()) {
case R.id.sp_crust: {
String StrCrustSPPosition = String.valueOf(crustSP
.getSelectedItemPosition());
System.out.println("crust = " + StrCrustSPPosition);
System.out.println("crust = " + position);
CrustSPPosition = Integer.parseInt(StrCrustSPPosition);
}
I don't see you using the selected position to determine the data for next spinner. BTW you should not make new adapter for each selection, instead reuse the once made adapter.
I have 2 spinners in my activity as category and sub-category.
When i choose some value from category spinner
I want to display its related values/data in sub-category spinner.
How can i perform this?
public class SellActivity extends Activity {
private Spinner spinner1, spinner2;
private String selectedOption;
List<String> addItemList = new ArrayList<String>();
String[] elCat = new String[] {"Mobile", "Television", "Computers","Home Appliances","Games","Others"};
String[] veCat = new String[] {"Cars", "Buses", "Motorcycles","Others"};
String[] reCat = new String[] {"Apartment", "Plot", "Office","Paying Guest","Others"};
String[] hCat = new String[] {"Books", "Furniture", "Musical Instrument","Watches","Others"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sell);
spinner1 = (Spinner) findViewById(R.id.categorySellSpinner);
spinner2 = (Spinner) findViewById(R.id.subCategorySpinner);
selectedOption=String.valueOf(spinner1.getSelectedItem());
System.out.println(selectedOption);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(selectedOption.equals("Electronics"))
{
Collections.addAll(addItemList, elCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SellActivity.this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
}
else if(selectedOption.equals("Home - Lifestyle"))
{
Collections.addAll(addItemList, hCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SellActivity.this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
}
else if(selectedOption.equals("Vehicles"))
{
Collections.addAll(addItemList, veCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SellActivity.this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
}
else if(selectedOption.equals("Real Estate"))
{
Collections.addAll(addItemList, reCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SellActivity.this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
Here is the whole code.
Data is not changing....plus old data adds onto it
Just do like this in every if condition :-
Collections.addAll(addItemList, elCat);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String(this,android.R.layout.simple_spinner_item, addItemList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
spinner2.setAdapter(dataAdapter);
Old data is getting added because you are not clearing the collection you are passing into the adapter just clear the contents of the collection before adding new data into it.
Try this :-
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
addItemList.clear();
spinner2.setAdapter(null);
if(selectedOption.equals("Electronics"))
{
Collections.addAll(addItemList, elCat);
Your code has many problems:
-- Data for spinner1 is missing. From your code, it should be: {"Electronics", "Home - Lifestyle", "Vehicles","Real Estate"}
-- spinner1 does not have an adapter set on it
-- creation and usage of list addItemList is not required
-- usage of selectedOption for checking against the selected item is not required. onItemClick() gives you the selected item in int arg2
-- calling notifyDataSetChanged() to an adapter not set to any View does nothing.
-- arrays can be passed to ArrayAdapters directly, without the need for dumping them into an ArrayList
Try re-initializing the addItemList after coming inside onItemSelected before your if statement..
i have to display a spinner ,it is getting its content from adapter at run time . when spinner appear on screen Select a value should appear on it instead of first value in adapter.Please help
If you want a particular item in your collection to be selected have a look at:
Spinner.setSelection(int position)
Then You have to add that text at first position in you string array.
Spinner rangeSpinner = (Spinner)findViewById(R.id.rangeSpinner);
String[] items = new String[] {"Select Product","Bread","Milk"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.custom_spinner_textview_layout, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
rangeSpinner.setAdapter(adapter);
custom_spinner_textview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18.0sp"
android:textColor="#color/grey_text"
android:gravity="left"/>
**Just try It fri...**
1.String[] _Purchesedlistarray = { "Required", "purchased" };
2.ArrayAdapter Purchesedadapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, _Purchesedlistarray);
Purchesedadapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
_PurchesedSpinner.setAdapter(Purchesedadapter);
3._PurchesedSpinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
Appsconstent._Purchesed = parent.getItemAtPosition(
position).toString();
System.out.println("the Value is--------------->"
+ Appsconstent._Purchesed);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});