Setting a spinner onClickListener() in Android
The following works how you want it, but it is not ideal.
public class Tester extends Activity {
String[] vals = { "here", "are", "some", "values" };
Spinner spinner;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner) findViewById(R.id.spin);
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, vals);
spinner.setAdapter(ad);
Log.i("", "" + spinner.getChildCount());
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
int a = spinner.getCount();
int b = spinner.getChildCount();
System.out.println("Count =" + a);
System.out.println("ChildCount =" + b);
for (int i = 0; i < b; i++) {
View v = spinner.getChildAt(i);
if (v == null) {
System.out.println("View not found");
} else {
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("","click");
}
});
}
}
}
}, 500);
}
}
Let me know exactly how you need the spinner to behave, and we can work out a better solution.
Is there a way to use setOnClickListener with an Android Spinner?
You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:
- Create a custom Spinner
- In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item
- Do a findViewById(android.R.id.text1) to get the TextView
- Now set the onClickListener to the TextView
Android - Spinner + setOnClickListener
I'm doing it like this:
public void addListenerOnSpinnerItemSelection() {
mySpinner = (Spinner) findViewById(R.id.GPU_LAYOUT);
mySpinner.setOnItemSelectedListener(new myOnItemSelectedListener());
}
public class myOnItemSelectedListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long arg3) {
int position=Arrays.asList(getResources().getStringArray(R.array.GPU_LAYOUT)).indexOf(fecha); }
@Override
public void onNothingSelected(AdapterView<?> arg0) { }
}
Also I suggest to get the position of the group you're using and then handle it.
set onClickListener for spinner item?
You SHOULD NOT call OnItemClickListener
on a spinner. A Spinner does not support item click events. Calling this method will raise an Exception. Check this.
You can apply OnItemSelectedListener
instead.
Edit :
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Add new category"))
{
// do your stuff
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
As far as adding "Add new category" to the end of the list is concerned, I think you should better go for custom adapter in which after adding all your items, you can add that constant ("Add new category") to end of array so that it should come last always.
How to set onClickListener() for view of Spinner?
What they might mean is to Override
the setOnItemClickListener
and then call that in the constructor. So in your mySpinner
class you would need to add: [notice it is called on ITEM click listener, that might also be part of your issue]
@Override
public void setOnItemClickListener(
android.widget.AdapterView.OnItemClickListener l) {
super.setOnItemClickListener(l);
//... do action here that you want to happen when item in spinner is clicked
}
Dunno if that will fix your issue but I hope it helps. Good Luck.
also it might be worthwhile to possibly use
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//do stuff
}
OnClickListener to Spinner ? ??
you can't add an onclicklistener on any adapter view try to use setOntouchlistener hope it will help you
spinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.gyerekek_array, R.layout.my_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnTouchlistener(this);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new ItemChooser());
//////////////////////////// your listener
public boolean onTouch(View v, MotionEvent event) {
Log.d("Spinner","clicked");
return false;
}
How to capture onClick event in Android for a spinner
Instead of setting the spinner's OnClickListener,try setting OnTouchListener and OnKeyListener.
spinner.setOnTouchListener(spinnerOnTouch);
spinner.setOnKeyListener(spinnerOnKey);
and the listeners:
private View.OnTouchListener spinnerOnTouch = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//Your code
}
return false;
}
};
private static View.OnKeyListener spinnerOnKey = new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
//your code
return true;
} else {
return false;
}
}
};
Related Topics
How to Make Edittext Not Focused When Creating Activity
Android Studio Build Gradle Outofmemoryerror
Error: Gradle: Execution Failed for Task ':App:Predexdebug'
Language Detection with Data in Postgresql
Calling Java Methods in JavaScript Code
What Java Xml Library Do You Recommend (To Replace Dom4J)
How to Get the Memory Location of a Object in Java
Prevent Webview from Displaying "Web Page Not Available"
Google Gson Linkedtreemap Class Cast to Myclass
How to Get Selected Xls File Path from Uri for Sdk 17 or Below for Android
Parcelable and Inheritance in Android
Android Call a Method from Another Class
Android Java.Net.Unknownhostexception: Host Is Unresolved (Strategy Question)
R: Cannot Install Rjava; What Is R-Api-3.4
Alternative to Jzebra/Qz Java Raw Print Plugin After Npapi Being Dropped on Chrome Browser