Linga
Linga

Reputation: 10563

Android AutoCompleteTextview Dropdown allow the user to select only

In my android application, I'm using an AutoCompleteTextview on which the user has to select a name. If the user has entered some text (assume that the user didn't select text from AutoCompleteTextview), it also be displayed.

So, now I want to allow the user to only select the text and don't allow to enter the text. I've used

r_code.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            r_code.showDropDown();
            return false;
        }
    });

This code will display all the all the names when the user touches the AutoCompleteTextview. But it allows the user to enter some text. As mentioned above, I don't want to allow the user to enter text, they have to select the text. Can someone say how to solve my issue.

Thanks in advance

Upvotes: 2

Views: 3211

Answers (4)

Steffen Haas
Steffen Haas

Reputation: 91

I did basically the same as you.

1) I set the OnTouchListener to consume the click, so the view is not focused and the keyboard is not shown.

autoCompleteTextView.setOnTouchListener(object : OnTouchListener {
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                autoCompleteTextView.showDropDown()
                return true
            }
        })

2) I added two things to my xml.

I set focusable="false" and android:focusableInTouchMode="false". This way the Keyboard is no longer shown, when the user clicks on the AutoCompleteTextView.

<AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        style="@style/Atomic.Body1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

Only downside here is that it won't be focused by imeOptions="actionNext"and therefore be skipped.

Upvotes: 8

gladmax
gladmax

Reputation: 347

add KeyListener r_code.setKeyListener and handle keys clicked. I think if you return true the key will be consumed.

Upvotes: 0

Ajit
Ajit

Reputation: 967

check entered string with strings in arrayadapter if array does not contains entered string ask user to select another string

Upvotes: 1

Jason Sankey
Jason Sankey

Reputation: 2338

If you want to restrict the selections to a known list, you can use a Spinner, see the Spinners guide for details.

Upvotes: 0

Related Questions