1 /* 2 * Copyright (C) 2008 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 android.text.style; 18 19 import android.annotation.NonNull; 20 import android.text.TextPaint; 21 import android.view.View; 22 23 /** 24 * If an object of this type is attached to the text of a TextView 25 * with a movement method of LinkMovementMethod, the affected spans of 26 * text can be selected. If selected and clicked, the {@link #onClick} method will 27 * be called. 28 * <p> 29 * The text with a <code>ClickableSpan</code> attached will be underlined and the link color will be 30 * used as a text color. The default link color is the theme's accent color or 31 * <code>android:textColorLink</code> if this attribute is defined in the theme. 32 * For example, considering that we have a <code>CustomClickableSpan</code> that extends 33 * <code>ClickableSpan</code>, it can be used like this: 34 * <pre>{@code SpannableString string = new SpannableString("Text with clickable text"); 35 *string.setSpan(new CustomClickableSpan(), 10, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre> 36 * <img src="{@docRoot}reference/android/images/text/style/clickablespan.png" /> 37 * <figcaption>Text with <code>ClickableSpan</code>.</figcaption> 38 */ 39 public abstract class ClickableSpan extends CharacterStyle implements UpdateAppearance { 40 private static int sIdCounter = 0; 41 42 private int mId = sIdCounter++; 43 44 /** 45 * Performs the click action associated with this span. 46 */ onClick(@onNull View widget)47 public abstract void onClick(@NonNull View widget); 48 49 /** 50 * Makes the text underlined and in the link color. 51 */ 52 @Override updateDrawState(@onNull TextPaint ds)53 public void updateDrawState(@NonNull TextPaint ds) { 54 ds.setColor(ds.linkColor); 55 ds.setUnderlineText(true); 56 } 57 58 /** 59 * Get the unique ID for this span. 60 * 61 * @return The unique ID. 62 * @hide 63 */ getId()64 public int getId() { 65 return mId; 66 } 67 } 68