Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

java - clearable autocomplete with AutoCompleteTextView

I am building a project and I am implementing an autocomplete, but I want to create an icon inside my autocomplete design to clear all the input.

What i want to create

My autocomplate

here is autocomplete

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false">

        <AutoCompleteTextView
            android:id="@+id/new_offer_adrs_depart"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="7dp"
            android:layout_marginTop="9dp"
            android:layout_marginRight="7dp"
            android:layout_marginBottom="3dp"
            android:layout_weight="0.6"
            android:background="@drawable/background_offer_text_input"
            android:drawableStart="@drawable/ic_baseline_search_24"
            android:elevation="5dp"
            android:gravity="center_vertical"
            android:hint="@string/new_offer_adrs_depart"
            android:inputType="text"
            android:maxLines="1"
            android:paddingLeft="15dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:textColor="@color/grey_9b"
            android:textSize="14sp"
            app:autoSizeTextType="uniform" />


    </LinearLayout>
question from:https://stackoverflow.com/questions/65892565/clearable-autocomplete-with-autocompletetextview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I created this solution for my problem and it worked for me

XML

 <androidx.appcompat.widget.AppCompatAutoCompleteTextView
        android:id="@+id/origin_adrs"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:drawableStart="@drawable/ic_baseline_search_24"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:hint="@string/selectionner_votre_addresse"
        android:fontFamily="@font/nunito_semibold"
        android:textSize="@dimen/standard_text_14"
        android:background="@drawable/backgroud_text_input_adress"
        />

JAVA CODE

Boolean isHere2 = false;
autoCompleteTextView.setOnTouchListener(new View.OnTouchListener() {
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int DRAWABLE_LEFT = 0;
            final int DRAWABLE_TOP = 1;
            final int DRAWABLE_RIGHT = 2;
            final int DRAWABLE_BOTTOM = 3;
            autoCompleteTextView.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    String adress = autoCompleteTextView.getText().toString();
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if(charSequence.length() == 0){
                        autoCompleteTextView.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getApplicationContext(),R.drawable.ic_baseline_search_24), null, null, null);
                        isHere2 = false;
                    }else{
                        autoCompleteTextView.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getApplicationContext(),R.drawable.ic_baseline_search_24), null, ContextCompat.getDrawable(getApplicationContext(),R.drawable.ic_baseline_cancel_24), null);
                        isHere2 = true;
                    }
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

            if(isHere2){
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (event.getRawX() >= (autoCompleteTextView.getRight() - autoCompleteTextView.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        autoCompleteTextView.setText("");
                        isHere2 = false;
                        return true;
                    }
                }
            }
            return false;
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...