Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android afterTextChanged won't enter a space after every 4 characters

Tags:

java

android

I'm trying to design a simple credit / debit card form. I have a cardNumber EditText field. As the user starts typing in, I want my app to insert a space after every 4 digits. I took help from this tutorial and modified a bit, but it's not working. It's not entering a space after every 4 digits.

MainActivity.java

package *************************

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import java.security.Key;

public class MainActivity extends AppCompatActivity {

    private EditText cardNumber, expiryDate, CVV, nameOnCard;

    private boolean flag = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);//Locks the screen orientation
        init(); //Initializes the variables

        typefunc();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void init()
    {
        cardNumber = (EditText) findViewById(R.id.cardNumber); //Field for storing the card number
        expiryDate = (EditText) findViewById(R.id.expiryDate); //For storing the Expiry Date
        CVV = (EditText) findViewById(R.id.CVV); //For storing the CVV
        nameOnCard = (EditText) findViewById(R.id.nameOnCard); //For storing the name of the Cardholder
    }

    public void typefunc()
    {
        cardNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

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

                if(s.length()==16)
                {
                    expiryDate.setVisibility(View.VISIBLE);
                    expiryDate.requestFocus();
                    CVV.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s)
            {

                char space = ' ';

                if (s.length() > 0 && (s.length() % 5) == 0)
                {
                    char c = s.charAt(s.length() - 1);

                    if (Character.isDigit(c))
                    {
                        s.insert(s.length() - 1, String.valueOf(space));
                    }
                }
            }
        });

        expiryDate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                if(s.length()==6)
                    CVV.requestFocus();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        CVV.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                if(s.length()==3) {
                    nameOnCard.setVisibility(View.VISIBLE);


                }
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                if(s.length()==3)
                    nameOnCard.requestFocus();
            }
        });
    }

}
like image 352
Auro Avatar asked Mar 12 '23 20:03

Auro


2 Answers

This is working fine

editText.addTextChangedListener(new TextWatcher() {
    private static final char space = ' ';

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

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

    @Override
    public void afterTextChanged(Editable s) {
        // Remove spacing char
        if (s.length() > 0 && (s.length() % 5) == 0) {
            final char c = s.charAt(s.length() - 1);
            if (space == c) {
                s.delete(s.length() - 1, s.length());
            }
        }
        // Insert char where needed.
        if (s.length() > 0 && (s.length() % 5) == 0) {
            char c = s.charAt(s.length() - 1);
            // Only if its a digit where there should be a space we insert a space
            if (Character.isDigit(c) && TextUtils.split(s.toString(), String.valueOf(space)).length <= 3) {
                s.insert(s.length() - 1, String.valueOf(space));
            }
        }
    }
});

and don't forgot to add below attributes to edittext

android:inputType="phone" android:maxLength="19"

hope this helps :)

like image 145
Mr.7 Avatar answered Mar 19 '23 02:03

Mr.7


Try this:

    cardNumber.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after){
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.length()==16) {
                expiryDate.setVisibility(View.VISIBLE);
                expiryDate.requestFocus();
                CVV.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            String txt = s.toString();
            s.clear();
            s.insert(0, txt.replaceAll("\\D","").replaceAll("(\\d{4}(?!$))","$1-"));
        }
    });
like image 22
Titus Avatar answered Mar 19 '23 03:03

Titus