Advertisement

header ads

How to invoke dialpad with phone number in android studio



In the last tutorial I’ve talked about how to invoke defaultbrowser when button clicked. If you haven’t read it yet you can read it from the given link. In that tutorial I’ve used a created a button and when the button clicked the default browser will appear and go to a given specific website, in my case it’ll go to www.google.com . 

So in this tutorial I’m going to tell you how to invoke dialpad with phone number in android studio.
So this example demonstrate how to invoke inbuilt dialpad with number entered in EditText. It is uses intent to open another app directly from this app by availing the phone number. 

So in this program I’m using, 

If((phoneedit.getText().toString()).equals(“”)) method.

This verifies inputs by user before invoking dialpad. And it will accept single digits also.
So here is the code. 



@Override
protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
    setContentView(R.layout.
activity_main);

    LinearLayout dialLayout =
new LinearLayout(this);
    dialLayout.setOrientation(LinearLayout.
VERTICAL);

    TextView phoneText =
new TextView(this);
    phoneText.setText(
"Dial Pad will be invoke with below entered phone/mobile number");

   
final EditText phoneEdit = new EditText(this);
    phoneEdit.setInputType(InputType.
TYPE_CLASS_NUMBER);

    phoneEdit.setHint(
"Enter Mobile/Phone Number...");

    Button phonePad =
new Button(this);
    phonePad.setText(
"Click to Open DialPad with Number");
    phonePad .setOnClickListener(
new View.OnClickListener(){
       
@Override
       
public void onClick(View v){
           
if ((phoneEdit.getText().toString()).equals("")){
                Toast.makeText(MainActivity.
this, "Please enter number..", Toast.LENGTH_LONG).show();
            }
else {
                Uri uriDialPad = Uri.parse(
"tel:" + phoneEdit.getText().toString());
                Intent uPhone =
new Intent(Intent.ACTION_DIAL, uriDialPad);
                startActivity(uPhone);
            }
        }
    });

    dialLayout.addView(phoneText);
    dialLayout.addView(phoneEdit);
    dialLayout.addView(phonePad);

    setContentView(dialLayout);
}



You can type the above code in your  program and test it by deploying it on the android studio AVD or on your android device. The result will be as below.


If you have any questions regarding the program, comment them in the comment section. And share this tutorial in social media. 

Happy Coding!

Post a Comment

0 Comments