Auto fill phone number without country code
Asked Answered
P

1

6

I have an edit text that takes in phone number.

<androidx.appcompat.widget.AppCompatEditText
                   android:id="@+id/et_mobile_number"
                   style="@style/EditTextBoxNumeric"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:inputType="number"
                   android:maxLength="@integer/max_phone_number_length"
                   android:autofillHints="phoneNumberDevice"
                    />

Android tries to auto fill this edit text with phone number, but while doing so fills it along with the country code. And since I have an upper limit to the number of characters this edit text can hold, the last three digits of the number are stripped down.

For example, the edit text shows auto fill option of the number (+91 77777 77890) and on clicking the option the edit text fills up with just (+91 77777 777). What I need is just (77777 77890) and not the country code (+91).

I have tried

      binding.etMobileNumber.setAutofillHints(HintConstants.AUTOFILL_HINT_PHONE_NUMBER_DEVICE)
binding.etMobileNumber.setAutofillHints(HintConstants.AUTOFILL_HINT_PHONE_NATIONAL)

and

android:autofillHints="phoneNumberDevice"
android:autofillHints="phoneNational"
Perversity answered 13/1, 2020 at 7:32 Comment(0)
G
2

Use AUTOFILL_HINT_PHONE_NATIONAL for phone number without country code

binding.etMobileNumber.setAutofillHints(
    HintConstants.binding.etMobileNumber.setAutofillHints(HintConstants.AUTOFILL_HINT_PHONE_NATIONAL))

In xml phoneNational

Here is documentation https://developer.android.com/reference/androidx/autofill/HintConstants#AUTOFILL_HINT_PHONE_NATIONAL

Grantgranta answered 13/1, 2020 at 7:36 Comment(2)
This is working fine in my test code. Try using only one of the options, either xml or Java. Not both. I have used in xml only and it works.Grantgranta
Hey @Perversity any chance you are using a samsung device? I have an S10E and I get the whole number (with the country code prefix) even with the phoneNational flagForehead

© 2022 - 2024 — McMap. All rights reserved.