Spinner in Edittext
You can do like below in your XML file: Here android:drawableRight
you can set left right top and bottom icon in EditText
and TextView
in andorid
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:drawableRight="@drawable/ic_menu_share"/>
For showing list like spinner use AutoCompleteTextView
Android AutoCompleteTextView completes the word based on the reserved words, so no need to write all the characters of the word.
Android AutoCompleteTextView is a editable text field, it displays a list of suggestions in a drop down menu from which user can select only one suggestion or value.
Android AutoCompleteTextView is the subclass of EditText class. The MultiAutoCompleteTextView is the subclass of AutoCompleteTextView class.
Android AutoCompleteTextView Example Tutorial
OR
you can use Android PopupWindow Listview example .
/**
* handle header listview onclick event
*/
private OnClickListener showPopupWindow() {
return new OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popUp = popupWindowsort();
popUp.showAsDropDown(v, 0, 0); // show popup like dropdown list
}
};
}
/**
* show popup window method reuturn PopupWindow
*/
private PopupWindow popupWindowsort() {
// initialize a pop up window type
popupWindow = new PopupWindow(this);
ArrayList<String> sortList = new ArrayList<String>();
sortList.add("A to Z");
sortList.add("Z to A");
sortList.add("Low to high price");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
sortList);
// the drop down list is a list view
ListView listViewSort = new ListView(this);
// set our adapter and pass our pop up window contents
listViewSort.setAdapter(adapter);
// set on item selected
listViewSort.setOnItemClickListener(onItemClickListener());
// some other visual settings for popup window
popupWindow.setFocusable(true);
popupWindow.setWidth(250);
// popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the listview as popup content
popupWindow.setContentView(listViewSort);
return popupWindow;
}
find complete implementation in below links:
Android PopupWindow Listview example .
You can achieve this with autocompletetextview like
<AutoCompleteTextView
android:id="@+id/acType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:paddingBottom="@dimen/lef_margin"
android:paddingTop="@dimen/lef_margin"
android:singleLine="true"
android:textSize="@dimen/header_text_large"/>
ArrayAdapter arrayAdapter= new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, dataList);
acType.setAdapter(arrayAdapter);
acType.setInputType(0);
acType.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
acType.showDropDown();
}
});
With help of @Chetan's Answer I built this widget which may help anyone Please provide list of options using
setOptions
method
public class DropDown extends AppCompatTextView implements View.OnClickListener {
private ArrayList<String> options = new ArrayList<>();
public DropDown(Context context) {
super(context);
initView();
}
public DropDown(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public DropDown(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
this.setOnClickListener(this);
}
private PopupWindow popupWindowsort(Context context) {
// initialize a pop up window type
PopupWindow popupWindow = new PopupWindow(context);
popupWindow.setWidth(this.getWidth());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line,
options);
// the drop down list is a list view
ListView listViewSort = new ListView(context);
// set our adapter and pass our pop up window contents
listViewSort.setAdapter(adapter);
// set on item selected
listViewSort.setOnItemClickListener((parent, view, position, id) -> {
this.setText(options.get(position));
popupWindow.dismiss();
});
// some other visual settings for popup window
popupWindow.setFocusable(true);
// popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the listview as popup content
popupWindow.setContentView(listViewSort);
return popupWindow;
}
@Override
public void onClick(View v) {
if (v == this) {
PopupWindow window = popupWindowsort(v.getContext());
window.showAsDropDown(v, 0, 0);
}
}
public void setOptions(ArrayList<String> options) {
this.options = options;
}
}
in your layout file
<com.yourdomian.app.DropDown
style="@style/formDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/ship_to_address" />
in your style file
<style name="formDropDown">
<item name="android:paddingRight">20dp</item>
<item name="android:paddingLeft">24dp</item>
<item name="android:paddingTop">20dp</item>
<item name="android:paddingBottom">20dp</item>
<item name="android:textSize">13sp</item>
<item name="android:background">@drawable/edit_text_background_dark_round</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:elevation">5dp</item>
<item name="android:drawableRight">@drawable/ic_down_arrow</item>
<item name="android:gravity">center_vertical</item>
</style>
In your Java File
ArrayList<String> options = new ArrayList<>();
options.add("Option 1");
options.add("Option 2");
options.add("Option 3");
options.add("Option 4");
((DropDown)findViewById(R.id.dropdown)).setOptions(options);
Output will be