Question Detail

How to split mobile number into country code, area code and local number?

4 years ago Views 3214 Visit Post Reply

I am getting a list of mobile numbers from the phone book, But I need to show mobile and country code split. 
how to split phone number and area code in android?


Thread Reply

alex levine

- 4 years ago

You can split your mobile number by checking is started with "+" sign and Phone number should have length 10.

void splitMobilenumberMethod(){
 String phoneNumb = MOBILE_NUMBER_TO_SPLIT;
                        String ext = "", phoneN = "";
                        if (phoneNumb.startsWith("+") || phoneNumb.length() > 10) {
                            ext=phoneNumb.substring(0, 3);
                            phoneN=phoneNumb.substring(3);
                        } else {
                            ext = "";
                            phoneN = phoneNumb;
                        }
                        showSelectedPhoneDialog(ext, phoneN);
}

 

void showSelectedPhoneDialog(String ext, String phone) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("Verify Phone Number");
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setGravity(View.TEXT_ALIGNMENT_CENTER);
    final EditText extEdit = new EditText(context);
    final EditText phoneEdit = new EditText(context);
    extEdit.setHint("Country");
    phoneEdit.setHint("Mobile Number");
    layout.addView(extEdit);
    layout.addView(phoneEdit);
    extEdit.setText(ext);
    phoneEdit.setText(phone);
    alertDialog.setView(layout);

    alertDialog.setIcon(R.drawable.ic_message);
    alertDialog.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do if split is correct or after make it corrent manually by user
        }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            dialog.cancel();
        }
    });

    alertDialog.show();
}