Click on spinner element android - java

I created a spinner in my main.xml:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/categoria_arrays"
android:prompt="#string/categoria_prompt" />
And in the strings.xml the values:
<string name="categoria_prompt">Choose</string>
<string-array name="categoria_arrays">
<item>All</item>
<item>One</item>
<item>Two</item>
<item>Three</item>
</string-array>
I can display it normally but actually there are no interactions.. I need that onClick over a item open a new activity for example. So if i click the item at position 2 i need go in the activity One. Is it possible?
I tried to create a toast when i click a item but not works:
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " +
parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
//HERE CHANGE ACTIVITY
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
I How could i do?

Simply call something like this in your onCreate after your setContentView:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new 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();
//HERE CHANGE ACTIVITY
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Yes it is possible. Do it like this
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
if (pos == 1){
Intent i = new Intent(currentActivity.this, One.class);
startActivity(i);
}else if (pos == 2)
{
Intent i = new Intent(currentActivity.this, Two.class);
startActivity(i);
}else if (pos == 3){
Intent i = new Intent(currentActivity.this, Three.class);
startActivity(i);
}
}
and so on...

You need to set item selected listener like this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

You need to explicitly set the listener for the spinner.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
// your code here
}
public void onNothingSelected(AdapterView<?> arg0) {
// your code here
}
});

Did you just added onItemSelected method, or setted onItemSelectListener? onItemSelected and onNothingSelected are just methods, you need to use onItemSelectListener.

Related

spinner setOnItemClickListener is crash?

holder.spinner_teacher_name=(Spinner) rowView.findViewById(R.id.spinner_teacher_name);
holder.spinner_teacher_name=(Spinner) rowView.findViewById(R.id.spinner_teacher_name);
arrayAdapter=new ArrayAdapter<String>(rowView.getContext(), android.R.layout.simple_spinner_item,teacher_name);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spinner_teacher_name.setAdapter(arrayAdapter);
holder.spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(rowView.getContext(), "You Clicked "+class_room[position], Toast.LENGTH_SHORT).show();
}
});
You should be used selected listener, not click listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
String selected_item = parent.getItemAtPosition(pos).toString();
Log.d("TAG", "_log : item_selected : " + selected_item);
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
When the user selects an item from the drop-down, the Spinner object receives an on-item-selected event.
To define the selection event handler for a spinner, implement the AdapterView.OnItemSelectedListener interface and the corresponding onItemSelected() callback method.
See documentation.
Use:
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(rowView.getContext(), "You Clicked " + class_room[position], Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Instead of:
holder.spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(rowView.getContext(), "You Clicked "+class_room[position], Toast.LENGTH_SHORT).show();
}
});

Spinner Value to String

Been making my first app and slow progress is being made. Just wondering how I return a Spinner value to pass it as a string.
Here is my code:
First the event class:
public void event(){
calanderBtn = (Button)findViewById(R.id.eventBtn);
calanderBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(CalendarContract.Events.TITLE, "Home");
intent.putExtra(CalendarContract.Events.DESCRIPTION, "Cleaning : ");
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Home");
startActivity(intent);
}
});
}
Second the Spinner Class:
public void selectcleaning() {
spCleaning = (Spinner) findViewById(spinner);
adapterCleaningType = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cleaningType);
adapterCleaningType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCleaning.setAdapter(adapterCleaningType);
}
I'm trying to select the value from selectcleaning (Value is either Yes or No) and pass it in the descrption in the Calendar of the event class and I'm not sure what to do.
you can use this code to get the value
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
or get the value when spinner selected using OnItemSelectedListener (Android Doc)
public class SpinnerActivity extends Activity implements
OnItemSelectedListener {
#Override
protected void onStart() {
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
You should try using the Overrides for the spinner class as so:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}});
For your purposes 'int position' would equal 0 (yes) or 1 (no)
Try setting an on item selected listener. When an item is selected, you can store that value in a global variable. It gets the first item by default too.
spCleaning.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Value is current spinner item
globalStringVariable = (String) parent.getItemAtPosition(position);
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
#Override
public void onNothingSelected(AdapterView<?> parent) {
// This is default, before you've selected anything.
// It gets the first value.
globalStringVariable = parent.getItemAtPositon(0);
}
});

How to use autocomplete textview

When I fill name and select in auto-complete text-view. It's not working same spinner. function in if-else not working or condition incorrect(I don't know).Please suggest me how to use onitemselectlistener on auto-complete text-view to use function in each condition.
AutoCompleteTextView auto1 = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
String[] word;
word = getResources().getStringArray(R.array.word_name);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, word);
auto1.setAdapter(adapter);
auto1.setOnItemSelectedListener(new OnItemSelectedListener() {
TextView txt1 = (TextView)findViewById(R.id.textView1);
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(arg2==0) // when I fill AED(array 0 in string.xml It's not work)
{
getmoney();
}
else if(arg2==1)
{
getmoney1();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
String.xml
<string-array name="word_name">
<item>AED United Arab Emirates Dirham</item>
<item>AFN Afghan Afghani</item>
<item>ALL Albanian Lek</item>
<item>AMD Armenian Dram</item>
</string-array>
Instead of OnItemSelectedListener, OnItemClickListener works for AutocompleteTextView.
For example:
auto1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
adapter.getItem(position).toString(),
Toast.LENGTH_SHORT).show();
}
});

unable to retrieve value from spinner

This is a very basic issue.
It is that, I have a basic Spinner implemented with a listener as below:
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array, android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(), "You need to select atleast one mode!", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(getBaseContext(), "sensorMode = " + sMode, Toast.LENGTH_SHORT).show();
The issue is the sMode variable result is NOT appearing in the Toast message(value is NULL) where as it works fine within the OnItemSelected method.
Where am I going wrong?
you are executing the Toast.show(); method before the sMode value could be set, instead place this in onItemSelected() after setting sMode value, change your code to following:
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array,
android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
Toast.makeText(getBaseContext(), "sensorMode = " + sMode, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(),
"You need to select atleast one mode!",
Toast.LENGTH_SHORT).show();
}
});
You can do this
Spinner sensModeSpinner = (Spinner) findViewById(R.id.SensorModeSpinner);
ArrayAdapter<CharSequence> sensModeAdapter = ArrayAdapter.createFromResource(this, R.array.sensorMode_array, android.R.layout.simple_spinner_item);
sensModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensModeSpinner.setAdapter(sensModeAdapter);
sensModeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
sMode = parent.getItemAtPosition(pos).toString();
//CALL_YOUR_FUNCTION_HERE
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getBaseContext(), "You need to select atleast one mode!", Toast.LENGTH_SHORT).show();
}
});
some time may be it is silly mistake so try this..
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// sMode = parent.getItemAtPosition(pos).toString();
sMode = parent.getAdapter().getItemAtPosition(pos).toString();
}

Spinner with on Click Listener

I am using spinner that shows error when i am trying to extract the item id of the selected spinner item.
My Code goes here:
public void dispspi()
{
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String> adap= new ArrayAdapter(this, android.R.layout.simple_spinner_item, level);
spinner.setAdapter(adap);
spinner.setOnItemClickListener(new OnItemClickListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int item = spinner.getSelectedItemPosition();
p=item;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
How to get the item id of the spinner? Any help is appreciated..Thanks in advance
IIRC, you should be using a selected listener, not click:
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
Then you can add the override tag to your selected method.
private String selecteditem;
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
selecteditem = adapter.getItemAtPosition(i).toString();
//or this can be also right: selecteditem = level[i];
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String SpinerValue3 = parent.getItemAtPosition(postion).toString();
Toast.makeText(getBaseContext(),
"You have selected 222 : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Yes you can use some OnItemSelectedListener for work with selected item. But sometimes we would like to handle exactly click for spinner. For example hide keyboard or send some analytics etc. In this case we should use TouchListener because OnClickListener doesn't work properly with Spinner and you can get error. So I suggest to use TouchListener like:
someSpinner.setOnTouchListener { _, event -> onTouchSomeSpinner(event)}
fun onTouchSomeSpinner(event: MotionEvent): Boolean {
if(event.action == MotionEvent.ACTION_UP) {
view.hideKeyBoard()
...
}
return false
}
you should have this in the listener(OnItemSelectedListener)
public void onNothingSelected(AdapterView<?> arg0) {
}
It might works without it but put it to be consistent
but there might be other errors also, can you provide the error log ?

Categories