1
/*
2
* Copyright 2013 The Android Open Source Project
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
package com.example.android.textlinkify;
18
19
import android.app.Activity;
20
import android.graphics.Typeface;
21
import android.os.Bundle;
22
import android.text.Html;
23
import android.text.SpannableString;
24
import android.text.Spanned;
25
import android.text.method.LinkMovementMethod;
26
import android.text.style.StyleSpan;
27
import android.text.style.URLSpan;
28
import android.widget.TextView;
29
30
/**
31
* This sample demonstrates how clickable links can be added to a
32
* {@link android.widget.TextView}.
33
*
34
* <p>This can be done in three ways:
35
* <ul>
36
* <li><b>Automatically:</b> Text added to a TextView can automatically be linkified by enabling
37
* autoLinking. In XML, use the android:autoLink property, programatically call
38
* {@link android.widget.TextView#setAutoLinkMask(int)} using an option from
39
* {@link android.text.util.Linkify}</li>
40
*
41
* <li><b>Parsing a String as HTML:</b> See {@link android.text.Html#fromHtml(String)})</li>
42
*
43
* <li><b>Manually by constructing a {@link android.text.SpannableString}:</b> Consisting of
44
* {@link android.text.style.StyleSpan} and {@link android.text.style.URLSpan} objects that
45
* are contained within a {@link android.text.SpannableString}</li>
46
* </ul></p>
47
*
48
*/
49
public class MainActivity extends Activity {
50
51
@Override
52
protected void onCreate(Bundle savedInstanceState) {
53
super.onCreate(savedInstanceState);
54
55
setContentView(R.layout.sample_main);
56
58
/*
59
* text_auto_linkify shows the android:autoLink property, which
60
* automatically linkifies things like URLs and phone numbers
61
* found in the text. No java code is needed to make this
62
* work.
63
* This can also be enabled programmatically by calling
64
* .setAutoLinkMask(Linkify.ALL) before the text is set on the TextView.
65
*
66
* See android.text.util.Linkify for other options, for example only
67
* auto-linking email addresses or phone numbers
68
*/
70
72
/*
73
* text_html_resource has links specified by putting anchor tags (<a>) in the string
74
* resource. By default these links will appear but not
75
* respond to user input. To make them active, you need to
76
* call setMovementMethod() on the TextView object.
77
*/
78
TextView textViewResource = (TextView) findViewById(R.id.text_html_resource);
79
textViewResource.setText(
80
Html.fromHtml(getResources().getString(R.string.link_text_manual)));
81
textViewResource.setMovementMethod(LinkMovementMethod.getInstance());
83
85
/*
86
* text_html_program shows creating text with links from HTML in the Java
87
* code, rather than from a string resource. Note that for a
88
* fixed string, using a (localizable) resource as shown above
89
* is usually a better way to go; this example is intended to
90
* illustrate how you might display text that came from a
91
* dynamic source (eg, the network).
92
*/
93
TextView textViewHtml = (TextView) findViewById(R.id.text_html_program);
94
textViewHtml.setText(
95
Html.fromHtml(
96
"<b>text_html_program: Constructed from HTML programmatically.</b>"
97
+ " Text with a <a href=\"http://www.google.com\">link</a> "
98
+ "created in the Java source code using HTML."));
99
textViewHtml.setMovementMethod(LinkMovementMethod.getInstance());
101
103
/*
104
* text_spannable illustrates constructing a styled string containing a
105
* link without using HTML at all. Again, for a fixed string
106
* you should probably be using a string resource, not a
107
* hardcoded value.
108
*/
109
SpannableString ss = new SpannableString(
110
"text_spannable: Manually created spans. Click here to dial the phone.");
111
112
/*
113
* Make the first 38 characters bold by applying a StyleSpan with bold typeface.
114
*
115
* Characters 45 to 49 (the word "here") is made clickable by applying a URLSpan
116
* pointing to a telephone number. Clicking it opens the "tel:" URL that starts the dialer.
117
*
118
* The SPAN_EXCLUSIVE_EXCLUSIVE flag defines this span as exclusive, which means
119
* that it will not expand to include text inserted on either side of this span.
120
*/
121
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 39,
122
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
123
ss.setSpan(new URLSpan("tel:4155551212"), 40 + 6, 40 + 10,
124
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
125
126
TextView textViewSpan = (TextView) findViewById(R.id.text_spannable);
127
textViewSpan.setText(ss);
128
129
/*
130
* Set the movement method to move between links in this TextView.
131
* This means that the user traverses through links in this TextView, automatically
132
* handling appropriate scrolling and key commands.
133
*/
134
textViewSpan.setMovementMethod(LinkMovementMethod.getInstance());
136
}
137
138
}