Last active
April 15, 2016 13:56
-
-
Save AndrewReitz/621aa6074cb3a83fbb3291f83af2c94e to your computer and use it in GitHub Desktop.
View Utils Android Helpers for View Stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.andrewreitz.utils; | |
import android.text.Spannable; | |
import android.text.SpannableString; | |
import android.text.TextPaint; | |
import android.text.method.LinkMovementMethod; | |
import android.text.style.URLSpan; | |
import android.widget.TextView; | |
/** | |
* Simple utility class for operations on views. | |
*/ | |
public final class ViewUtils { | |
/** | |
* Removes URL underlines in a string by replacing URLSpan occurrences by | |
* URLSpanNoUnderline objects. | |
* | |
* @param text A TextView object. | |
*/ | |
public static void removeUnderlines(TextView text) { | |
Spannable spannable = new SpannableString(text.getText()); | |
URLSpan[] spans = spannable.getSpans(0, text.length(), URLSpan.class); | |
for (URLSpan span : spans) { | |
int start = spannable.getSpanStart(span); | |
int end = spannable.getSpanEnd(span); | |
spannable.removeSpan(span); | |
span = new URLSpanNoUnderline(span.getURL()); | |
spannable.setSpan(span, start, end, 0); | |
} | |
text.setText(spannable, TextView.BufferType.NORMAL); | |
text.setMovementMethod(LinkMovementMethod.getInstance()); | |
} | |
/** A UrlSpan that does not draw the underline. */ | |
private static final class URLSpanNoUnderline extends URLSpan { | |
public URLSpanNoUnderline(String url) { | |
super(url); | |
} | |
public void updateDrawState(TextPaint drawState) { | |
super.updateDrawState(drawState); | |
drawState.setUnderlineText(false); | |
} | |
} | |
private ViewUtils() { | |
throw new AssertionError("No Instances"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment