0

Can you guys tell me how to write a java code inside a textview and i want it colored just like i'm writing a java code. I found an example that is composed of 2 types of text and I think that both of them are TextView but the seconde one contain Highlight text and that green bar at left , can you guys tell how to do it . ( sorry i can't upload a picture )

  • Welcome to Stack Overflow! Please take a tour [How do I ask a good question?](//stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](//stackoverflow.com/help/mcve). – Olaia Apr 05 '17 at 10:54

2 Answers2

1

Try this,

public static void setText(TextView textView, String text) {
    Spannable spannable = new SpannableString(text);

    // For Foreground Highlight
    spannable.setSpan(new ForegroundColorSpan(fromColor(ColorCode)), highlight.start, highlight.end, 0);

    // For Background Highlight
    spannable.setSpan(new BackgroundColorSpan(fromColor(ColorCode)), highlight.start, highlight.end, 0);

    textView.setText(spannable);
}
Siraj Sumra
  • 934
  • 1
  • 11
  • 28
0

Try this,

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

Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);

Original answer here

Community
  • 1
  • 1
Wilson Christian
  • 650
  • 1
  • 6
  • 17
  • You didn't understand my question , i'm writing a long java code inside a textview and i want it to Highlight the important words like "class" , "void" "public" just like they appear in java code , if you still didn't get the point tell me so i can send you the picture – hamid saad Apr 05 '17 at 09:48