Auro Auro - 5 months ago 64
Java Question

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

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();
}
});
}

}

Answer

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 :)

Comments