Created
January 6, 2015 19:16
-
-
Save vedant1811/d66fb759464bbe5c30e6 to your computer and use it in GitHub Desktop.
Custom Spinner to differentiate between user selected and prorammatically selected item. Make sure to call correct method to use this feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.AdapterView; | |
/** | |
* Used this to differentiate between user selected and prorammatically selected | |
* Call {@link Spinner#programmaticallySetPosition} to use this feature. | |
* Created by vedant on 6/1/15. | |
*/ | |
public class Spinner extends android.widget.Spinner implements AdapterView.OnItemSelectedListener { | |
OnItemSelectedListener mListener; | |
/** | |
* used to ascertain whether the user selected an item on spinner (and not programmatically) | |
*/ | |
private boolean mUserActionOnSpinner = true; | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | |
if (mListener != null) { | |
mListener.onItemSelected(parent, view, position, id, mUserActionOnSpinner); | |
} | |
// reset variable, so that it will always be true unless tampered with | |
mUserActionOnSpinner = true; | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
if (mListener != null) | |
mListener.onNothingSelected(parent); | |
} | |
public interface OnItemSelectedListener { | |
/** | |
* <p>Callback method to be invoked when an item in this view has been | |
* selected. This callback is invoked only when the newly selected | |
* position is different from the previously selected position or if | |
* there was no selected item.</p> | |
* | |
* Impelmenters can call getItemAtPosition(position) if they need to access the | |
* data associated with the selected item. | |
* | |
* @param parent The AdapterView where the selection happened | |
* @param view The view within the AdapterView that was clicked | |
* @param position The position of the view in the adapter | |
* @param id The row id of the item that is selected | |
*/ | |
void onItemSelected(AdapterView<?> parent, View view, int position, long id, boolean userSelected); | |
/** | |
* Callback method to be invoked when the selection disappears from this | |
* view. The selection can disappear for instance when touch is activated | |
* or when the adapter becomes empty. | |
* | |
* @param parent The AdapterView that now contains no selected item. | |
*/ | |
void onNothingSelected(AdapterView<?> parent); | |
} | |
public void programmaticallySetPosition(int pos, boolean animate) { | |
mUserActionOnSpinner = false; | |
setSelection(pos, animate); | |
} | |
public void setOnItemSelectedListener (OnItemSelectedListener listener) { | |
mListener = listener; | |
} | |
public Spinner(Context context) { | |
super(context); | |
super.setOnItemSelectedListener(this); | |
} | |
public Spinner(Context context, int mode) { | |
super(context, mode); | |
super.setOnItemSelectedListener(this); | |
} | |
public Spinner(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
super.setOnItemSelectedListener(this); | |
} | |
public Spinner(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
super.setOnItemSelectedListener(this); | |
} | |
public Spinner(Context context, AttributeSet attrs, int defStyle, int mode) { | |
super(context, attrs, defStyle, mode); | |
super.setOnItemSelectedListener(this); | |
} | |
} |
You should call programmaticallySetPosition
Hey,
Thanks for quick reply.
Should I've to do anything else except use this method?
Thank You,
Jimmy Trivedi
…________________________________
From: Vedant Agarwala <[email protected]>
Sent: Tuesday, July 23, 2019 17:27
To: vedant1811 <[email protected]>
Cc: Jimmy Trivedi <[email protected]>; Comment <[email protected]>
Subject: Re: vedant1811/Spinner.java
You should call programmaticallySetPosition
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<https://gist.github.com/d66fb759464bbe5c30e6?email_source=notifications&email_token=AITWX4AZTSOXCBEAH2UQLJDQA3W25A5CNFSM4IGDNK72YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFVYUA#gistcomment-2978112>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AITWX4FELCMPUQFWKV53ANTQA3W25ANCNFSM4IGDNK7Q>.
This is my class:
package com.tekitsolutions.remindme.Utils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
public class CustomSpinnerClass extends androidx.appcompat.widget.AppCompatSpinner implements AdapterView.OnItemSelectedListener {
OnItemSelectedListener mListener;
private boolean mUserActionOnSpinner = true;
public CustomSpinnerClass(Context context) {
super(context);
super.setOnItemSelectedListener(this);
}
public CustomSpinnerClass(Context context, int mode) {
super(context, mode);
super.setOnItemSelectedListener(this);
}
public CustomSpinnerClass(Context context, AttributeSet attrs) {
super(context, attrs);
super.setOnItemSelectedListener(this);
}
public CustomSpinnerClass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setOnItemSelectedListener(this);
}
public CustomSpinnerClass(Context context, AttributeSet attrs, int defStyle, int mode) {
super(context, attrs, defStyle, mode);
super.setOnItemSelectedListener(this);
}
@OverRide
public void setSelection(int position, boolean animate) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
//TODO:-> Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
@OverRide
public void setSelection(int position) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
//TODO:-> Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
@OverRide
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (mListener != null) {
mListener.onItemSelected(parent, view, position, id, mUserActionOnSpinner);
}
// reset variable, so that it will always be true unless tampered with
mUserActionOnSpinner = true;
}
@OverRide
public void onNothingSelected(AdapterView<?> parent) {
if (mListener != null)
mListener.onNothingSelected(parent);
}
public void programmaticallySetPosition(int pos, boolean animate) {
mUserActionOnSpinner = false;
setSelection(pos, animate);
}
public void setOnItemSelectedListener(OnItemSelectedListener listener) {
mListener = listener;
}
public interface OnItemSelectedListener {
/**
* <p>Callback method to be invoked when an item in this view has been
* selected. This callback is invoked only when the newly selected
* position is different from the previously selected position or if
* there was no selected item.</p>
* <p>
* Impelmenters can call getItemAtPosition(position) if they need to access the
* data associated with the selected item.
*
* @param parent The AdapterView where the selection happened
* @param view The view within the AdapterView that was clicked
* @param position The position of the view in the adapter
* @param id The row id of the item that is selected
*/
void onItemSelected(AdapterView<?> parent, View view, int position, long id, boolean userSelected);
/**
* Callback method to be invoked when the selection disappears from this
* view. The selection can disappear for instance when touch is activated
* or when the adapter becomes empty.
*
* @param parent The AdapterView that now contains no selected item.
*/
void onNothingSelected(AdapterView<?> parent);
}
}
…________________________________
From: Jimmy Trivedi <[email protected]>
Sent: Tuesday, July 23, 2019 17:31
To: vedant1811 <[email protected]>; vedant1811 <[email protected]>
Cc: Comment <[email protected]>
Subject: Re: vedant1811/Spinner.java
Hey,
Thanks for quick reply.
Should I've to do anything else except use this method?
Thank You,
Jimmy Trivedi
________________________________
From: Vedant Agarwala <[email protected]>
Sent: Tuesday, July 23, 2019 17:27
To: vedant1811 <[email protected]>
Cc: Jimmy Trivedi <[email protected]>; Comment <[email protected]>
Subject: Re: vedant1811/Spinner.java
You should call programmaticallySetPosition
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<https://gist.github.com/d66fb759464bbe5c30e6?email_source=notifications&email_token=AITWX4AZTSOXCBEAH2UQLJDQA3W25A5CNFSM4IGDNK72YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFVYUA#gistcomment-2978112>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AITWX4FELCMPUQFWKV53ANTQA3W25ANCNFSM4IGDNK7Q>.
No nothing else besides that method. In the callback you'll get the boolean userSelected
param correctly.
This class looks exactly like mine :D
It'll be better if you just share the changes, ideally by fork
ing my gist.
Ofcourse, I copied content from your class only. Actually I already created this class for setSelection method, and for onSelectedItem I copied your code.
But my problem is more complicated. I have add and edit case for setSpinnerSpinner method. Add case working fine. For editCase when I click on spinner list item so one dialog will open, now in dialog have two ok and cancel button , if I click it cancel, it should set previously selected position[FIRST POSITION], not the latest one which I came from, because user selected cancel button. That also I did it by storing into temp variable. But now again I open dialogue by clicking[SECOND POSITION] another list inside spinner, then open dialog then ok button. Now again select another item,[THIRD POSITION] then open dialog and click cancel, that time it is showing first time selected position, not the second time position
…________________________________
From: Vedant Agarwala <[email protected]>
Sent: Tuesday, July 23, 2019 7:41:00 PM
To: vedant1811 <[email protected]>
Cc: Jimmy Trivedi <[email protected]>; Comment <[email protected]>
Subject: Re: vedant1811/Spinner.java
No nothing else besides that method. In the callback you'll get the boolean userSelected param correctly.
This class looks exactly like mine :D
It'll be better if you just share the changes, ideally by forking my gist.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<https://gist.github.com/d66fb759464bbe5c30e6?email_source=notifications&email_token=AITWX4F2UTFC5AOX2LJV2JDQA4GPJA5CNFSM4IGDNK72YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFVY4O#gistcomment-2978247>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AITWX4B6YBUAOVQ6IK6BJG3QA4GPJANCNFSM4IGDNK7Q>.
should the spinner object be created by actual Spinner class or the CustomSpinner class?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just tell me, that If I do setSelection it automatically call onItemSelected, now to prevent this what should I do?