0

In my string i need to find the link and phone number and then need to change that by underline like a link that is requirement.

In this i need to find the number, if there is number then it need to open dialer. If there is link it need to open in browser.

Eg:-

String myString ="Please check the link www.google.com sadsd asdasd asd. Call us xxx-xxx-xxxx asd asdbsd sdasd"

In that www.google.com should open in browser and number open in phone number.

Navaneethan
  • 2,125
  • 6
  • 22
  • 32
  • now see my answer. i have made only calling button , now u have to made webview calling button – Rohit Oct 09 '13 at 07:44

2 Answers2

1

to find phone number Look for a phone number in a string

For underline android: how to add Underline in the String

for dailer

add in menifest

<uses-permission android:name="android.permission.CALL_PHONE" />

for call button

Button callButton = (Button)findViewById(R.id.btnCall);
txtPhn = (EditText)findViewById(R.id.txtPhnNumber);
callButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        try {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+txtPhn.getText().toString()));
                startActivity(callIntent);
            } catch (ActivityNotFoundException activityException) {
                Log.e("Calling a Phone Number", "Call failed", activityException);
            }
    }
});

try this

 <Button
android:id="@+id/btnview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"

 />

in class

Button btn = (button)findViewbyid(R.id.btnview);
btn.setText("YOUR Phone number");
btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
 Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+btn.getText().toString()));
                startActivity(callIntent);
            } catch (ActivityNotFoundException activityException) {
                Log.e("Calling a Phone Number", "Call failed", activityException);
            }

                }
            });
Community
  • 1
  • 1
Rohit
  • 3,401
  • 4
  • 33
  • 60
0

I found the solution.

we need to use spannalbe.

String [] parts = textViewText.split(" ");
StringBuilder strtextViewBuiler = new StringBuilder();
for(String items:parts){
if(items.startsWith("http://") || (items.startsWith("https://")) || (items.startsWith("www."))) {
        if(items.contains("<")){
            items = items.substring(0, items.indexOf("<"));
        }
        strtextViewBuiler .append(items+" ");
    }
    else if(items.contains("<http://")){
        int startIndex = items.indexOf("<http://");
        int endIndex = items.indexOf(">");
        String replacement = "";
        String toBeReplaced = items.substring(startIndex, endIndex+1);

        items = items.replace(toBeReplaced, replacement);
        strtextViewBuiler .append(items+" ");
    }
    else if(items.contains("<https://")){
        int startIndex = items.indexOf("<https://");
        int endIndex = items.indexOf(">");
        String replacement = "";
        String toBeReplaced = items.substring(startIndex, endIndex+1);                           
        items = items.replace(toBeReplaced, replacement);
        strtextViewBuiler .append(items+" ");
    }
    else{
        strtextViewBuiler .append(items+" ");
    }                               
}
mtextView.setMovementMethod(LinkMovementMethod.getInstance());
mtextView.setText(setSpannablePropertyForDescription(strtextViewBuiler .toString()), BufferType.SPANNABLE);
Navaneethan
  • 2,125
  • 6
  • 22
  • 32