Skip to content

Instantly share code, notes, and snippets.

@chandruark
Created February 26, 2018 09:09
Show Gist options
  • Save chandruark/f8b91a6ed8907e6a7635f21375b0b2cb to your computer and use it in GitHub Desktop.
Save chandruark/f8b91a6ed8907e6a7635f21375b0b2cb to your computer and use it in GitHub Desktop.
Change color for particular char in the TextView String.
/*default initializations for android activity file*/
/* here we gone a simple implementaion for Dollar Sign based on the type specified
R.string.Dollar ="$$$$"
*/
SpannableStringBuilder builder = new SpannableStringBuilder();
Textview textView_colchar=(TextView)findViewById(R.id.textView_colchar);
int type=1; //change based on the type needed ...
switch (type){
case 1:{
SpannableString str1= new SpannableString(getString(R.string.dollar));
str1.setSpan(new ForegroundColorSpan(Color.CYAN), 0, str1.length()-3, 0);
builder.append(str1);
textView_colchar.setText( builder, TextView.BufferType.SPANNABLE);
}
break;
case 2:{
SpannableString str1= new SpannableString(getString(R.string.dollar));
str1.setSpan(new ForegroundColorSpan(Color.CYAN), 0, str1.length()-2, 0);
builder.append(str1);
textView_colchar.setText( builder, TextView.BufferType.SPANNABLE);
}
break;
case 3:{
SpannableString str1= new SpannableString(getString(R.string.dollar));
str1.setSpan(new ForegroundColorSpan(Color.CYAN), 0, str1.length()-1, 0);
builder.append(str1);
textView_colchar.setText( builder, TextView.BufferType.SPANNABLE);
}
break;
case 4:{
SpannableString str1= new SpannableString(getString(R.string.dollar));
str1.setSpan(new ForegroundColorSpan(Color.CYAN), 0, str1.length(), 0);
builder.append(str1);
textView_colchar.setText( builder, TextView.BufferType.SPANNABLE);
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment