I have an Edittext with a drawable [v] at the right side of it to make it looks like a spinner. Now, how can i achieve this? I will set the edittext as clickable then when I click it, a dialogfragment will pop up with a list (looks like a spinner option)
Is it possible?
<android.support.design.widget.TextInputLayout
android:id="@+id/tilAppCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtAppCategory"
android:hint="Category"
android:fontFamily="sans-serif-light"
android:textColorHint="@color/textColorHint"
android:maxLines="1"
android:gravity="center|start"
android:inputType="textNoSuggestions"
android:textColor="@color/colorPrimaryDark"
android:textSize="14sp"
android:drawableEnd="@drawable/icon_spinner_down"
android:focusableInTouchMode="true"
android:clickable="true"
/>
</android.support.design.widget.TextInputLayout>
Here is an workaround: Spinner spinner = (Spinner) findViewById(R. id. spinner); ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With