Nitesh Verma Nitesh Verma - 18 days ago 9
Android Question

Separate hints for TextInputLayout and AutocompleteTextView

I want to tell user about the pattern in which they should type location like below

enter image description here

So I have used AutocompleteTextView inside TextInputLayout like code below

<android.support.design.widget.TextInputLayout
android:id="@+id/profile_youraddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/profile_emailaddress"
android:layout_marginTop="@dimen/margin_10"
android:hint="@string/your_address"
android:textColor="@color/home_primary_color">

<AutoCompleteTextView
android:id="@+id/profile_addresstext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Malviya Nagar, New Delhi, Delhi"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>


The problem is that when the TextInputLayout is not in focus it two hints overlap on each other like below
enter image description here

How can I override TextInputLayout's hint on the AutocompleteTextView's hint in non focusable state? Any help will be appreciated.

What I am looking For :

When not in focus, the hint should be "Your Address" ("Malviya Nagar, New Delhi, Delhi" should be hidden) and when in focus hint should be "Malviya Nagar, New Delhi, Delhi".

Answer

Do it using OnFocusChangeListener:

TextInputLayout text;
AutoCompleteTextView auto;
View.OnFocusChangeListener listener;

     text = (TextInputLayout) findViewById(R.id.profile_youraddress);
            auto = (AutoCompleteTextView) findViewById(R.id.profile_addresstext);
            auto.setFocusable(true);

            listener = new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus) {
                        auto.setHint("Malviya Nagar, New Delhi, Delhi");
                        text.setHint("");
                    }else {
                        auto.setHint("");
                        text.setHint("Your address");
                    }
                }
            };

            auto.setOnFocusChangeListener(listener);
Comments