I want to perform some actions on the appearance of a DropDown spinner.
I cannot find any way to get "onShowListener"
Is there such a listener ?
You can achieve this implementing your own SpinnerAdapter.
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Hide the keyboard here
}
This method is only invoked when the spinner appears.
You can find an example of a custom SPinnerAdapter here
Easiest solution would be to use the OnTouchListener. But maybe it gives you some unwanted side effects, because it gets also called, when you click on a list item.
BTW You can't set an OnClickListener, because it gives this Exception:
"java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead"
spinner.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Hide your Keyboard
return false;
}
});
setOnItemSelectedListener is available which will give you which item is selected
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Have you tried adding setOnClickListener and then check if spinner isShown or not
Related
I have list view with two actions one is for new activity and other one is when I long press the list item delete but the item will not delete mean while when I long press the new activity will be opened automatically..how to solve this issue
You have to set setOnItemLongClickListener() and setOnItemClickListener() in the ListView:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos: " + pos);
return true;
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("Hello!", "Y u no see me?");
}
});
Make sure your onLongClick retrun true ,true means that the event is consumed. It is handled. No other click events will be notified.
#Override
public boolean onLongClick(View view) {
return true; // or false
}
or add in xml
<ListView android:longClickable="true">
or in java class
listView.setLongClickable(true)
You can also manage multiple touch my implementing your custom touch listener. Please refers this link for detail.
Elapsed time between first and second ACTION_DOWN
Here i have a spinner and some text fields below the spinner. when one of the text field has focus, i select an item from spinner and i see the focus is still on that text field, Now what i want to do is, on spinner item selected i want to change focus from that text field to the spinner.
Is there any way to set focus to spinner?like,
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//set focus to the spinner
}
}
I had a similar problem and found part of the answer here.
However, I also had to set focusable(true) and focusableInTouchMode(true) from my code and not the XML file. I couldn't get it to work until I set the focusable properties in the code. Here is a sample from my project:
spinUoM.setFocusable(true);
spinUoM.setFocusableInTouchMode(true);
spinUoM.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
DialogDefineRecipeActivity.this.spinUoM.performClick();
}
});
worked in my case doing
#Override
public void onItemSelected(final AdapterView<?> parent, View view,
final int position, long id) {
parent.post(new Runnable() {
#Override
public void run() {
spinner.requestFocusFromTouch();
}
});
}
I'd like to have both type of clicks on a listView - onClick and LongClick.
I've implemented it like this:
this.listViewSub = (ListView) this.findViewById(R.id.listsub);
this.listViewSub.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(final AdapterView parent, final View view, final int position,
final long id) { ... } });
// listen to long click - to share texts
this.listViewSub.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) { ... } });
But it does't fire the Long Click.
Anyone has any idea why?
You have to enable the LongClickable
list.setLongClickable(true);
and
list.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
}
});
#Vadim, are your listview's adapter is extends from BaseAdapter? if yes, then also need to set convertView.setLongClickable(true); in the getView().
For me, I had to set android:longClickable="true" in the XML file that contains my ListView row layout (not ListView layout) for the item to be long-clickable.
onLongClick returns true if the callback consumed the long click, false otherwise. So if the event is handled by this method, return true.
I have a spinner in my Android app, and its onItemSelected() event automatically gets triggered upon entering the activity.
How do I avoid this?
We can use a flag, and just enable it when the spinner is really touched.
private boolean isSpinnerTouched = false;
spinner.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
isSpinnerTouched = true;
return false;
}
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View arg1,
int arg2, long arg3) {
if (!isSpinnerTouched) return;
// do what you want
}
});
To add on Jerry Abraham, You should clear selection before enabling setOnItemSelectedListener
Spinner mSpinner=(Spinner)findViewById(R.id.mySpinner);
int initialSelectedPosition=mSpinner.getSelectedItemPosition();
mSpinner.setSelection(initialSelectedPosition, false); //clear selection
mSpinner.setOnItemSelectedListener(this); //set listener after clearing section
I have solved this issue,
You can avoid this issue by not setting any default values to the spinner
int initialposition=spinner.getSelectedItemPosition();
spinner.setSelection(initialposition, false);
This will avoid entering into onItemSelected()
There are no any way to avoid this.
You may add some flag, indicating readiness of your application and use it in your onItemSelected() method to decide, what to do in each case.
Well, you can add a dummy selection to the initial adapter, and ignore position number in the setOnItemSelectedListener. It's not pretty but it works. See this code for setting up the items for an array adapter.
List<String> names = new ArrayList<String>();
names.add("");
names.addAll(realValues);
Then in your setOnItemSelectedListener you can do this:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (position > 0)
{
String name = names.get(position - 1);
}
else
{
Log.d(TAG, "selected nothing or perhaps the dummy value");
}
}
I have found a solution for this problem and posted it here (with code sample):
Spinner onItemSelected() executes when it is not suppose to
Simple and easy is this...
validate with a boolean to see if is first time...
Spinner mySpinner = (Spinner)findViewById(R.id.spinner_xml_pro);
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(isSpinnerInitial){ // globar var boolean isSpinnerInitial = false;
//do something
}else
isSpinnerInitial=true;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Check this with spinner.post(new Runnable()...)
or this other my source
I think that you can use spinner position which is a better approach in my opinion.
Create a global variable where you store the spinner position, in onItemSelected method the position is provided you can compare them, if they are the same do not make an action.
private int spinnerPosition; \\ Global variable
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if(spinnerPosition != position){
// Do whatever you like
// Do not forget to save the new position
spinnerPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
You can avoid it by ignoring the first click by,
private boolean isSpinnerInitial = true; //As global variable
public void onItemSelected(xxx xxx, xxx xxx, xxx xxx, xxx xxx) {
if(isSpinnerInitial) {
isSpinnerInitial = false;
return;
}
// Write your code here
}
what I'm trying to do is make a selection from a spinner in android and then whatever is selected to be added to an edittext box. The code I have so far is this...
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
edittext.setText("");
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
//add some code here
}
);
Problem is this seems to be run even before the spinner is selected so it always sets my edittext to "". Ideally I would like to have it set the text to the selection made in the spinner. So, anyone have any ideas?
At startup, your spinner will get its defaultvalue, that counts as a selection.
Do a boolean FirstTime or something like that.
You probably initialize your spinner from some array or something?
The function actually looks like this
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id);
So just use the position variable
{
edittext.setText(myArray[position]);
}
You can use the getItem method in the adapter to get the object that is shown. Like this:
onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editText.setText((String) adapter.getItem(position));
}