Code to show hyphen(-) after every four digits of entered number in EditText,android

->    Here in my requirement when ever i entered CreditCard number in my EditText,a Hyphen(-) will be added after entering every four digits in my EditText.just refer below image..

this code will work even in "Samsung galaxy s3 " latest Android version devices also..
EditText cardNumber=(EditText) findViewById(R.id.cardNumber);
cardNumber.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                //XXX do something                
            }
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {
                //XXX do something                

            }
            public void onTextChanged(CharSequence s, int start, int before, int count) {                                                  
                showImage(cardNumber.getText().toString());

                ///Code  set listener for BackButton
                /*cardNumber.setOnKeyListener(new OnKeyListener() {                
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {

                        if(keyCode != KeyEvent.KEYCODE_DEL && cardNumber.getText().length() !=0)
                        {*/
                            if(cardNumber.getText().length()==5 ||cardNumber.getText().length()==10 ||cardNumber.getText().length()==15 ||cardNumber.getText().length()==20)
                            {                            
                                tempString=cardNumber.getText().toString()+"-";
                                char c=tempString.charAt(tempString.length()-2);

                                if(c!='-')
                                {
                                    stringArray = tempString.toCharArray();                   
                                    stringArray[tempString.length()-2]=stringArray[tempString.length()-1];
                                    stringArray[tempString.length()-1]=c;

                                    //code to convert charArray back to String..
                                    tempString=new String(stringArray);
                                    cardNumber.setText(tempString);            
                                    cardNumber.setSelection(tempString.length());
                                    tempString=null;
                                }

                            }
                        /*}

                        return false ;      
                    }
                });*/

            }
        });




 <EditText
                        android:id="@+id/cardNumber"
                        android:layout_width="fill_parent"
                        android:layout_height="47dip"
                        android:layout_below="@+id/cardHolderName"
                        android:layout_marginLeft="25dip"
                        android:layout_marginRight="25dip"
                        android:gravity="center_vertical"
                        android:hint="Card Number*"
                        android:inputType="number"
                        android:maxLength="24"
                     
                        android:singleLine="true"
                        android:textSize="15dip" />

Comments

Post a Comment

Popular posts from this blog

Custom Dialog with no title and full screen in Android

Best working code to set Date limits on Date Picker Dialog,in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard