Search code examples
androidtextviewsettext

In Android, how to specify different text style in setText( )?


I want to create a setText that derives one textView but contains texts in different textStyle.

For instance, there are some codes below.And, I ask how to use different textStyle's in setText() without working on xml file.

    TextView textView1 = (TextView) findViewById(R.id.textView1);
    textView1.setText("The sum of numbers 1 to " + lastNumber + " is "+ sum);

Solution

  • You are exactly searching for this:

    Apply two different font styles to a TextView

    You can also check this

    Is it possible to have multiple styles inside a TextView?

    Or if you only want to set one single typeface use this:

        Typeface font = Typeface.createFromAsset(activity.getAssets(),
    fonts/androidnation.ttf");
        TextView title .setTypeface(font);
    

    Therefor the font must be in the font folder.