user3767207
user3767207

Reputation: 33

Changing the font of String

I am trying to change the font size of string which is displaying on textview. remember there are two string which are displaying on same textview.I want different font size for both string.

String name = shopsArrayList.get(position);
String address = shopsAddress.get(position);

i tried this but it is done on both string,

tv.setText(name.toUpperCase() + "\r\n" + address);
tv.setPadding(25, 15, 0, 0);
tv.setTextSize(25);

please help!!!

Upvotes: 0

Views: 2811

Answers (5)

Abdullah AlHazmy
Abdullah AlHazmy

Reputation: 1299

Try to using Html format, Something like this:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        myTextView.setText(Html.fromHtml("<p style='font-family: serif;'>Description here</p><br><p style='font-family: Times;'>Blah Blah Blah</p>", Html.FROM_HTML_MODE_LEGACY));
    } else {
        myTextView.setText(Html.fromHtml("<p style='font-family: serif;'>Description here</p><br><p style='font-family: Times;'>Blah Blah Blah</p>"));
    }

Upvotes: 1

Jiju Induchoodan
Jiju Induchoodan

Reputation: 4266

A dynamic way to acheive the same

    String name = shopsArrayList.get(position);
    String address = shopsAddress.get(position);

    int nameLength = name.length();
    int addressLength = address.length();
    Spannable span = new SpannableString(name+address);
    span.setSpan(new StyleSpan(Typeface.BOLD),0, nameLength,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new StyleSpan(Typeface.ITALIC),nameLength+1, addressLength,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

now set the text to textview

textView.setText(span);

Upvotes: 0

CodeWarrior
CodeWarrior

Reputation: 5176

You can first set style for that

<style name="firstStyle">
    <item name="android:textSize">@dimen/regular_text</item>
</style>
<style name="secondStyle">
    <item name="android:textSize">@dimen/bullet_text</item>
</style>

Then you have to make a single string from those two string of yours and specify the length of your string where you need to apply for effect.

SpannableString span = new SpannableString(myString);

span.setSpan(new TextAppearanceSpan(getContext(), R.style.firstStyle),0,15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new TextAppearanceSpan(getContext(), R.style.secondStyle),16,30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

tv.setText(span, TextView.BufferType.SPANNABLE);

Upvotes: 0

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You can do that easily using SpannableString.

See this example:

String str= "Hello World";
SpannableString spannable=  new SpannableString(str);
spannable.setSpan(new RelativeSizeSpan(1.5f), 0, 5, 0); //size
spannable.setSpan(new ForegroundColorSpan(Color.CYAN), 0, 5, 0);//Color
TextView txtview= (TextView) findViewById(R.id.textview);
txtview.setText(spannable); 

Hope this helps.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93708

Put the second string into a Span that allows you to change the style of the text, such as a TextAppearanceSpan. http://developer.android.com/reference/android/text/style/TextAppearanceSpan.html

Upvotes: 0

Related Questions