StyleUtils.java revision afaab1752ab5b507cdaad7b3619ffc1c9728368f
1/*
2 * Copyright (C) 2014 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.utils;
19
20import android.text.Spannable;
21import android.text.Spanned;
22import android.text.style.URLSpan;
23import android.view.View;
24import android.widget.TextView;
25
26import com.android.mail.text.LinkStyleSpan;
27
28/**
29 * Utility class for styling UI.
30 */
31public class StyleUtils {
32    /**
33     * Removes any {@link android.text.style.URLSpan}s from the text view and replaces them with a
34     * non-underline version {@link LinkStyleSpan} which calls the supplied listener when clicked.
35     */
36    public static void stripUnderlinesAndLinkUrls(TextView textView,
37                                                  View.OnClickListener onClickListener) {
38        final Spannable spannable = (Spannable) textView.getText();
39        final URLSpan[] urls = textView.getUrls();
40
41        for (URLSpan span : urls) {
42            final int start = spannable.getSpanStart(span);
43            final int end = spannable.getSpanEnd(span);
44            spannable.removeSpan(span);
45            spannable.setSpan(new LinkStyleSpan(onClickListener), start, end,
46                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
47        }
48    }
49
50    /**
51     * Removes any {@link android.text.style.URLSpan}s from the text view and replaces them with a
52     * non-underline version {@link LinkStyleSpan} that does nothing when clicked.
53     */
54    public static void stripUnderlinesAndUrl(TextView textView) {
55        stripUnderlinesAndLinkUrls(textView, null /* onClickListener */);
56    }
57}
58