Skip to content

Instantly share code, notes, and snippets.

@MehdiFal
Last active October 24, 2018 23:11
Show Gist options
  • Save MehdiFal/67b6c6a7cb35faeec2d88951e41e1ba7 to your computer and use it in GitHub Desktop.
Save MehdiFal/67b6c6a7cb35faeec2d88951e41e1ba7 to your computer and use it in GitHub Desktop.
if (Build.VERSION.SDK_INT >= 26) {
AutofillManager autofillManager = getActivity ().getSystemService (AutofillManager.class);
autofillManager.registerCallback (new AutofillManager.AutofillCallback () {
@Override
public void onAutofillEvent (@NonNull View view, int event) {
super.onAutofillEvent (view, event);
if (event == EVENT_INPUT_HIDDEN) {
recentAutofill = true;
}
}
});
cvcEditText.addTextChangedListener (new TextWatcher () {
@Override
public void beforeTextChanged (CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void onTextChanged (CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void afterTextChanged (Editable editable) {
if (editable.length () >= 3 && recentAutofill) {
//This is because Android can't get its shit together. AfterTextChanged is fired BEFORE the EditText text has changed.
final Handler autoFillHandler = new Handler ();
autoFillHandler.postDelayed (new Runnable () {
@Override
public void run () {
Activity activity = getActivity ();
if (activity != null) {
activity.runOnUiThread (new Runnable () {
@Override
public void run () {
onCreditCardEntered (EditorInfo.IME_ACTION_SEARCH, false);
recentAutofill = false;
}
});
}
}
}, 500);
}
}
});
nameOnCardEditText.setAutofillHints (View.AUTOFILL_HINT_NAME);
creditCardEditText.setAutofillHints (View.AUTOFILL_HINT_CREDIT_CARD_NUMBER);
zipEditText.setAutofillHints (View.AUTOFILL_HINT_POSTAL_CODE);
expirationEditText.setAutofillHints (View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE);
cvcEditText.setAutofillHints (View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment