Add Clickable links to TextView in Sketchware
2 minute read
Here are two ways of adding clickable links to TextView in Sketchware.
Method 1
1. In main.xml add a TextView text_html.
2. Create a String html_string.
3. In onCreate event:
a. Set the text of html_string to your text in html form with links declared in <a> tag ( <a href="http://google.com"> Google </a> will display 'Google' in link form).
So let's set String html_string to:
<b>text_html_program: Explicit links using <a> markup.</b> This has markup for a <a href="http://www.google.com">link</a> specified via an <a> tag. Use a "tel:" URL to <a href="tel:4155551212">dial a phone number</a>.
b. Display this String in TextView. In an add source directly block put following codes:
text_html.setText( Html.fromHtml(html_string));
text_html.setMovementMethod(android.text.method.LinkMovementMethod.getInstance());
Method 2
1. In main.xml add a TextView text_spannable.
2. Create a String mystring.
3. In onCreate event:
a. Set the text of mystring to your text.
So let's set String mystring to:
text_spannable: Manually created spans. Click here to dial the phone and here to visit Yahoo.
b. Use an add source directly block put following codes:
// Convert this String to a Spannable String ss.
SpannableString ss = new SpannableString(mystring);
// Make Substring 0-39 bold
ss.setSpan(new android.text.style.StyleSpan(Typeface.BOLD), 0, 39, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
// linkify substring 46-50 (first 'here') and substring 73-77 (second 'here') of ss.
ss.setSpan(new android.text.style.URLSpan("tel:4155551212"), 46, 50, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new android.text.style.URLSpan("http://yahoo.com"), 73, 77, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Display Spannable String in TextView.
text_spannable.setText(ss);
text_spannable.setMovementMethod(android.text.method.LinkMovementMethod.getInstance());
Watch Video below:
ClickableSpan Example
This example shows how to open a new app page or Activity by clicking on a link in TextView text.
1. In main.xml add a TextView textview1.
2. Create a new page details.xml.
3. In MainActivity.java, create a new String mystring.
4. In onCreate of MainActivity.java:
a. Set the text of mystring to your text.
So let's set String mystring to:
You can click here to find the Complete code.
b. Use an add source directly block put following codes:
// Convert this String to a Spannable String ss.
SpannableString ss = new SpannableString(mystring);
// Create a new ClickableSpan and set onClick event for it. In onClick event use Intent to move to DetailsActivity.
android.text.style.ClickableSpan clickableSpan1 = new android.text.style.ClickableSpan() {
@Override
public void onClick(View widget) {
Intent a = new Intent(MainActivity.this, DetailsActivity.class); startActivity(a);
}
};
// Make Substring 14-18 clickable
ss.setSpan(clickableSpan1, 14, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Display Spannable String in TextView.
textview1.setText(ss);
// Enable the link
textview1.setMovementMethod(android.text.method.LinkMovementMethod.getInstance());
Method 1
1. In main.xml add a TextView text_html.
2. Create a String html_string.
3. In onCreate event:
a. Set the text of html_string to your text in html form with links declared in <a> tag ( <a href="http://google.com"> Google </a> will display 'Google' in link form).
So let's set String html_string to:
<b>text_html_program: Explicit links using <a> markup.</b> This has markup for a <a href="http://www.google.com">link</a> specified via an <a> tag. Use a "tel:" URL to <a href="tel:4155551212">dial a phone number</a>.
b. Display this String in TextView. In an add source directly block put following codes:
text_html.setText( Html.fromHtml(html_string));
text_html.setMovementMethod(android.text.method.LinkMovementMethod.getInstance());
Method 2
1. In main.xml add a TextView text_spannable.
2. Create a String mystring.
3. In onCreate event:
a. Set the text of mystring to your text.
So let's set String mystring to:
text_spannable: Manually created spans. Click here to dial the phone and here to visit Yahoo.
b. Use an add source directly block put following codes:
// Convert this String to a Spannable String ss.
SpannableString ss = new SpannableString(mystring);
// Make Substring 0-39 bold
ss.setSpan(new android.text.style.StyleSpan(Typeface.BOLD), 0, 39, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
// linkify substring 46-50 (first 'here') and substring 73-77 (second 'here') of ss.
ss.setSpan(new android.text.style.URLSpan("tel:4155551212"), 46, 50, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new android.text.style.URLSpan("http://yahoo.com"), 73, 77, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Display Spannable String in TextView.
text_spannable.setText(ss);
text_spannable.setMovementMethod(android.text.method.LinkMovementMethod.getInstance());
Watch Video below:
ClickableSpan Example
This example shows how to open a new app page or Activity by clicking on a link in TextView text.
1. In main.xml add a TextView textview1.
2. Create a new page details.xml.
3. In MainActivity.java, create a new String mystring.
4. In onCreate of MainActivity.java:
a. Set the text of mystring to your text.
So let's set String mystring to:
You can click here to find the Complete code.
b. Use an add source directly block put following codes:
// Convert this String to a Spannable String ss.
SpannableString ss = new SpannableString(mystring);
// Create a new ClickableSpan and set onClick event for it. In onClick event use Intent to move to DetailsActivity.
android.text.style.ClickableSpan clickableSpan1 = new android.text.style.ClickableSpan() {
@Override
public void onClick(View widget) {
Intent a = new Intent(MainActivity.this, DetailsActivity.class); startActivity(a);
}
};
// Make Substring 14-18 clickable
ss.setSpan(clickableSpan1, 14, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Display Spannable String in TextView.
textview1.setText(ss);
// Enable the link
textview1.setMovementMethod(android.text.method.LinkMovementMethod.getInstance());