문제

I have one text view in android which text is "Hi how are you?"

My query is can we keep color of "hi how" as blue and "are you?" as red in one line as below

See this image

cause now am using 2 text view to show "hi how" and "are you" respectively but i want to show it in one text view with above requirement?

can we give two color code at a time to one text in String file? any reference?

도움이 되었습니까?

해결책 2

Try this:

String text = "<font color=#0000FF>Hi How</font> <font color=#ff0000>are you</font>";
mTxtVw.setText(Html.fromHtml(text));

다른 팁

For this purpose you need a SpannableString. Here the documentation

try below code:-

TextView TV = (TextView)findViewById(R.id.mytextview01);

Spannable wordtoSpan = new SpannableString("hi how are you?");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top