title: Using PhoneNumberFormattingTextWatcher without typing country calling code
body: <p>In the login panel of my app, I divided the country calling code and the remaining numbers in two editable TextView as below:</p>
  
  <p><a href="https://i.stack.imgur.com/Ds6oj.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ds6oj.jpg" alt="enter image description here"></a></p>
  
  <p>I want to use international formatting standard in the TextView on the right. If a user who has a phone number as +905444444444 types in number in these boxes, I want to see "90" in the box on the left and "544 444 4444" on the right.</p>
  
  <p>For this reason, I tried to use the following implementation that uses libphonenumber:</p>
  
  <pre><code>/**
   * Watches a {@link android.widget.TextView} and if a phone number is entered
   * will format it.
   * &lt;p&gt;
   * Stop formatting when the user
   * &lt;ul&gt;
   * &lt;li&gt;Inputs non-dialable characters&lt;/li&gt;
   * &lt;li&gt;Removes the separator in the middle of string.&lt;/li&gt;
   * &lt;/ul&gt;
   * &lt;p&gt;
   * The formatting will be restarted once the text is cleared.
   */
  public class PhoneNumberFormattingTextWatcher implements TextWatcher {
  
      /**
       * Indicates the change was caused by ourselves.
       */
      private boolean mSelfChange = false;
  
      /**
       * Indicates the formatting has been stopped.
       */
      private boolean mStopFormatting;
  
      private AsYouTypeFormatter mFormatter;
  
      private String code;
  
      /**
       * The formatting is based on the current system locale and future locale changes
       * may not take effect on this instance.
       */
      public PhoneNumberFormattingTextWatcher() {
          this(Locale.getDefault().getCountry());
      }
  
      /**
       * The formatting is based on the given &lt;code&gt;countryCode&lt;/code&gt;.
       *
       * @param countryCode the ISO 3166-1 two-letter country code that indicates the country/region
       * where the phone number is being entered.
       */
      public PhoneNumberFormattingTextWatcher(String countryCode) {
          if (countryCode == null) throw new IllegalArgumentException();
          mFormatter = PhoneNumberUtil.getInstance().getAsYouTypeFormatter(countryCode);
      }
  
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
                                    int after) {
          if (mSelfChange || mStopFormatting) {
              return;
          }
          // If the user manually deleted any non-dialable characters, stop formatting
          if (count &gt; 0 &amp;&amp; hasSeparator(s, start, count)) {
              stopFormatting();
          }
      }
  
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
          if (mSelfChange || mStopFormatting) {
              return;
          }
          // If the user inserted any non-dialable characters, stop formatting
          if (count &gt; 0 &amp;&amp; hasSeparator(s, start, count)) {
              stopFormatting();
          }
      }
  
      @Override
      public synchronized void afterTextChanged(Editable s) {
  
          if (mStopFormatting) {
              // Restart the formatting when all texts were clear.
              mStopFormatting = !(s.length() == 0);
              return;
          }
          if (mSelfChange) {
              // Ignore the change caused by s.replace().
              return;
          }
          String formatted = reformat(s, Selection.getSelectionEnd(s));
          if (formatted != null) {
              int rememberedPos = mFormatter.getRememberedPosition();
              mSelfChange = true;
              s.replace(0, s.length(), formatted, 0, formatted.length());
              // The text could be changed by other TextWatcher after we changed it. If we found the
              // text is not the one we were expecting, just give up calling setSelection().
              if (formatted.equals(s.toString())) {
                  Selection.setSelection(s, rememberedPos);
              }
              mSelfChange = false;
          }
          // PhoneNumberUtils.ttsSpanAsPhoneNumber(s, 0, s.length());
      }
  
      /**
       * Generate the formatted number by ignoring all non-dialable chars and stick the cursor to the
       * nearest dialable char to the left. For instance, if the number is  (650) 123-45678 and '4' is
       * removed then the cursor should be behind '3' instead of '-'.
       */
      private String reformat(CharSequence s, int cursor) {
          // The index of char to the leftward of the cursor.
          int curIndex = cursor - 1;
          String formatted = null;
          mFormatter.clear();
          char lastNonSeparator = 0;
          boolean hasCursor = false;
          int len = s.length();
          for (int i = 0; i &lt; len; i++) {
              char c = s.charAt(i);
              if (PhoneNumberUtils.isNonSeparator(c)) {
                  if (lastNonSeparator != 0) {
                      formatted = getFormattedNumber(lastNonSeparator, hasCursor);
                      hasCursor = false;
                  }
                  lastNonSeparator = c;
              }
              if (i == curIndex) {
                  hasCursor = true;
              }
          }
          if (lastNonSeparator != 0) {
              formatted = getFormattedNumber(lastNonSeparator, hasCursor);
          }
          return formatted;
      }
  
      private String getFormattedNumber(char lastNonSeparator, boolean hasCursor) {
          return hasCursor ? mFormatter.inputDigitAndRememberPosition(lastNonSeparator)
                  : mFormatter.inputDigit(lastNonSeparator);
      }
  
      private void stopFormatting() {
          mStopFormatting = true;
          mFormatter.clear();
      }
  
      private boolean hasSeparator(final CharSequence s, final int start, final int count) {
          for (int i = start; i &lt; start + count; i++) {
              char c = s.charAt(i);
              if (!PhoneNumberUtils.isNonSeparator(c)) {
                  return true;
              }
          }
          return false;
      }
  }
  </code></pre>
  
  <p>However, this TextWatcher formats the numbers includes the calling code. In other words, it successfully formats "+905444444444" but cannot format "54444444444". How can I achieve to get the same result when the input phone number includes the country code in the TextView on the right? Needless to say but I want to get the following output:</p>
  
  <ul>
  <li>5</li>
  <li>54</li>
  <li>544</li>
  <li>544 4</li>
  <li>544 44</li>
  <li>544 444</li>
  <li>544 444 4</li>
  <li>544 444 44 ...</li>
  </ul>