0

My String is somthing conbination of url string and url within href like: " Go http://www.google.co.in for more detail click

here

"

No i need to show clickable both: 1- http:www.google.co.in 2- here

and clicking on both the link it should show the respective url.

  • http://stackoverflow.com/questions/19908547/create-clickable-link-in-text-view-in-android. Use a Spannable String – Raghunandan May 28 '14 at 12:55

2 Answers2

1

Use android:autoLink="web" in your XML file.

Or

Just use an HTML format link in your resource (Strings.xml):

<string name="my_link"><a ref="http://somesite.com/">Click me!</a></string>

You can then use setMovementMethod(LinkMovementMethod.getInstance()) on your TextView to make the link clickable.

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
  • in my string,both are there like string is="Go http://www.google.co.in and for more detail click here" and i am using it from html.fromhtml() method. I have used autolink but it remove the links from here. – Nitish Srivastava May 29 '14 at 04:02
0

You can try:

// text2 has links specified by putting <a> tags in the string
// resource.  By default these links will appear but not
// respond to user input.  To make them active, you need to
// call setMovementMethod() on the TextView object.

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

Another Way:

https://stackoverflow.com/a/17544212/1318946

Edited:

Its not perfectly right solution but you can solve your problem using this one.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Go"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Google.com"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#0000ff" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="5dp"
    android:text="For more Details"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/textView3"
    android:text="Click here"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#0000ff" />

Output is looks like:

enter image description here

Now you have to define onClickListener for textView2 and textView4 and you can do want you want. :)

Happy Coding.

Community
  • 1
  • 1
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437