StyleUtils.java revision d3714d293cbfa2b3cce35e9c6e7b87a2538abb83
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; 27import com.android.mail.text.UrlSpan; 28 29/** 30 * Utility class for styling UI. 31 */ 32public class StyleUtils { 33 34 /** 35 * Removes any {@link android.text.style.URLSpan}s from the text view 36 * and replaces them with their non-underline version {@link com.android.mail.text.UrlSpan}. 37 */ 38 public static void stripUnderlines(TextView textView) { 39 final Spannable spannable = (Spannable) textView.getText(); 40 final URLSpan[] urls = textView.getUrls(); 41 42 for (URLSpan span : urls) { 43 final int start = spannable.getSpanStart(span); 44 final int end = spannable.getSpanEnd(span); 45 spannable.removeSpan(span); 46 span = new UrlSpan(span.getURL()); 47 spannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 48 } 49 } 50 51 /** 52 * Removes any {@link android.text.style.URLSpan}s from the text view and replaces them with a 53 * non-underline version {@link LinkStyleSpan} which calls the supplied listener when clicked. 54 */ 55 public static void stripUnderlinesAndLinkUrls(TextView textView, 56 View.OnClickListener onClickListener) { 57 final Spannable spannable = (Spannable) textView.getText(); 58 final URLSpan[] urls = textView.getUrls(); 59 60 for (URLSpan span : urls) { 61 final int start = spannable.getSpanStart(span); 62 final int end = spannable.getSpanEnd(span); 63 spannable.removeSpan(span); 64 spannable.setSpan(new LinkStyleSpan(onClickListener), start, end, 65 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 66 } 67 } 68 69 /** 70 * Removes any {@link android.text.style.URLSpan}s from the text view and replaces them with a 71 * non-underline version {@link LinkStyleSpan} that does nothing when clicked. 72 */ 73 public static void stripUnderlinesAndUrl(TextView textView) { 74 stripUnderlinesAndLinkUrls(textView, null /* onClickListener */); 75 } 76} 77