Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smithaaron/d2acd57937d7a4201a79 to your computer and use it in GitHub Desktop.
Save smithaaron/d2acd57937d7a4201a79 to your computer and use it in GitHub Desktop.
Used to make your EditText a better option than Spinners
public class ClickToSelectEditText <T> extends AppCompatEditText {
CharSequence mHint;
OnItemSelectedListener<T> onItemSelectedListener;
ListAdapter mSpinnerAdapter;
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();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setFocusable(false);
setClickable(true);
}
public void setAdapter(ListAdapter adapter) {
mSpinnerAdapter = adapter;
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.setAdapter(mSpinnerAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int selectedIndex) {
if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelectedListener((T)mSpinnerAdapter.getItem(selectedIndex), selectedIndex);
}
}
});
builder.setPositiveButton(android.R.string.cancel, null);
builder.create().show();
}
});
}
public void setOnItemSelectedListener(OnItemSelectedListener<T> onItemSelectedListener) {
this.onItemSelectedListener = onItemSelectedListener;
}
public interface OnItemSelectedListener<T> {
void onItemSelectedListener(T item, int selectedIndex);
}
}
@InjectView(R.id.signup_text_input_job_category)
ClickToSelectEditText<JobCategory> editTextJobCategory;
...
//Adapter must implement ListAdapter (e.g. extend ArrayAdapter<MyModel>)
JobCategoryAdapter adapter = new JobCategoryAdapter(getContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
mJobCategoryList);
editTextJobCategory.setAdapter(adapter);
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:drawableRight="@drawable/ic_arrow_down"/>
</android.support.design.widget.TextInputLayout>
@mcelotti
Copy link

You should add also setLongClickable(false) to avoid paste action:

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setFocusable(false);
        setClickable(true);
        setLongClickable(false);
    }

@rodrigohenriques
Copy link

Nice!

@vsg24
Copy link

vsg24 commented Apr 14, 2017

Actually in my case in order to show selected item, I had to modify onItemSelectedListener to be like:

@Override
      public void onItemSelectedListener(JobCategory item, int selectedIndex) {
          editTextJobCategory.setText(item.getSomeText());
      }

@Gizmodo
Copy link

Gizmodo commented Sep 20, 2017

Thank you!

@AditChauhan
Copy link

JobCategory is it a Pojo class ?

@DjangoLC
Copy link

DjangoLC commented Jan 2, 2019

@ihaveafix yes it is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment