Change Font size programmatically in Android
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);
Deepthi
Updated on June 04, 2022Comments
-
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 over 11 yearsThanks for the response and what if I want to display the string in a listview
-
Deepthi over 11 yearsAvadhani: Thanks for the help. So it seems its impossible to change font size of a String!! :(
-
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 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 over 11 yearsYou 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 over 11 yearsAvadhani: Yes I do.. but I wanted to display this string in a listview
-
Deepthi over 11 yearsStephane Mathis:k thanks a lot :) U mean I need to create a custom listview right?
-
Deepthi over 11 yearsMoJo: Thanks for the help.. I'll have a look at it.. :)
-
Stephane Mathis over 11 yearsNo, 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 over 11 yearsThen use Custom Adapter... Set the String to the listview and change the size as well
-
Avadhani Y over 11 years@Deepthi Found the solution for ur question from here?
-
Ankit Mishra over 3 yearscould you explain your answer please?