- 400+ Android & Flutter Code
- Saturday, January 3, 2015
- How to change TextView font size in android
- how to prevent system font-size changing effects to android application?
- 9 Answers 9
- How to change the Font Size in a whole Application programmatically, Android?
- 6 Answers 6
- How to get string width on Android?
- 7 Answers 7
- How to adjust text font size to fit textview
- 23 Answers 23
400+ Android & Flutter Code
Example code for android + flutter app developers.
Saturday, January 3, 2015
How to change TextView font size in android
TextView widget display text on android application. we can set or change TextView font size statically by declarative syntax in xml layout file or programmatically at run time in java file. even we can use an xml file source to define font size.
the following example code demonstrate us how can we define TextView font size in xml layout file and how can we uses dimens.xml to reference font size. in this example we did not changes any coding in java file, so here we only include the layout xml file and dimens.xml file.
Dimension
we defined dimension value in xml layout file as example 50sp, 20pt, 30dp. dimension is specified with a number followed by a unit of measure. android supported the following units of measure, those are dp, sp, pt, px, mm, in.
dp means Density-independent-Pixels. this is an abstract unit that is based on the physical density of the screen.
sp means Scale-independent-Pixels. SP as like dp but it is also scaled by the user’s font size preference. Sp is recommended unit for measure.
pt means Points — 1/72 of an inch based on the physical size of screen. px describe Pixels which corresponds to actual pixels on the screen. mm is Millimeters which is based on the physical size of the screen. in describe inches which also based on the physical size of the screen.
Источник
how to prevent system font-size changing effects to android application?
I recently finished developing my android application. I used sp (Scaled pixels) for all textSize. The problem is when i adjusted the system font-size, my application’s font-sizes are changing. I can use dp (Device independent pixels) but it will take too long to maintain my application.
I referenced text size from this.
Is there a way to prevent system font-size changing effects to my application ?
9 Answers 9
If you require your text to remain the same size, you’ll have to use dp .
An sp is the same base unit, but is scaled by the user’s preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes).
So you’re seeing the expected behaviour for using sp as your units for text size.
I don’t understand what you mean about using dp taking too long to maintain your app — as far as I can tell, it’ll exactly be the same amount of effort? (perhaps less, though it’ll likely make it less usable for users with poor eyesight)
I recently ran into this problem as well. Our UI didn’t scale well on phones with limited screen dimensions and changing the entire UI on the off chance a user set’s their Accessibility Options to «Huge» seemed silly.
I found this question on StackOverflow to be most helpful.
What I did was put the following code below in my BaseActivity (an Activity class that all my activities extend from)
And called it right after my super.onCreate() like so
What this code does is identify if the user set their font scale in Accessibility Settings to something greater than 1.30f (1.30f is «Large» on The Note 5, but probably varies a bit from device-to-device). If the user set their font too large («Extra Large», «Huge». ), we scale the application only to «Large».
This allows your app to scale to a user’s preferences (to a degree) without distorting your UI. Hopefully this will help others. Good luck scaling!
Other Tips
If you want certain layouts to scale with your fonts (say. a RelativeLayout that you use as a backdrop against your fonts), you can set their width/height with sp instead of the classic dp. When a user changes their font size, the layout will change accordingly with the fonts in your application. Nice little trick.
Источник
How to change the Font Size in a whole Application programmatically, Android?
I have created Spinner with the list of Font Sizes from «8» to «46» . I could be able to click the font Size and in a spinner it has shown me .
My need is, if i click the Font Size «26» inside a Spinner then it should be applied to my whole project. Like applying to my Screen, Textview appearance, Edittext — Bold/ Italic, etc. Again if i click 46 size then it should be apply to my whole project.
How could i do this by programmatically ?
6 Answers 6
Android documentation is not specific on the most efficient way to change the font size globally through the user’s selection at application level.
There is a problem I think with the answer given by Black Devil.
The problem is that many of the Android widgets subclass TextView , such as Button , RadioButton , and CheckBox . Some of these are indirect subclasses of TextView , which makes implementing customized version of TextView in these classes very difficult.
However as pointed out by Siddharth Lele in his comment, using styles or themes is much better way to handle change in the text size throughout the app.
We set styles for layouts to control the look and feel of the view. Themes are essentially just collections of these styles. However, we can use a theme just for text size settings; without defining values for every property. Using a theme over styles provides us with one huge advantage: we can set a theme for the entire view programmatically.
theme.xml
Create a class to handle loading our preferences:
Finally, create activities by extending BaseActivity, like this:
As most of the application have a much fewer amount of Activities than TextViews or widgets that inherit TextView. This will be exponentially so as complexity increases, so this solution requires less changes in code.
Источник
How to get string width on Android?
I would like to get height too if possible.
7 Answers 7
You can use the getTextBounds(String text, int start, int end, Rect bounds) method of a Paint object. You can either use the paint object supplied by a TextView or build one yourself with your desired text appearance.
Using a Textview you Can do the following:
If you just need the width you can use:
There are two different width measures for a text. One is the number of pixels which has been drawn in the width, the other is the number of ‘pixels’ the cursor should be advanced after drawing the text.
paint.measureText and paint.getTextWidths returns the number of pixels (in float) which the cursor should be advanced after drawing the given string. For the number of pixels painted use paint.getTextBounds as mentioned in other answer. I believe this is called the ‘Advance’ of the font.
For some fonts these two measurements differ (alot), for instance the font Black Chancery have letters which extend past the other letters (overlapping) — see the capital ‘L’. Use paint.getTextBounds as mentioned in other answer to get pixels painted.
I have measured width in this way:
This would help you.
Most likely you want to know the painted dimensions for a given string of text with a given font (i.e. a particular Typeface such as the “sans-serif” font family with a BOLD_ITALIC style, and particular size in sp or px).
Rather than inflating a full-blown TextView , you can go lower level and work with a Paint object directly for single-line text, for example:
For multi-line or spanned text ( SpannedString ), consider using a StaticLayout , in which you provide the width and derive the height. For a very elaborate answer on measuring and drawing text to a canvas in a custom view doing that, see: https://stackoverflow.com/a/41779935/954643
Also worth noting @arberg’s reply below about the pixels painted vs the advance width («number of pixels (in float) which the cursor should be advanced after drawing the given string»), in case you need to deal with that.
Источник
How to adjust text font size to fit textview
Is there any way in android to adjust the textsize in a textview to fit the space it occupies?
E.g. I’m using a TableLayout and adding several TextView s to each row. Since I don’t want the TextView s to wrap the text I rather see that it lowers the font size of the content.
I have tried measureText , but since I don’t know the size of the column it seems troublesome to use. This is the code where I want to change the font size to something that fits
23 Answers 23
The solution below incorporates all of the suggestions here. It starts with what was originally posted by Dunni. It uses a binary search like gjpc’s, but it is a bit more readable. It also include’s gregm’s bug fixes and a bug-fix of my own.
I’ve written a class that extends TextView and does this. It just uses measureText as you suggest. Basically it has a maximum text size and minimum text size (which can be changed) and it just runs through the sizes between them in decrements of 1 until it finds the biggest one that will fit. Not particularly elegant, but I don’t know of any other way.
Here is the code:
This is speedplane’s FontFitTextView , but it only decreases font size if needed to make the text fit, and keeps its font size otherwise. It does not increase the font size to fit height.
Here is an example how it could be used in xml:
This would keep the font size to 60sp as long as the text fits in width. If the text is longer, it will decrease font size. In this case, the TextView s height will also change because of height=wrap_content .
If you find any bugs, feel free to edit.
Here is my solution which works on emulator and phones but not very well on Eclipse layout editor. It’s inspired from kilaka’s code but the size of the text is not obtained from the Paint but from measuring the TextView itself calling measure(0, 0) .
The XML attribute file :
Check my github for the latest version of this class. I hope it can be useful for someone. If a bug is found or if the code needs explaination, feel free to open an issue on Github.
Here is an improved version of his response that also take care of height and comes with a maxFontSize attribute to limit font size (was useful in my case, so I wanted to share it) :
Corresponding /res/values/attr.xml file:
To use the new maxFontSize attribute, don’t forget to add xmlns:res-auto=»http://schemas.android.com/apk/res-auto» as show in the example.
You can now do this without a third party library or a widget. It’s built into TextView in API level 26. Add android:autoSizeTextType=»uniform» to your TextView and set height to it. That’s all. Use app:autoSizeTextType=»uniform» for backward compatibility
You can also use TextViewCompat for compatibility.
I had the same problem and wrote a class that seems to work for me. Basically, I used a static layout to draw the text in a separate canvas and remeasure until I find a font size that fits. You can see the class posted in the topic below. I hope it helps.
Use app:autoSizeTextType=»uniform» for backward compatibility because android:autoSizeTextType=»uniform» only work in API Level 26 and higher.
Slight modification to onMeasure:
And binary search on refitText:
I found the following to work nicely for me. It doesn’t loop and accounts for both height and width. Note that it is important to specify the PX unit when calling setTextSize on the view. Thanks to the tip in a previous post for this!
Here is the routine I use, passing in the getPaint() from the view. A 10 character string with a ‘wide’ character is used to estimate the width independent from the actual string.
Works with modification
You need to set the text view size like this because otherwise setTextSize assumes the value is in SP units:
And you needed to explicitly add this code.
I had this pain in my projects for soooo long until I found this library:
You just need to add the xml by your needs and it’s done. For example:
I used a variation of Dunni solution above, but that particular code didn’t work for me. In particular, when trying to use the Paint object set to have the traits of the view’s Paint object, and then calling measureText(), it doesn’t return the same value as directly calling the view’s Paint object. Perhaps there are some differences in the way my views are set up that make the behavior different.
My solution was to directly use the view’s Paint, even though there might be some performance penalties in changing the font size for the view multiple times.
I’ve been working on improving the excellent solution from speedplane, and came up with this. It manages the height, including setting the margin such that the text should be centered correctly vertically.
This uses the same function to get the width, as it seems to work the best, but it uses a different function to get the height, as the height isn’t provided anywhere. There are some corrections that need to be made, but I figured out a way to do that, while looking pleasing to the eye.
Google already made this feature.
Inspired by the previous posters I wanted to share my solution. It works with a scale factor which is applied to the previous font size to make it fit the available space. In addition to prevent unexpected behaviour of TextViews onDraw method, it simply draws the text on its own.
Источник