Skip to content

Instantly share code, notes, and snippets.

@harish81
Created June 10, 2020 09:50
Show Gist options
  • Save harish81/33648f174f94de53058c44d4f8e87377 to your computer and use it in GitHub Desktop.
Save harish81/33648f174f94de53058c44d4f8e87377 to your computer and use it in GitHub Desktop.
Click to select edittext, alternative to spinner
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ClickToSelectEditText">
<attr name="android:entries"/>
</declare-styleable>
</resources>
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatEditText;
import java.util.List;
public class ClickToSelectEditText<T extends ClickToSelectEditText.Listable> extends AppCompatEditText {
public static interface Listable {
String getLabel();
}
List<T> mItems;
CharSequence[] mListableItems;
CharSequence mHint;
OnItemSelectedListener<T> onItemSelectedListener;
public ClickToSelectEditText(Context context) {
this(context,null);
}
public ClickToSelectEditText(Context context, AttributeSet attrs) {
this(context, attrs, androidx.appcompat.R.attr.editTextStyle);
}
public ClickToSelectEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mHint = getHint();
TypedArray typeAttr = context.obtainStyledAttributes(attrs,R.styleable.ClickToSelectEditText);
try {
mListableItems = typeAttr.getTextArray(R.styleable.ClickToSelectEditText_android_entries);
if(mListableItems!=null){
configureOnClickListener();
}
}finally {
typeAttr.recycle();
}
}
@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("Close", null);
builder.create().show();
}
});
}
public void setOnItemSelectedListener(OnItemSelectedListener<T> onItemSelectedListener) {
this.onItemSelectedListener = onItemSelectedListener;
}
public interface OnItemSelectedListener<T> {
void onItemSelectedListener(T item, int selectedIndex);
}
}
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.your-package-name.ClickToSelectEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Marital Status"
android:entries="@array/maritial_status"
/>
</com.google.android.material.textfield.TextInputLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment