• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 @android.ravenwood.annotation.RavenwoodKeepWholeClass
40 public abstract class ClickableSpan extends CharacterStyle implements UpdateAppearance {
41     private static int sIdCounter = 0;
42 
43     private int mId = sIdCounter++;
44 
45     /**
46      * Performs the click action associated with this span.
47      */
onClick(@onNull View widget)48     public abstract void onClick(@NonNull View widget);
49 
50     /**
51      * Makes the text underlined and in the link color.
52      */
53     @Override
updateDrawState(@onNull TextPaint ds)54     public void updateDrawState(@NonNull TextPaint ds) {
55         ds.setColor(ds.linkColor);
56         ds.setUnderlineText(true);
57     }
58 
59     /**
60      * Get the unique ID for this span.
61      *
62      * @return The unique ID.
63      * @hide
64      */
getId()65     public int getId() {
66         return mId;
67     }
68 
69     @Override
toString()70     public String toString() {
71         return "ClickableSpan{}";
72     }
73 }
74