Search code examples
androidandroid-layoutandroid-widget

Custom format edit text input android to accept credit card number


how to make edit text accept input in format

4digitnumber-4dignumber-4dignumber-4dignumber   

The code

text.addTextChangedListener(new TextWatcher() {
    int len = 0;
    String string ;
    @Override

    public void afterTextChanged(Editable s) {

        text.setOnKeyListener(new OnKeyListener()
        {   public boolean onKey(View v, int keyCode, KeyEvent event)
            {              
                    if (keyCode == KeyEvent.KEYCODE_DEL)
                    {

                    }
                    else{

                        string = text.getText().toString();
                        len = string.length()+1;
                        if(len%5==0){text.append("-");}

             }

                return false;      }   });
    }
});

works fine upon adding, but deleting or editing causes problem.


Solution

  • Now this works fine for soft/hard keyboard for all delete/edit ops. tx 4 ur help..

    package com.and;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.inputmethodservice.KeyboardView;
    import android.os.Bundle;
    import android.telephony.PhoneNumberFormattingTextWatcher;
    import android.text.Editable;
    import android.text.Selection;
    import android.text.Spannable;
    import android.text.TextWatcher;
    import android.text.format.Formatter;
    import android.text.method.NumberKeyListener;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnKeyListener;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class ccformat extends Activity {
    
        String a;
        int keyDel;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            final EditText text = (EditText) findViewById(com.and.R.id.editText1);
    
            text.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                    boolean flag = true;
                    String eachBlock[] = text.getText().toString().split("-");
                    for (int i = 0; i < eachBlock.length; i++) {
                        if (eachBlock[i].length() > 4) {
                            flag = false;
                        }
                    }
                    if (flag) {
    
                        text.setOnKeyListener(new OnKeyListener() {
    
                            @Override
                            public boolean onKey(View v, int keyCode, KeyEvent event) {
    
                                if (keyCode == KeyEvent.KEYCODE_DEL)
                                    keyDel = 1;
                                return false;
                            }
                        });
    
                        if (keyDel == 0) {
    
                            if (((text.getText().length() + 1) % 5) == 0) {
    
                                if (text.getText().toString().split("-").length <= 3) {
                                    text.setText(text.getText() + "-");
                                    text.setSelection(text.getText().length());
                                }
                            }
                            a = text.getText().toString();
                        } else {
                            a = text.getText().toString();
                            keyDel = 0;
                        }
    
                    } else {
                        text.setText(a);
                    }
    
                }
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        }
    }