Change Font size programmatically in Android

11,761

Solution 1

You do not change the font size of a String instead you change the font size of the text when you display the String(for instance in the TextView if you are using one). A String is simply a data object holding the text you want to display and has nothing to do with the way you display it.

Solution 2

Use

TextView textView = (TextView) findViewById(R.id.textView);
Spannable span = new SpannableString(str);
span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);

on a TextView to get different TextSizes.

Solution 3

When you are putting the text into a textView you can increase the font. For example

textView.setText(yourString);
textView.setTextSize(20);

Or you can give the font size in the Layout.xml file itself

<TextView
      android:id = "@+id/textView"
       ........
       android:textSize = "20dp"/>

Please feel free to ask any further doubts if you need further clarifications.

Solution 4

You need to use a Spannable and give it to your TextView in order to modify just a portion of the text. To change the size use :

 span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Share:
11,761
Deepthi
Author by

Deepthi

Updated on June 04, 2022

Comments

  • Deepthi
    Deepthi about 2 years

    Hi I'm a newbie in android. I just wanted to know if there is any way to change the font size of a String in android?

    String name = db.getName(Id)
    String str = "Name : " + name;
    

    I want to have "Name" with bigger font size than the value in "name".Where "name" is the value I get from the database.

    Please do suggest any method to do this!! Thanks in advance!!

  • Deepthi
    Deepthi over 11 years
    Thanks for the response and what if I want to display the string in a listview
  • Deepthi
    Deepthi over 11 years
    Avadhani: Thanks for the help. So it seems its impossible to change font size of a String!! :(
  • pvn
    pvn over 11 years
    @Deepthi why does it seem so deepti, setTextSize() works. Do let me know, I will try to help out.
  • Avadhani Y
    Avadhani Y over 11 years
    @Deepthi yes. A string is just the representation of some text. Applying some style information depends on the GUI framework. Do u understand?
  • Stephane Mathis
    Stephane Mathis over 11 years
    You can do the same thing, but you will have to create a custom class that implements the adapter (ie: ArrayAdapter or BaseAdapter) that you need and override the getView() method.
  • Deepthi
    Deepthi over 11 years
    Avadhani: Yes I do.. but I wanted to display this string in a listview
  • Deepthi
    Deepthi over 11 years
    Stephane Mathis:k thanks a lot :) U mean I need to create a custom listview right?
  • Deepthi
    Deepthi over 11 years
    MoJo: Thanks for the help.. I'll have a look at it.. :)
  • Stephane Mathis
    Stephane Mathis over 11 years
    No, just a custom Adapter to give to your listview. Take a look a this example : http://www.ezzylearning.com/tutorial.aspx?tid=1763429 and the WeatherAdapter.java.
  • Avadhani Y
    Avadhani Y over 11 years
    Then use Custom Adapter... Set the String to the listview and change the size as well
  • Avadhani Y
    Avadhani Y over 11 years
    @Deepthi Found the solution for ur question from here?
  • Ankit Mishra
    Ankit Mishra over 3 years
    could you explain your answer please?