title: Android Phone number format (XXX)-XXX-XXXX
body: <p>I want to type phone number in <code>EditText</code> in format <code>(XXX)-XXX-XXXX</code>. But I am not getting any solution for this. I succeeded to type but when I use backspace and again type number then this format not comes.Please give me solution. My code is </p>
  
  <pre><code>phoneEditText.addTextChangedListener(new TextWatcher() {
              private int keyDel;
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before,
                      int count) {
  
                  phoneEditText.setOnKeyListener(new OnKeyListener() {
  
                      @Override
                      public boolean onKey(View v, int keyCode, KeyEvent event) {
                          if (keyCode == KeyEvent.KEYCODE_DEL)
                              keyDel = 1;
                          return false;
                      }
                  });
  
                  if (keyDel == 0) {
                      int len = phoneEditText.getText().length();
                      if (len == 3
                              &amp;&amp; !(phoneEditText.getText().toString()
                                      .contains("("))) {
                          phoneEditText.setText("("
                                  + phoneEditText.getText().toString().trim()
                                  + ")-");
                          phoneEditText.setSelection(phoneEditText.getText()
                                  .length());
                      } else if ((len == 1 || len == 2 || len == 4 || len == 3)
                              &amp;&amp; (phoneEditText.getText().toString()
                                      .contains("("))) {
                          if (len == 4)
                              phoneEditText.setText(phoneEditText.getText()
                                      .toString().trim()
                                      + ")-");
                          phoneEditText.setSelection(phoneEditText.getText()
                                  .length());
  
                      } else if (len &gt; 3 &amp;&amp; len == 9) {
                          phoneEditText.setText(phoneEditText.getText()
                                  .toString().trim()
                                  + "-");
                          phoneEditText.setSelection(phoneEditText.getText()
                                  .length());
                      } else if (len &gt; 9 &amp;&amp; len == 14) {
                      }
  
                  } else {
                      keyDel = 0;
                  }
              }
  
              @Override
              public void afterTextChanged(Editable arg0) {
              }
  
              @Override
              public void beforeTextChanged(CharSequence arg0, int arg1,
                      int arg2, int arg3) {
              }
          });
  </code></pre>