0 comments

Android set difference color for textView

Android textView you can set the text Italic or Bold or set The text Color for everywhere you want.

this is you string.xml
<string name="description">Your description where you want to set the red color <i>here you want to set italic</i> <b> here is your Bold text here</b></string>

your main.xml


    



and this is your java file
package monstercodz.blogspot.com;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

public class AndroidSetTextViewDifferenceColorActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SpannableStringBuilder text  = new SpannableStringBuilder(getResources().getText(R.string.description));
        int start = 32;
        int end = 52;
        TextView display = (TextView)findViewById(R.id.display);
        text.setSpan(new ForegroundColorSpan(Color.RED),start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        display.setText(text);
        //display.setTextColor(Color.BLUE);
        
    }
}

you can download the example project by Click here