I was trying to create a spinner which display a list of data from array list.
When I clicked on the dropdown it displays the list, but when I clicked on an item inside the dropdown list it doesn't show up the value on the spinner.
Am I missing something here?
Note: Yesterday I have tried to check using Log.d() and System.out.println, itemOnSelected() doesn't but today it works fine. Maybe I rebuilding the project or I have changed something in the code but the value on the spinner still doesn't show up after I clicked on the item inside the spinner.
Spinner spnSubjectIDInfo;
ArrayList<String> subjectList;
ArrayAdapter<String> adpSubj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subjectList = new ArrayList<String>();
subjectList .add("John");
subjectList .add("Maxi");
subjectList .add("Jeni");
spnSubjectIDInfo = (Spinner) v.findViewById(R.id.spnSubjectIDInfo);
adpSubj = new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_spinner_item, subjectList);
adpSubj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnSubjectIDInfo.setAdapter(adpSubj);
spnSubjectIDInfo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
XML
<Spinner
android:id="#+id/spnSubjectIDInfo"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:layout_centerHorizontal="true" />
Try this
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ActivityName.this, subjectList.get(position).toString() " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
You are passing the wrong context to Toast instance:
If your code is in Activity then replace this by YourActivity.this or if it's in Fragment then use getActivity() or use Application context getApplicationContext(). Because currently, this is representing Spinner's onItemSelected listener context.
Do :
Toast.makeText(/*Your activity/application context*/, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
instead of:
Toast.makeText(this, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
Change the "this" to "ActvityName.this" or use "getApplicationContext()" in the Toast, you are passing anonymous class context in the toast.
Like below:
spnSubjectIDInfo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ActivityName.this,parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Hope this will help you.
public class SpinnerTest extends AppCompatActivity {
private ArrayList<String> subjectList;
private ArrayAdapter<String> adpSubj;
private Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
spinner= (Spinner) findViewById(R.id.spnSubjectIDInfo);
subjectList = new ArrayList<String>();
subjectList .add("John");
subjectList .add("Maxi");
subjectList .add("Jeni");
adpSubj = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, subjectList);
adpSubj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adpSubj);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), spinner.getItemAtPosition(position).toString() + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
in Spinner setOnItemSelectedListener methord not work. if you want to get selected value show in toast use this line
String Text = mySpinner.getSelectedItem().toString();
Toast.makeText(this,Text,Toast.LENGTH_LONG).show();
it will return the selected value and will be displayed on Toast.
After few more searching I realized that I have to set the width size of the spinner to wrap_content as the length of the value doesn't support to display it on the spinner after the item being clicked at.
<Spinner
android:id="#+id/spnSubjectIDInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:layout_centerHorizontal="true" />
Related
I want to use spinner prompt like "Select City". I did set a value for select_city in string layout use in layout android:prompt="#string/select_city" but it did not work. Also tried to use sp_city.setPrompt("Select City"); also did not work.
What is my problem? How can I fix it and set the prompt?
Layout:
<Spinner
android:id="#+id/spinner_poi_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
Class:
public class FirstPOIPreference extends DialogPreference {
private Spinner sp_city;
private ArrayAdapter<String> adapter;
private POI poiDialog = new POI();
public FirstPOIPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setPersistent(false);
setDialogLayoutResource(R.layout.pref_poi_dialog);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
#Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
initViews(view);
}
private void initViews(View view) {
sp_city = (Spinner) view.findViewById(R.id.spinner_poi_city);
sp_city.setPrompt("City");
String[] arrayCity = new String[]{"Erie", "Pittsburgh", "Cleveland", "Buffalo", "Niagara Falls", "Jamestown"};
adapter = new ArrayAdapter <> (this.getContext(), android.R.layout.simple_spinner_dropdown_item, arrayCity);
sp_city.setAdapter(adapter);
sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView <?> parentView, View selectedItemView, int position, long id) {
poiDialog.setCity(sp_city.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView <?> parentView) {
}
});
}
}
you can do it this way, i have done it as some tricky way.it helps you
private void initViews(View view) {
sp_city = (Spinner) view.findViewById(R.id.spinner_poi_city);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (position == getCount()) {
((TextView) v.findViewById(android.R.id.text1)).setText("");
((TextView) v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
}
return v;
}
#Override
public int getCount() {
return super.getCount() - 1; // you dont display last item. It is used as hint.
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("Erie");
adapter.add("Pittsburgh");
adapter.add("Cleveland");
adapter.add("Buffalo");
adapter.add("Niagara Falls");
adapter.add("Jamestown");
adapter.add("Select City"); //This is the text that will be displayed as hint.
sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String spinnerValue = String.valueOf(sp_city.getSelectedItem().toString());
if (!spinnerValue.equalsIgnoreCase("Select City")) {
//do your code
Toast.makeText(this, sp_city.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
sp_city.setAdapter(adapter);
sp_city.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
}
I think better solution is to use "Select City" as a first item of the array thats you will add to spinner. So by default it will show Select city. And you can check if city is selected or not by checking spinner selected position. If position is 0 show toast or something like "Please select city."
You are mistaking prompt with default text.
Tap on the spinner and you will see "select City" as title
Prompt is used to show title on dropdown popup not for default text.
If you want to select the default value on spinner
when you have not selected any value from spinner dropdown.
Then you must use
NothingSelectedSpinnerAdapter
How to make an Android Spinner with initial text "Select One"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_listview);
myspinner = (Spinner)findViewById(R.id.spinnertype);
String provider = myspinner.getSelectedItem().toString();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, type_arrays,R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setAdapter(adapter);
myspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(parent.getContext(), "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
i tried different kinds of toast but still it wont appear. what went wrong?
I viewed some posts and it says i cant use onclicklistener to a spinner
Have yout try this, that might help you
Toast.makeText(getApplicationContext(), "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
or
Toast.makeText(YourActivity.this, "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
Try using getContext() instead of getApplicationContext.
Ok, Let's check the codes again ;
Like i said, You'll need to use the current Activity Context.
For example, My Activity name is, MainActivity so, Use:
MainActivity.this This should be the context of that Activity.But i
think if you use your own code(parent.getContext()) inside the another
activity, It won't show up because it's somewhere else actually you
know
Also, You did something wrong here i guess (Something similiar : ArrayAdapter.createFromResource issue)
I just quote the answer:
Query the data, put it in a List or Array and use this constructor of
ArrayAdapter:
ArrayAdapter<CharSequence> dataAdapter1 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, yourArrayOrList);
Update: So here it is:
Spinner myspinner = (Spinner)findViewById(R.id.spinnertype);
String provider = myspinner.getSelectedItem().toString();
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this, type_arrays, R.layout.support_simple_spinner_dropdown_item);
/*ArrayAdapter.createFromResource(this, type_arrays,R.layout.support_simple_spinner_dropdown_item);*/
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setAdapter(adapter);
myspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(MainActivity.this, "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Now it should work!
I'm new to Android development, I having some problem which unable to resolve.
I tried to implement a Spinner inside Fragment, now it having problems with the spinner.setOnItemClickListener() method,
It showing an error message:
"setOnItemClickListener
(android.widget.AdapterView.OnItemClickListener) in Spinner cannot be
applied to (anonymous
android.widget.AdapterView.OnItemSelectedListener)
Here is my code:
public class CheckAvailabilityFragment extends Fragment {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_check_availability, container,false);
spinner = (Spinner)getView().findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(),R.array.venue,android.R.layout.simple_list_item_1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemClickListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return rootView;
}
}
The error message tells you that setOnItemClickListener() takes a parameter of type OnItemClickListener. However, you are giving it a OnItemSelectedListener. Since Spinner doesn't support OnItemClickListener, you probably mean to call onItemSelectedListener() which takes a OnItemSelectedListener as its parameter.
setOnItemClickListener will not works with spinner as been said at Spinner SetOnItemClickListener:
A spinner does not support item click events. Calling this method will
raise an exception.
Instead use
setOnItemSelectedListener(AdapterView.OnItemSelectedListener).
Try using the following:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
use getActivity() instead of getBaseContext()
Is there a reason you want getBaseContext()? You should be able to just pass getActivity() as the first parameter of createFromResource()
This should be simple but I'm having a lot of trouble with an AutoCompleteTextView having it's OnItemSelected method being called.
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {
private List<Contact> contactsList = new ArrayList<>();
private List<String> forAutoComplete = new ArrayList<>();
private List<Contact> selectedList = new ArrayList<>();
AutoCompleteTextView textView;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, forAutoComplete);
textView = (AutoCompleteTextView)
findViewById(R.id.editText);
textView.setAdapter(adapter);
textView.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
textView.setText("");
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText( this,
"Nothing selected",
Toast.LENGTH_SHORT).show();
textView.setText("");
}
None of my toasts are being called.
Thanks.
Althought in docs I noticed that it is valid to use onItemSelectedListener.
For AutoComplete TextView, you should use onItemClickListener, since it's more correct than other, as the ItemSelect is more specified for ListViews.
Check this tutorial on how to use it. Dont forget to initialize your Listener!
I want a Spinner which contains last Item as "Add more items"
and when i click on it, then i can add next item. The item i have added should get displayed in spinner list and will have same last item as "Add more items"..
I tried using Adapter but how can i keep last element as "Add more items"
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == spinner.getItemIdAtPosition(spinner.getCount()))
// my code for adding item to list using Adapter
else
// spinner.setSelection();
}
did i wrote anything wrong??
any help??..Thanks ...
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownVerticalOffset="2dp"
android:dropDownWidth="500sp"
android:spinnerMode="dropdown" />
</RelativeLayout>
MainActivity.java :
public class MainActivity extends Activity {
private Spinner spinner;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> data = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
data.add("item " + i);
}
data.add("Add New Item");
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
tv.setText(spinner.getSelectedItem().toString());
if (spinner.getSelectedItem().toString().equals("Add New Item")) {
data.remove(position);
data.add(position, "item " + position);
data.add((position + 1), "Add New Item");
updateAdapter(data);
Toast.makeText(getApplicationContext(), "item " + position,
Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void updateAdapter(ArrayList<String> data) {
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
data);
adapter.notifyDataSetChanged();
}
}