show initial value on spinner other than first value in adapter? - android

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
}
});

Related

Spinner selection not showing

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!

How do you create a dropdown answer in android?

I'm looking for something similar to a drop down menu but when you click on it a block of text drops instead of a list of clickable items. Then you should be able to click to close it again when finished. My app is an informational app so, I want to make a list of several of these within a glossary in my app.
Any ideas??
you should use Spinner in android:
Here is the example :
Spinner mSipnner = (Spinner)findViewById(R.id.spin_beneficiary_targetcode);
List<String> categories2 = new ArrayList<String>();
categories2.add("Choose code");
categories2.add("001");
categories2.add("002");
categories2.add("003");
categories2.add("004");
categories2.add("005");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories2);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
////Spinner item selected
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String targetCode_item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + category_item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// attaching data adapter to spinner
mSpinner.setAdapter(dataAdapter);

Spinner dropdown selected value not visible - Android

I'm newbie to Android, i'm trying to design a spinner dropdown widget, the user selected value is not set as selected value in dropdown box [not display in marked area in below image].
My design xml:
<Spinner
android:id="#+id/propertyid"
android:layout_width="fill_parent"
android:layout_marginTop="160dip"
android:padding="230dp"
android:layout_height="60dp"
android:background="#android:drawable/btn_dropdown"
android:prompt="#string/prompt"
android:spinnerMode="dropdown" />
Java code:
private Spinner spin;
List<String> list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
list.add("list 1");
list.add("list 2");
list.add("list 3");
imgPreview = (ImageView) findViewById(R.id.imgPreview);
spin = (Spinner) findViewById(R.id.propertyid);
spin.setOnItemSelectedListener(this);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
spin.setSelection(0);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
spin.setSelection(position);
String item = list.get(position);
System.out.println("&&&&&&----&&&&&&"+position+item);
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
Please anyone help me to get the selection list value displayed
Just remove your
android:padding="230dp"
It is moving your item out of the boundary,
All the rest codes are fine enough.

Styling dropdown portion for Spinner widget

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);

Android - spinner text color change does not work

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) {
}
};

Categories

Resources