0

I am making a app that users autocompleteTextview. which suggest suggestion from server when user type anything by addTextChangedListener but many times user may be not select suggestion and type directly and move But i want that user only select suggested items.

Kishor
  • 121
  • 9

1 Answers1

1

Output

enter image description here

After click on remove button

enter image description here

i have implement this same in my previous project i have create demo for your try this:

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user31.demosforstack.AutocomplateTextVIew">

    <AutoCompleteTextView
        android:id="@+id/autoComplateText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search here" />

</LinearLayout>

JAVA file

package com.example.user31.demosforstack;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import java.util.ArrayList;

public class AutocomplateTextVIew extends AppCompatActivity {

    AutoCompleteTextView autoComplateText;
    ArrayAdapter arrayAdapter;
    ArrayList<String> arrayList;
    boolean isSelect = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autocomplate_text_view);

        arrayList = new ArrayList();
        for (int i = 0; i < 10; i++) {
            arrayList.add("item " + i);
        }

        autoComplateText = findViewById(R.id.autoComplateText);
        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, arrayList);
        autoComplateText.setAdapter(arrayAdapter);
        autoComplateText.setThreshold(2);

        autoComplateText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String item = (String) parent.getAdapter().getItem(position);
                isSelect = true;

                autoComplateText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.remove, 0);

                Log.e("Item: ", item);
                autoComplateText.setInputType(InputType.TYPE_NULL);

                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(autoComplateText.getWindowToken(), 0);

            }
        });

        autoComplateText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int DRAWABLE_RIGHT = 2;

                if (event.getAction() == MotionEvent.ACTION_UP) {

                    if (autoComplateText != null){
                        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(autoComplateText.getWindowToken(), 0);

                        if (event.getRawX() >= (autoComplateText.getRight() - autoComplateText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                            autoComplateText.setText("");
                            autoComplateText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                            isSelect = false;
                            return true;
                        }
                    }



                }

                return false;
            }
        });


    }
}
ND1010_
  • 3,743
  • 24
  • 41