Skip to content

Instantly share code, notes, and snippets.

@adamsp
Created October 28, 2013 00:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamsp/7189653 to your computer and use it in GitHub Desktop.
Save adamsp/7189653 to your computer and use it in GitHub Desktop.
If you've got a TextView with clickable links setup using HTML and LinkMovementMethod ( http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable/2746708#2746708 ), this class allows you to hide either the underlines or the entire link - useful, for example, if you're showing tweets and need to link to profiles etc.
/**
* Copyright 2013 Adam Speakman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nz.net.speakman.android.example;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.style.URLSpan;
import android.widget.TextView;
/**
* Hides links within a text view.
*/
public class TextViewLinkHider {
class HiddenURLSpan extends URLSpan {
public HiddenURLSpan(URLSpan span) {
super(span.getURL());
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
}
class NonUnderlinedURLSpan extends URLSpan {
public NonUnderlinedURLSpan(URLSpan span) {
super(span.getURL());
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
/**
* Removes underlining from any URLSpans (links) within the supplied TextView.
*/
public void hideLinkUnderlines(TextView textView) {
Spannable text = (Spannable) textView.getText();
URLSpan[] spansToHide = text.getSpans(0, text.length(), URLSpan.class);
for (URLSpan spanToHide : spansToHide) {
hideSpan(text, spanToHide, new NonUnderlinedURLSpan(spanToHide));
}
}
/**
* Removes underlining and coloring from any URLSpans (links) within the supplied TextView.
*/
public void hideLinks(TextView textView) {
Spannable text = (Spannable) textView.getText();
URLSpan[] spansToHide = text.getSpans(0, text.length(), URLSpan.class);
for (URLSpan spanToHide : spansToHide) {
hideSpan(text, spanToHide, new HiddenURLSpan(spanToHide));
}
}
private void hideSpan(Spannable text, URLSpan spanToHide, URLSpan hiddenSpan) {
int start = text.getSpanStart(spanToHide);
int end = text.getSpanEnd(spanToHide);
int flags = text.getSpanFlags(spanToHide);
text.removeSpan(spanToHide);
text.setSpan(hiddenSpan, start, end, flags);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment