0

I am struggling to make my links clickable within an Android application. I have tried the following:

<ScrollView
<RelativeLayout
<TextView
    ...
    android:id="@+id/textView"
    android:text="@string/stringName"
    android:linksClickable="true"
    android:autoLink="web"

with either

<string name="stringName"><a href="http://www.example.com">String</a></string>

or

<string name="stringName">&lt;a href="http://www.example.com">String&lt;/a></string>

while also incorporating

((TextView) findViewById(R.id.textView)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.textView)).setText(Html.fromHtml(getResources().getString(R.string.stringName)));

in my .java

EDIT: I can make my string appear as clickable using the former method in my string.xml file, however, when I click on it nothing happens. I believe it is because I am clicking on either the relativeLayout or even more likely the ScrollView as opposed to the TextView

USER_8675309
  • 853
  • 2
  • 14
  • 33

3 Answers3

0

This works for me:

TextView text1 = (TextView)findViewById(R.id.textView);

Spanned spanned = Html.fromHtml(getString(R.string.stringName));
text1.setMovementMethod(LinkMovementMethod.getInstance());
text1.setText(spanned);

And my strings.xml, it changes at the end replacing > for &gt;

<string name="stringName">&lt;a href="http://www.example.com"&gt;>String&lt;/a&gt;</string>
DavidGSola
  • 697
  • 5
  • 17
0

I am not too sure about nesting a TextView in a scrollView making it not clickable, but you can always make your TextView clickable in your code. You set an OnClickListener on your TextView, and inside start a new Intent like this :

First inflate the ScrollView and get your textView in the code like this :

ScrollView layout = (ScrollView) ScrollView.inflate(this, R.layout.campaign_info_layout, null);
TextView textView = (TextView) layout.findViewById(R.id.textView);

Then set the OnClickListener like this ;

textView.setOnClickListener(new OnClickListener (){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
                startActivity(i);
            }
        });
JDenais
  • 2,956
  • 2
  • 21
  • 30
  • whenever I do this my textView is redlined and says it can't be resolved to a type...What am I missing here? – USER_8675309 Dec 03 '14 at 01:54
  • You probably didn't inflate the ScrollView and the TextView in the code. I edited my answer to add the way to inflate it. – JDenais Dec 03 '14 at 02:15
0

this work for me:

<string name="stringName"><a href="http://www.example.com">String</a></string>


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

In Xml file:

Remove this two lines from TextView
android:linksClickable="true"
android:autoLink="web" 
Deepak Singh
  • 144
  • 6
  • This worked for me. The only way I could get it to work. It seems that having the lines in text view 'android:linksClickable' and 'android:autoLink' will prevent it from working correctly – USER_8675309 Dec 04 '14 at 18:02