'Android autofill enters the same info multiple times, when reusing layout
I have a fragment in which I reuse the same layout multiple times. The layout has two EditText fields, one for name and one for email. In other words the view lets you put in multiple contacts at the same time, how many is optional. What happens is:
- The user presses one of the fields, say the email field, and Android's built in autofill feature shows a suggestion.
- User presses the option, a stored email address.
- The email address is then entered multiple times by the autofill function, into each of the reused layouts. Say I have three views for contact information, then each of the three email fields will receive the autofilled email address, clearing anything that was before.
I want it to be entered only into the currently selected field, is there any way to control this?
LinearLayout contactsListLayout;
for (Contact contact : contacts) {
ContactView view = new ContactView(context, contact);
contactsListLayout.addView(view);
}
ContactView.java
public class ContactView extends RelativeLayout {
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_view);
}
...
}
contact_view.xml
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/nameEdit"
android:inputType="textCapSentences"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="flagNoExtractUi|actionSearch"
android:hint="Name"/>
<EditText
android:id="@+id/emailEdit"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="E-mail Address"/>
</LinearLayout>
Solution 1:[1]
Then the user fills the editText with his email address, you can store it in SQL database, after that user goes to the second view and there will be another field for email address. So at that time your app gets String value(email address) that you inserted into your SQL database , and fills in the editText automatically.
Hopefully my answer helps you.
Here are some links for setting up SQL database:
Solution 2:[2]
I have multiple email input fields in a layout. When one of the input fields gets focus, autofill options shows up. And if we choose one of the options, autofill service pastes the same value to all input fields even if its focused or not.
I tried manipulating the importantForAutoFill
flags of my views, to autoFill only the focused input field, but that didn't work.
So in my custom component, I've overridden the autoFill()
method, to make it work only if the view is focused. If not, suppressed the autoFill action.
override fun autofill(value: AutofillValue?) {
if (hasFocus()) {
super.autofill(value)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dor |
Solution 2 | opsenes |