0

I am building an Andoid app where you can enter you credit card info. I want to change the Image next to the edit text depending with what the user typed in real time .

ex: If the number start with 4 it is a visa card.

Any answer will be helpful.

coder
  • 8,346
  • 16
  • 39
  • 53
sami
  • 11
  • 4

3 Answers3

0

You can use the listener methods in edit text, as they type update the image you want to edit in afterTextChanged

editText.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) 
   {
       //update view
   }

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {

   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
   int before, int count) {

   }
});

A good link on how the methods work: Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

Nigel Brown
  • 440
  • 2
  • 13
  • I think I will basically just use the OnTextchenged ?? – sami Jul 26 '18 at 16:24
  • yeah just put your logic in the method on what image should show, you could use after text changed as well. I updated my answer with a link on how all methods work – Nigel Brown Jul 26 '18 at 16:26
0

You can use databinding. It will allow you to keep variable sync with any field on your UI.

You will have to add xml property on your view. ex.

 android:text="@{viewModel.myText}"

Here is the guide how to implement it: https://developer.android.com/topic/libraries/data-binding/start

Maksym V.
  • 2,877
  • 17
  • 27
0

You can use library Card Form for credit card input.

<com.braintreepayments.cardform.view.CardForm
    android:id="@+id/card_form"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

for details https://github.com/braintree/android-card-form

Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53