1 /* 2 * Copyright (C) 2007 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.apis.text; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.graphics.Typeface; 23 import android.os.Bundle; 24 import android.text.Html; 25 import android.text.SpannableString; 26 import android.text.Spanned; 27 import android.text.method.LinkMovementMethod; 28 import android.text.style.StyleSpan; 29 import android.text.style.URLSpan; 30 import android.widget.TextView; 31 32 public class Link extends Activity { 33 @Override onCreate(Bundle savedInstanceState)34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 setContentView(R.layout.link); 38 39 // text1 shows the android:autoLink property, which 40 // automatically linkifies things like URLs and phone numbers 41 // found in the text. No java code is needed to make this 42 // work. 43 44 // text2 has links specified by putting <a> tags in the string 45 // resource. By default these links will appear but not 46 // respond to user input. To make them active, you need to 47 // call setMovementMethod() on the TextView object. 48 49 TextView t2 = (TextView) findViewById(R.id.text2); 50 t2.setMovementMethod(LinkMovementMethod.getInstance()); 51 52 // text3 shows creating text with links from HTML in the Java 53 // code, rather than from a string resource. Note that for a 54 // fixed string, using a (localizable) resource as shown above 55 // is usually a better way to go; this example is intended to 56 // illustrate how you might display text that came from a 57 // dynamic source (eg, the network). 58 59 TextView t3 = (TextView) findViewById(R.id.text3); 60 t3.setText( 61 Html.fromHtml( 62 "<b>text3: Constructed from HTML programmatically.</b> Text with a " + 63 "<a href=\"http://www.google.com\">link</a> " + 64 "created in the Java source code using HTML.")); 65 t3.setMovementMethod(LinkMovementMethod.getInstance()); 66 67 // text4 illustrates constructing a styled string containing a 68 // link without using HTML at all. Again, for a fixed string 69 // you should probably be using a string resource, not a 70 // hardcoded value. 71 72 SpannableString ss = new SpannableString( 73 "text4: Manually created spans. Click here to dial the phone."); 74 75 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 30, 76 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 77 ss.setSpan(new URLSpan("tel:4155551212"), 31+6, 31+10, 78 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 79 80 TextView t4 = (TextView) findViewById(R.id.text4); 81 t4.setText(ss); 82 t4.setMovementMethod(LinkMovementMethod.getInstance()); 83 } 84 } 85