Avadhani Y
Avadhani Y

Reputation: 7636

Hyperlinking the Phone number in AlertDialog of Android

I have an AlertDialog having the text "For more information, Please Call 1-800-200-1000".

Here is my Code for the Alert Dialog Display while clicking on the ListView Item :

 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final SpannableString s = new SpannableString("1-800-200-1000");
        Linkify.addLinks(s, Linkify.ALL);
        ListView.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

          builder.setMessage("For more information, Please Call "+s)
           .setCancelable(true)
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                 dialog.dismiss();
               }
           });
          AlertDialog alert = builder.create();
          alert.show();
        }});

Here i wish to hyperlink the "1-800-200-1000" and while clicking on this, another dialog for call function should be implemented.

Here is my AlertDialog for Call Function:

    final Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you want to Call?");
    builder.setCancelable(false);
    builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
       Intent dial = new Intent();
            dial.setAction("android.intent.action.DIAL");dial.setData(Uri.parse("tel:"+ Phone));
                                        startActivity(dial); 

                                    }
                                });
                                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                       // Toast.makeText(getApplicationContext(),"Activity will continue",Toast.LENGTH_LONG).show();
                                        dialog.cancel();
                                    }
                                });
                                AlertDialog dialog = builder.create();
                                dialog.show();

Please help me in two problems:

1.How to hyperlink the Phone number in the First Dialog?

2.how to embed the Call AlertDialog while clicking the Hyperlinked Phone Number?

Thanks in advance.

Upvotes: 4

Views: 1982

Answers (1)

Tofeeq Ahmad
Tofeeq Ahmad

Reputation: 11975

try this one:

Spannable spans = (Spannable) text;
ClickableSpan clickSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"+ url));
}
public void updateDrawState(TextPaint ds) {
    //override link-centric text appearance
}
};
int index = (text.toString()).indexOf(url);
spans.setSpan(clickSpan, index, url.length() + index,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Upvotes: 5

Related Questions