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!
Related
I am working with auto Complete Text View. when I choose item from it, it is displaying first Item instead of showing selected item.
here is my code
Utilities.setAutoCompleteTextViewAdapter(this, districtsNameList, district);
district.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//selectedDistrict = (String) adapterView.getItemAtPosition(position);
selectedDistrict = districtsNameList.get(position);
Log.d("tag","============positioj===="+position)
Log.d("tag", "22222222222222222222222222===" + selectedDistrict);
district.setText(selectedDistrict);
}
});
and it is the adapter Method
public static void setAutoCompleteTextViewAdapter(Context context, ArrayList<String> arrayList,
AutoCompleteTextView autoCompleteTextView) {
int layoutItemId = android.R.layout.simple_dropdown_item_1line;
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, layoutItemId, arrayList);
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setThreshold(0);
}
// have you increased the size of Threshold
public static void setAutoCompleteTextViewAdapter(Context context, ArrayList<String> arrayList,
AutoCompleteTextView autoCompleteTextView) {
int layoutItemId = android.R.layout.simple_dropdown_item_1line;
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, layoutItemId, arrayList);
autoCompleteTextView.setAdapter(adapter);
// here you have to change the code
autoCompleteTextView.setThreshold(3);
}
Make Custom textview for show selected item text, have look
list_view_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<TextView
android:id="#+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Item name here..."
android:textSize="15dp" />
</RelativeLayout>
now set adapter like this:
myAdapter = new YourAdapter(this, R.layout.list_view_row_item, list);
myAutoComplete.setAdapter(myAdapter);
Now implement itemClick listener like this:
myAutoCompleteText.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
RelativeLayout rl = (RelativeLayout) arg1;
TextView tv = (TextView) rl.getChildAt(0);
myAutoComplete.setText(tv.getText().toString());
}
});
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);
In the wake of a question asked yesterday, I am still attempting to write to little app without any xml whatsoever. Here is my attempt (origin code here), that works, but fails because of "Resources$NotFoundException" if the two commented out lines replace the two lines before them:
public class SpinnerExample extends Activity {
private Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
spinner = new Spinner(this);
linearLayout.addView(spinner);
List<String> list = new ArrayList<String>();
list.add("Android");
list.add("Java");
list.add("Spinner Data");
list.add("Spinner Adapter");
list.add("Spinner Example");
TextView textView = new TextView(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
//ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 0x7f030012, list);
CheckedTextView checkedTextView = new CheckedTextView(this);
checkedTextView.setId(0x7f030022); // arbitrary value
dataAdapter.setDropDownViewResource(android.R.layout.spinner_dropdown);
//dataAdapter.setDropDownViewResource(0x7f030012);
spinner.setAdapter(dataAdapter);
}
// Add spinner data
public void addListenerOnSpinnerItemSelection(){
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
}
The xml resources refer to:
android.R.layout.simple_spinner_item:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
android.R.layout.spinner_dropdown:
<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:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
Can somebody explain to may why this exception occurs and how to prevent it and have an app without xml?
Thanks in advance.
UPDATE: added the suggestion of 323go and made this class:
public class MyArrayAdapter<T> extends ArrayAdapter<T> {
List<T> list;
public MyArrayAdapter(Context context, int resource, List<T> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
list = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView tv = new CheckedTextView( this.getContext() );
tv.setText( "This is item " + position );
return tv;
}
}
...and obviously replacede ArrayAdapter with MyArrayAdapter in the original code. The result is that the spinner is shown with "This is item 0", but still gives the original exception is you tap on the spinner.
UPDATE-2
The question is half solved, I can get rid of the first xml reference with this ArrayAdapter:
public class MyArrayAdapter<T> extends ArrayAdapter<T> {
List<T> list;
public MyArrayAdapter(Context context, int resource, List<T> objects) {
super(context, resource, objects);
list = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView tv = new CheckedTextView( this.getContext() );
tv.setText(list.get(position).toString());
return tv;
}
}
Now the code in SpinnerExample can be modified like this:
TextView textView = new TextView(this);
MyArrayAdapter<String> dataAdapter = new MyArrayAdapter<String>(this, textView.getId(), list);
New is that I learned that Android attributes an id to a widget created in code as well. So no need for this:
textView.setId(0x7f030012); // arbitrary int
You're going about it the wrong way. XML or not, you still need IDs if you want to access elements by ID. Hardcoding IDs is not a good idea, and there's nothing wrong using the android.R.id... constants.
Further, the reason why your code fails is that setDropDownViewResource() expects the id of a layout, not of an actual view. A layout will tell Android what views to create through the LayoutInflater. If your code worked, then the same view would be referenced by id for each of the Spinner rows.
If you want to completely handle the UI creation yourself, you'll need to override the ArrayAdapter so that getView returns the view for each element. It might be easier, however, to just use the android.R layouts, unless you need something different.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView( context, parent );
tv.setText( "This is item " + position );
return tv;
}
I would like to change text color of my spinner (I want to have the chosen value to be white).
I have red about this topic on this forum, but it doesnt helped me. I have created the layout xml file for my spinner (spin.xml). Here is what I have:
spin.xml :
<?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="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="#ffffff" />
Array adapter in my onCreate() :
spinner = (Spinner) findViewById(R.id.shift);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.shiftarray, R.layout.spin);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View view, int pos, long id) {
selected = spinner.getSelectedItem().toString();
((TextView) spinner.getChildAt(0)).setTextColor(1);
Log.e("SELECT", selected);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
What should I do, to make it work please?
Than you. :)
Simple and effective...
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
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
}
});