Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save umesh0492/06dc848ef2a6d7174607 to your computer and use it in GitHub Desktop.
Save umesh0492/06dc848ef2a6d7174607 to your computer and use it in GitHub Desktop.
Used to make your EditText a better option than Spinners
public class ClickToSelectEditText<T extends Listable> extends EditText {
List<T> mItems;
String[] mListableItems;
CharSequence mHint;
OnItemSelectedListener<T> onItemSelectedListener;
public ClickToSelectEditText(Context context) {
super(context);
mHint = getHint();
}
public ClickToSelectEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mHint = getHint();
}
public ClickToSelectEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mHint = getHint();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ClickToSelectEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mHint = getHint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setFocusable(false);
setClickable(true);
}
public void setItems(List<T> items) {
this.mItems = items;
this.mListableItems = new String[items.size()];
int i = 0;
for (T item : mItems) {
mListableItems[i++] = item.getLabel();
}
configureOnClickListener();
}
private void configureOnClickListener() {
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(mHint);
builder.setItems(mListableItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int selectedIndex) {
setText(mListableItems[selectedIndex]);
if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelectedListener(mItems.get(selectedIndex), selectedIndex);
}
}
});
builder.setPositiveButton(R.string.dialog_close_button, null);
builder.create().show();
}
});
}
public void setOnItemSelectedListener(OnItemSelectedListener<T> onItemSelectedListener) {
this.onItemSelectedListener = onItemSelectedListener;
}
public interface OnItemSelectedListener<T> {
void onItemSelectedListener(T item, int selectedIndex);
}
}
public interface Listable {
String getLabel();
}
@InjectView(R.id.signup_text_input_job_category)
ClickToSelectEditText<JobCategory> editTextJobCategory;
...
editTextJobCategory.setItems(jobCategories);
editTextJobCategory.setOnItemSelectedListener(new ClickToSelectEditText.OnItemSelectedListener<JobCategory>() {
@Override
public void onItemSelectedListener(JobCategory item, int selectedIndex) {
selectedJobCategory = item;
}
});
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.your.package.ClickToSelectEditText
android:id="@+id/signup_text_input_job_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_job_category" />
</android.support.design.widget.TextInputLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment