2

my requirement is to show the Text in the TextView in multicoloured like rainbow color text and Bold, how can I achieve this.and I need to display them dynamically using java code..

TextView text=new TextView(context);
                        text.setText(status);
                    text.setBackgroundResource(R.drawable.grd_btn);
                    text.setGravity(Gravity.CENTER);
                    text.setPadding(2, 0, 2, 0);
                    text.setTypeface(font2,Typeface.BOLD);
                    text.setTextColor(Color.WHITE);
Andro Selva
  • 53,910
  • 52
  • 193
  • 240

3 Answers3

9

Hey sorry for the delay my friend. Had to work on your problem and it took a while. So first goes the output,

enter image description here

So assuming that the above shown is your needed output here is the code to it.

xml file

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"/>

res file (say strings.xml)

<color  name="violet">#9400D3</color>
<color  name="indigo">#4B0082</color>
<color  name="blue">#0000FF</color>
<color  name="green">#00FF00</color>
<color  name="yellow">#FFFF00</color>
<color  name="orange">#FF7F00</color>
<color  name="red">#FF0000</color>

your java file

     TextView textView = (TextView)findViewById(R.id.textView1);
        Shader textShader=new LinearGradient(0, 0, 0, 20,
                new int[]{getResources().getColor(R.color.violet),getResources().getColor(R.color.indigo),
                getResources().getColor(R.color.blue),
                getResources().getColor(R.color.green),
                getResources().getColor(R.color.yellow),
                getResources().getColor(R.color.orange),
                getResources().getColor(R.color.red)},
                new float[]{0,0.2f,0.4f,0.6f,0.8f,0.9f,1}, TileMode.CLAMP);
        textView.getPaint().setShader(textShader);
        textView.setTextSize(20);

That's it. And for your bold style follow the below link of my previous answer,

https://stackoverflow.com/a/5169604/603744

Community
  • 1
  • 1
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
1
 String text = "This is <font color='red'>red</font>. This is <font     color='blue'>blue</font>.";
 textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
 textView.setTypeface(null,Typeface.BOLD);
Jambaaz
  • 2,788
  • 1
  • 19
  • 30
0

This was easiest for me

All Credits to: https://gist.github.com/ishitcno1/0c8bcb8ad72cb0879acb

public class RainbowTextView extends TextView {
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    int[] rainbow = getRainbowColors();
    Shader shader = new LinearGradient(0, 0, 0, w, rainbow, 
        null, Shader.TileMode.MIRROR);

    Matrix matrix = new Matrix();
    matrix.setRotate(90);
    shader.setLocalMatrix(matrix);

    getPaint().setShader(shader);
  }

  private int[] getRainbowColors() {
    return new int[] {
      getResources().getColor(R.color.rainbow_red),
      getResources().getColor(R.color.rainbow_yellow),
      getResources().getColor(R.color.rainbow_green),
      getResources().getColor(R.color.rainbow_blue),
      getResources().getColor(R.color.rainbow_purple)
    };
  }
}