Created
February 26, 2013 20:05
-
-
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
This file contains hidden or 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
// 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