0

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".

Nitesh Verma
  • 2,460
  • 4
  • 20
  • 36

1 Answers1

2

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);
Nidhi
  • 777
  • 7
  • 17