Spinner in Edittext
Asked Answered
T

3

7

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>
Tuna answered 3/3, 2017 at 13:34 Comment(0)
D
5

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 enter image description here

Dichroism answered 6/5, 2019 at 9:34 Comment(0)
R
4

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();
        }
    });
Renayrenckens answered 3/3, 2017 at 13:47 Comment(1)
I can't use AutoCompleteTextView because I will choose a category from the "custom" spinner. I dont want to use spinner because I want also to use a text input layout.Tuna
O
0
public class DropDownEditText extends AutoCompleteTextView {
    private float x;

    public DropDownEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCompoundDrawablesWithIntrinsicBounds(null, null, context.getDrawable(R.drawable.ic_spinner_caret), null);
        setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, new String[]{"one", "two", "three", "four"}));
    }

    @Override
    public boolean performClick() {
        if (x < (getRight() - getCompoundDrawables()[2].getBounds().width()))
            return super.performClick();
        showDropDown();
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            x = event.getX();
        }
        return super.onTouchEvent(event);
    }
}
Oxidation answered 6/12, 2020 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.