Reputation: 62519
I'm not able to linkfy
a string with a href
when the string says the domain name with it. so string. let me show you.
Here is the string:
<string name="go_to_settings">Please go to "Settings" in <a href="https://www.mywebsite.com/settings">mywebsite.com</a> to change this status</string>
And it would say this to the user: "Please go to "Settings" in mywebsite.com to change this status. "
here is the textview
I'm using:
<TextView
android:id="@+id/tv_instructions"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:lineSpacingExtra="3sp"
android:textAlignment="center"
android:textColor="#5c5c5c"
android:textColorLink="@color/action_blue"
android:linksClickable="true"
android:text="@string/go_to_settings"/>
and here is how i am trying to make the link clickable
:
Linkify.addLinks(tv_instructions, Linkify.ALL)
tv_instructions.movementMethod=LinkMovementMethod.getInstance()
But i am seeing happen on api 24 and 27 device and emulator is that the link is going to www.mywebsite.com instead of www.mywebsite/settings
but here is the strange thing, if i change the text the user sees and remove .com from that then it works fine. so if the user see this instead it works:
and it would say this to the user: "Please go to "Settings" in mywebsite to change this status. "
notice there is no .com being mentioned. How can i get this displayed. i also tried this way:
tv_instructions.text=(utils.fromHTML(resources.getString(R.string.go_to_settings),null))
tv_instructions.movementMethod=LinkMovementMethod.getInstance()
and textview like this:
<TextView
android:id="@+id/tv_instructions"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:lineSpacingExtra="3sp"
android:textAlignment="center"
android:textColor="#5c5c5c"
android:text="@string/go_to_settings"
/>
you guys have any idea what I'm doing wrong ?
Upvotes: 0
Views: 1906
Reputation: 62519
Figured this out. for some reason i needed to wrap my text as character data using tag cData
in my strings
file. this works:
<string name="go_to_settings"><![CDATA[Please go to "Settings" in <a href="https://www.mywebsite.com/settings">mywebsite.com</a> to change this status :]]></string>
then the textview
looks like this:
<TextView
android:id="@+id/tv_instructions"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="73dp"
android:lineSpacingExtra="3sp"
android:text="@string/go_to_settings"
android:textAlignment="center"
android:textColor="#5c5c5c"
android:textColorLink="@color/action_blue"/>
Java Code
tv_instructions.text=(utils.fromHTML(resources.getString(R.string.go_to_settings),null))
tv_instructions.movementMethod=LinkMovementMethod.getInstance()
Upvotes: 2
Reputation: 69681
linkify on textview with domain in text not working, links not clickable
You can also use ClickableSpan
If an object of this type is attached to the text of a
TextView
with a movement method ofLinkMovementMethod
, the affected spans of text can be selected. If selected and clicked, theonClick(View)
method will be called.
Check below SAMPLE CODE
Try this
public class MainActivity extends AppCompatActivity {
TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.myTextview);
setSpan();
}
private void setSpan() {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("Please go to \"Settings\" in");
SpannableString spannableString = new SpannableString(" mywebsite.com ");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
// you can open your link here as per your requiremnt
}
};
spannableString.setSpan(clickableSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new UnderlineSpan(), 0, spannableString.length(), 0);
spannableStringBuilder.append(spannableString);
spannableStringBuilder.append("to change this status");
myTextView.setText(spannableStringBuilder);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
Upvotes: 1