Skip to content

Instantly share code, notes, and snippets.

@logcat
Created August 18, 2015 10:10
Show Gist options
  • Select an option

  • Save logcat/35044e99b9775c7ca983 to your computer and use it in GitHub Desktop.

Select an option

Save logcat/35044e99b9775c7ca983 to your computer and use it in GitHub Desktop.
Based on EditText and ListPopupWindow
import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.ListPopupWindow;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
// XXX AppCompatEditText is used as we want to use it with TextInputLayout and utilize its tinting and hint feature
// style it with @style/Widget.AppCompat.Spinner.Underlined
public class DropDownSpinner extends AppCompatEditText {
private ListPopupWindow listPopupWindow;
private BaseAdapter adapter;
private AdapterView.OnItemSelectedListener onItemSelectedListener;
private ToString converter;
public DropDownSpinner(Context context) {
super(context, null);
init();
}
public DropDownSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DropDownSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
listPopupWindow = new ListPopupWindow(getContext());
listPopupWindow.setAnchorView(this);
listPopupWindow.setPromptPosition(ListPopupWindow.POSITION_PROMPT_ABOVE);
listPopupWindow.setModal(true);
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = adapter.getItem(position);
if (converter == null) {
setText(item.toString());
} else {
setText(converter.convertToString(item));
}
if (onItemSelectedListener != null) {
onItemSelectedListener.onItemSelected(parent, view, position, id);
}
listPopupWindow.dismiss();
}
});
listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (MotionEvent.ACTION_UP == event.getAction()) {
if (listPopupWindow.isShowing()) {
listPopupWindow.dismiss();
} else {
requestFocus();
listPopupWindow.setAdapter(adapter);
listPopupWindow.show();
}
}
return super.onTouchEvent(event);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
setKeyListener(null);
}
}
public <T extends BaseAdapter & ToString> void setAdapter(T adapter, ToString converter) {
this.adapter = adapter;
this.converter = converter;
}
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener onItemSelectedListener) {
this.onItemSelectedListener = onItemSelectedListener;
}
public interface ToString<T> {
String convertToString(T object);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment