Cómo colorear texto utilizando Regex en android
Tengo tres expresiones regulares:
Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)"); Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)"); Pattern urlPattern = Patterns.WEB_URL;
Tengo una cadena:
- PHP - RegEx Números de teléfono con o sin código de país
- Jsoup obtiene elemento en value = ""
- Grupos java regex con android
- Cómo utilizar la expresión regular en android
- Manejo de spoiler BBcode Android
Este es un #sample #twitter texto de @tom_cruise con un enlace http://tom_cruise.me
Necesito emparejar este texto con la expresión regular tres arriba y colorear el texto emparejado con azul y fijar el texto final en un TextView
. ¿Cómo puedo lograrlo?
Hay que mencionar que no necesito Linkify
el texto, sólo color. Y no estoy usando Twitter4j
Library.
- Regex <img> Análisis de etiquetas con src, width, height
- Java.util.regex.PatternSyntaxException en Android
- Cómo ejecutar una consulta con regexp en Android
- Regex no funciona en Android pero funciona bien en Java
- Regex para eliminar espacios entre números solamente
- Cómo encontrar elementos por subcadena de ID usando la sintaxis del selector Jsoup?
- ¿Qué regex se puede utilizar para filtrar dalvikvm y dalvikvm-heap mensajes desde el logcat
- Cómo reemplazar una cadena si no está contenida entre '<' y '>'
He reemplazado http://tom_cruise.me
con http://www.google.com
. Pruebe lo siguiente:
String a = "This is a #sample #twitter text of @tom_cruise with a link http://www.google.com"; Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)"); Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)"); Pattern urlPattern = Patterns.WEB_URL; StringBuffer sb = new StringBuffer(a.length()); Matcher o = hashtagPattern.matcher(a); while (o.find()) { o.appendReplacement(sb, "<font color=\"#437C17\">" + o.group(1) + "</font>"); } o.appendTail(sb); Matcher n = mentionPattern.matcher(sb.toString()); sb = new StringBuffer(sb.length()); while (n.find()) { n.appendReplacement(sb, "<font color=\"#657383\">" + n.group(1) + "</font>"); } n.appendTail(sb); Matcher m = urlPattern.matcher(sb.toString()); sb = new StringBuffer(sb.length()); while (m.find()) { m.appendReplacement(sb, "<font color=\"#EDDA74\">" + m.group(1) + "</font>"); } m.appendTail(sb); textView.setText(Html.fromHtml(sb.toString()));
Eche un vistazo a SpannableString
y SpannableStringBuilder
. Un ejemplo de uso de SpannableStringBuilder
está disponible en https://stackoverflow.com/a/16061128/1321873
Podrías escribir un método que acepte la String
sin estilo y devuelve un CharSequence
así:
private CharSequence getStyledTweet(String tweet){ SpannableStringBuilder stringBuilder = new SpannableStringBuilder(tweet); //Find the indices of the hashtag pattern, mention pattern and url patterns //and set the spans accordingly //... return stringBuilder; }
y luego utilice el valor devuelto de lo anterior para establecer el texto de TextView
TextView tView = (TextView)findViewById(R.id.myText); tView.setText(getStyledTweet(tweet));
- Java / Android espera hasta que haya terminado un hilo antes de ejecutar un nuevo método
- Cómo buscar NULL en la consulta de base de datos Android y la sentencia OR en la selección