NagarjunaReddy
NagarjunaReddy

Reputation: 8645

Android How to Display Links in webview like this?

Does anyone know of a way to Display URL's in webview like this

enter image description here

But here I am geting Like this here no focus and no click option not working any thing give me some sugessions please..

enter image description here

this code useing display data in webview

 webview.getSettings().setJavaScriptEnabled(true);
 webview.loadDataWithBaseURL(null, string, "text/html", "utf-8", null);

Upvotes: 7

Views: 8603

Answers (5)

dicenice
dicenice

Reputation: 261

Try this:

  String header = "< ?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
  String data = "< html>< body>< a href='tel:555-5599'>508-776-5510
  " "< /body>< /html>";
  mWebView.loadData(header+data,  "text/html", "UTF-8");

If you are loading a string of html texts into webView then you can use:

mWebView.loadData(header+data,  "text/html", "UTF-8");

If you have an html file then you can use:

webView.loadUrl("file:///android_asset/mypage.html"):

Note: Dont forget to put your html file in your assets folder.

Cheers!!! :D

Upvotes: 1

jojo
jojo

Reputation: 473

Please Add android:autoLink="web" in your TextView

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_text"
        android:autoLink="web"  
        android:textColorLink="@color/link"
        android:text="click here to load www.fb.com"
        android:textColor="@color/black" />

Upvotes: 1

Rajesh
Rajesh

Reputation: 15774

Use Linkify.

Spannable sp = new SpannableString(Html.fromHtml(string));
Linkify.addLinks(sp, Linkify.ALL);
final String html = "<body>" + Html.toHtml(sp) + "</body>";
webView.loadData(html, "text/html", "utf-8");

Upvotes: 11

PrincessLeiha
PrincessLeiha

Reputation: 3174

String str="Your text goes here... http://stackoverflow.com/questions/14576507/android-how-to-display-links-in-webview-like-this";

ArrayList<String> myArray = new ArrayList<String>();

myArray.add( "<!DOCTYPE HTML><!-- created HTML PAGE -->");
myArray.add("<head> ");
myArray.add("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"  width=100%>");
myArray.add("<meta name=\"viewport\" content=\"width=device-width\">");
myArray.add("<style>");
myArray.add("   p { font-family:Arial, Helvetica, sans-serif;}");
myArray.add("</style>");
myArray.add("</head> ");
myArray.add("<body style=\"background-color: transparent; margin-left:5%; margin-right:5%; \">");

myArray.add("<div >");
Spannable sp = new SpannableString(str);
Linkify.addLinks(sp, Linkify.ALL);
str = Html.toHtml(sp) ;
myArray.add(str);

String myFullString = myArray.toString().replace("[", "").replace("]", "").replace(",", "\n").replace("&lt;", "<").replace("&gt;", ">");

mWebView.loadDataWithBaseURL("about:blank", myFullString ,"text/html", "utf-8", null);

Upvotes: 7

Rachel Gallen
Rachel Gallen

Reputation: 28563

Sample code:

Java:

 TextView t2 = (TextView) findViewById(R.id.text2);
 t2.setMovementMethod(LinkMovementMethod.getInstance());

android:

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/yourid"
android:id="@+id/yourid"
android:layout_below="@+id/yourid" android:layout_centerInParent="true"
android:layout_marginTop="20dp"></TextView>

Android WebView: Change Auto-Link Display for webview

Upvotes: 1

Related Questions