Skip to content

Instantly share code, notes, and snippets.

@raultm
Created February 26, 2013 20:05
Show Gist options
  • Save raultm/5041668 to your computer and use it in GitHub Desktop.
Save raultm/5041668 to your computer and use it in GitHub Desktop.
Código de ejemplo para marcar la última palabra de una cadena con un color de text y de fondo concretos
// Método para cambiar estilos a la última palabra
private Spannable highlightLastWordOfSentence(String sentence, int backgroundColor, int foregroundColor){
int startIndexOfLastWord = sentence.lastIndexOf(" ") + 1;
int endIndexOfLastWord = sentence.length();
SpannableStringBuilder spannable = new SpannableStringBuilder(sentence);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(backgroundColor);
spannable.setSpan(backgroundSpan, startIndexOfLastWord, endIndexOfLastWord, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(foregroundColor);
spannable.setSpan(foregroundSpan, startIndexOfLastWord, endIndexOfLastWord, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
return spannable;
}
// [...]
// En nuestra lógica de la aplicación
TextView openStatsTextView = (TextView)layout.findViewById(R.id.main_close_status);
Spannable spannable = highlightLastWordOfSentence(
openStatsTextView.getText().toString(),
getResources().getColor(R.color.close_font_color),
getResources().getColor(R.color.close_main_color)
);
openStatsTextView.setText(spannable);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment