TextView Hyperlink not working?

Why TextView Hyperlink Doesnโ€™t Work.

Using hyperlinks as inside a custom dialog box .

The hyperlink is not displayed.

Where am I mistaken. How to solve this problem. Give me some tips.

XML code

 <TextView android:id="@+id/google_Link" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:padding="10dip" android:textSize="20dip" android:linksClickable="true" android:autoLink="all" android:textColorLink="#306EFF" android:text="" /> 

Android code

 TextView googleLink = ( TextView ) layout.findViewById( R.id.google_Link ); googleLink.setClickable(true); googleLink.setMovementMethod(LinkMovementMethod.getInstance()); googleLink.setText( Html.fromHtml( "<a href=`http://www.google.co.in`>Google</a>" ) ); 

Android manifest code

 <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> 

Thanks in advance.

+6
source share
3 answers

Replace only this link, it will work:

 TextView textView=(TextView) findViewById(R.id.link); textView.setClickable(true); String linkTxt=getResources().getString(R.string.link); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setText(Html.fromHtml( linkTxt)); 

Add this to strings.xml:

 <string name="link">&lt;a href=http://www.google.co.in&gt;Google&lt;/a&gt;</string> 
+6
source

The best solution to this problem: First, create a Textview.

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/link" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:text="@string/developed_by_bracecodes"/> 

Then add the text to the text view from strings.xml as shown below:

 <string name="developed_by_bracecodes"><a href="http://www.bracecodes.com">Developed by Bracecodes</a> </string> 

NB: don't forget to add http: // in front of your link

Then add these lines to your Java code:

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

Good coding! Thanks!

+1
source

It does not work because you cannot set href in TextView .

You need to install OnClickListener which has this in onClick :

 String url = "http://www.google.co.in"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); 

After that, you can configure the listener on a TextView as follows: googleLink.setOnClickListener(myListener);

Then run the application again and the click should be processed correctly.

0
source

Source: https://habr.com/ru/post/920117/


All Articles