Q:

Phone number format in Android. (X-XXX-XXX-XX)

I have a editText which will accept phone number from the user. My target is as soon as user enters the phone number it should be formatted like "X-XXX-XXX-XXX" and once a user long pressed it will open the dialler. 
I search on google but found format like XXX-XXX-XXX by using this code.

PhoneNumberUtils.formatNumber(String)

How can i achieve my desire output?

A:

 Add editText in your xml file like this 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="13"
        android:id="@+id/edit"/>
</LinearLayout>

 In your activity set textchange listner and key change listner to your editText
public class MainActivity extends AppCompatActivity {
    EditText editor;
    boolean isDeletePressed;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //X-XXX-XXX-XXX
        editor= (EditText) findViewById(R.id.edit);
        editor.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) {

            }

            @Override
            public void afterTextChanged(Editable s) {
             String text=s.toString();
                if(text.length()==1) {
                    if(!isDeletePressed) {
                        editor.setText(editor.getText().toString() + "-");
                        editor.setSelection(editor.getText().toString().length());
                    }
                }else if(text.length()==2){
                    if(!(text.charAt(1)=='-')){
                        editor.setText(editor.getText().toString().substring(0,1)+"-"+editor.getText().toString().substring(1));
                        editor.setSelection(editor.getText().toString().length());
                    }
                }else if(text.length()==5){
                    if(!isDeletePressed) {
                        editor.setText(editor.getText().toString() + "-");
                        editor.setSelection(editor.getText().toString().length());
                    }
                }else if(text.length()==6){
                    if(!(text.charAt(5)=='-')){
                        editor.setText(editor.getText().toString().substring(0,5)+"-"+editor.getText().toString().substring(5));
                        editor.setSelection(editor.getText().toString().length());
                    }
                }else if(text.length()==9){
                    if(!isDeletePressed) {
                        editor.setText(editor.getText().toString() + "-");
                        editor.setSelection(editor.getText().toString().length());
                    }
                }else if(text.length()==10){
                    if(!(text.charAt(9)=='-')){
                        editor.setText(editor.getText().toString().substring(0,9)+"-"+editor.getText().toString().substring(9));
                        editor.setSelection(editor.getText().toString().length());
                    }
                }
            }
        });
        editor.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                isDeletePressed = (i == KeyEvent.KEYCODE_DEL);
                return false;
            }
        });
    }
}

