Skip to content Skip to sidebar Skip to footer

Formatting Credit Card Input Like (xxxx Xxxx Xxxx Xxxx) In Android

How to set credit card number in above format(spaces in between) in android? Can anyone help me with this?

Solution 1:

If i am not wrong, you want to do like, as user provides input in edittext after every four characters typed, a space should be placed after it.

If this is the thing you need, then you can implement:

editText.addTextChangedListener(new TextWatcher(){
        publicvoidafterTextChanged(Editable s) {
           // apply your logic for putting space after every four characters typed
        }
        publicvoidbeforeTextChanged(CharSequence s, int start, int count, int after){}
        publicvoidonTextChanged(CharSequence s, int start, int before, int count){}
    });

Edit:

I think you can use PatternMatcher class. Follow these links on [developer.android.com]:

http://developer.android.com/reference/android/os/PatternMatcher.html

http://developer.android.com/reference/java/util/regex/Pattern.html

http://developer.android.com/reference/java/util/regex/Matcher.html

Check the tutorial here: http://www.vogella.com/articles/JavaRegularExpressions/article.html

Solution 2:

You can do like this:

privateString_ccNumber="";

cc_no.addTextChangedListener(ccWatcher);

private TextWatcher ccWatcher= newTextWatcher() {
    @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {}
    @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {}
    @OverridepublicvoidafterTextChanged(Editable s) {
        if(_ccNumber.length() < s.length()){

            switch(s.length()){
                case6:
                    s.insert(5, " ");
                    break;
                case11:
                    s.insert(10, " ");
                    break;
                case16:
                    s.insert(15, " ");
                    break;
            }
        }
        _ccNumber = s.toString();
    }
};

Solution 3:

//get the reference of this edit text field
 EditText  etNICNO_Sender=(EditText)findViewById(R.id.etNICNO_Sender);
    /*add textChangeListner with TextWatcher argument
        by adding text change listner with text watcher we can get three methods of
        Edit Text 1) onTextChanged 2) beforeTextChanged 3) afterTextChanged
        these methods work when user types in text feild.
     */
 etNICNO_Sender.addTextChangedListener(newTextWatcher() {
   int len=0;
   @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stubStringstr= etNICNO_Sender.getText().toString();

  if((str.length()==5 && len <str.length()) || (str.length()==10 && len <str.length()) || (str.length()==15 && len <str.length())){
                  //checking length  for backspace.
                  etNICNO_Sender.append(" "); //append space
                 }
   }
   @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
     int after) {
    Stringstr= etNICNO_Sender.getText().toString();
                         len = str.length();
        }
   @OverridepublicvoidafterTextChanged(Editable s) {
    // TODO Auto-generated method stub

   }
  });

this worked for me,like charm! see here for complete code snippet just you need to append space automatically,i use to append hyphen automatically

Post a Comment for "Formatting Credit Card Input Like (xxxx Xxxx Xxxx Xxxx) In Android"