Skip to content Skip to sidebar Skip to footer

Use 2 (or More) Colors For A Button Text

I know I can change the color of the text on a button by the following ways : button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR or

Solution 1:

You should use ForegroundColorSpan

Try like this,

        Button b = (Button) findViewById(R.id.button1);
        SpannableString text = new SpannableString("Click Here");
        // make "Clicks" (characters 0 to 5) Red
        text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);
        // make "Here" (characters 6 to 10) Blue
        text.setSpan(new ForegroundColorSpan(Color.BLUE), 6, 10, 0);
        // shove our styled text into the Button
        b.setText(text, BufferType.SPANNABLE);

OutPut:

enter image description here

Hope this will help you.


Solution 2:

Yes it is possible

do like this

        Button btn = (Button) findViewById(R.id.btn);

        btn.setText(Html.fromHtml("<font color='red'>Click</font>"
            + "<font color='blue'> Here</font>"));

Post a Comment for "Use 2 (or More) Colors For A Button Text"