title: Android afterTextChanged won't enter a space after every 4 characters
body: <p>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 <a href="https://stackoverflow.com/questions/11790102/format-credit-card-in-edit-text-in-android">this</a> tutorial and modified a bit, but it's not working. It's not entering a space after every 4 digits.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>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() &gt; 0 &amp;&amp; (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();
              }
          });
      }
  
  }
  </code></pre>