Add Clickable links to TextView in Sketchware

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 &lt;a&gt; markup.</b> This has markup for a <a href="http://www.google.com">link</a> specified via an &lt;a&gt; 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());


Comments

  1. Hi! Thanks for the relentless efforts. I have learned a whole lot from you. But, sir, I want to please send me a guide on Hi! How has been? Have found some help or a way foreward? Sorry, bro. Can you help me with how I can create a moreblock that does : selected CheckBox: Checbox .... to answer ......... That is to say, it will allow me pick a checkbox to fill the first ...... and allow me to make input to the second ... ?

    ReplyDelete
    Replies
    1. Check this video. It may help https://youtu.be/H2OhtwLemug

      Delete
  2. emmyname7746@gmail.com is mine for the help,

    ReplyDelete
  3. How to read and download PDF file from webpage on Sketchware. Pls teach me.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  4. How can you use buttons to search withim a pargraph page, id rather ise buttons than search box? thanks

    ReplyDelete
  5. How can you use buttons to search withim a pargraph page, id rather ise buttons than search box? thanks

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware