How to get same textSize across Android devices? - android

I my Android app I have a TextView with a width of 50% of the screen width, and a height of 10% of the screen height. I've set the textSize to 44sp, but on big devices (screen size) it renders the text too small, and too big on small devices. How can I solve this?

use folder like for dimens.xml file
values
values-sw600dp
values-sw720dp

I suggest you set both width and height of your TextView to wrap_content and use sp for your text size.
Setting your text size to 44sp with wrap_content should work pretty well for most devices.
If you still need to adjust certain devices (densities) you can extract your text size into its own dimen resource and override its value for default configurations, like shown here

Related

Android: text cropped in some devices

In my android app, there is a certain layout with a fixed height. In this layout, there are some TextView s arranged vertically. I have set the font size of text fields to be somewhat larger (18sp).
In almost all the devices I checked, The text fits inside the fixed sized layout. But in few devices, The bottom TextView gets cropped out of the layout.
If the proportion between text unit size (sp) and the layout unit height (dp) is the same in all devices, I wouldn't expect this to happen.
So, is this due to something wrong with the devices? Is there any way I can fix this?
Screen density is different than screen dimensions. dp normalizes densities across devices, but they can still have varying screen heights and widths. The obvious example is tablet vs phones, tablets have the same screen densities as phones but much larger screen dimensions. Just like that, some phones have smaller screens than others, so you need to take that into account when using fixed height layouts.
To get around this problem, you can take advantage of the width and smallest-width resource buckets and provide different layouts for smaller devices.
As it seems, the proportion between a unit sp and a unit dp depends on the font size setting (Settings->Display->Font size).
I could recreate the issue on other devices when I set the font size to be Huge.
So, it is not a good idea to set fixed sizes to layouts containing text.

What's the relationship between the font size and the height of TextView in android?

I occured a problem when using RadioButton. The RadioButton's layout_height must be 15dp, and the offical design guide says that 1sp is 1dp, so I set the font size to 1sp, but the TextView's layout_height is a little bigger than 15dp. It seems that when the font size is 15sp, the layout_height of the TextView is 18dp. So what's the relationship between the font size and the layout_height?
First thing I wanted to mention is the font-padding, which is by default included into the TextView size. So to make TextView's size closer to the size of the text itself - specify android:includeFontPadding="false"
Now, if you compare View with layout_height equals to 20 sp and TextView with TextSize 20sp - they are going to be almost equally high.
(There's small difference, as text might contain very high letter like "Å" or the letter like "y" which goes below the baseline)
About sp vs dp.
Scale-independent Pixels - This is like the dp unit, but it is also
scaled by the user's font size preference. It is recommend you use
this unit when specifying font sizes, so they will be adjusted for
both the screen density and the user's preference.
It depends on how big is your font (Settings->Accessibility->Enable/Disable Large Text). You can change it and see the effect.
Hope, it helps.

Specify icon size as a function of font size

The app design that I'm implementing requires icon sizes to be a function of font size. The height of the icon should roughly match the height of the font.
It appears to be possible to specify any view size in sp.
Google has decreed that layout sizes should not be specified in sp but is there anything to stop me specifying an ImageView size in sp? It appears that nobody else is doing this and I'm worried about falling into unforeseen traps.
You could use the align options to size the icon
android:layout_alignTop="#+id/your_font_view"
android:layout_alignBottom="#+id/your_font_view"
and set android:scaleType to the appropriate value.
It would probably be best to be in a RelativeLayout as well
From android dev site
"A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. 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)."
Yes, you can safely specify the ImageView size in sp. This is not recommended for layouts because they typically scale to fit their content and specifying their size in sp would a) be unnecessary and b) probably look bad.

Android button and the size of text

I have a layout with a few buttons that takes the space of the entire layout using weight properties.
Each button has some text on it with a fixed size which is set by android:textSize="30dp" so on a screen size of a small mobile device (A phone) the text's size if about the size of the button. But on a large device (A tablet) the button size expands yet the text size remains the same (30dp).
I want the button size and text size ratio to be maintained in every resolution, can it be done using the layout XML code? I would like to avoid doing it programmatically.
The solution is very simple
You should use a dimension.xml with diferent sizes each density/size and place it on:
values-mdpi
Values-hdpi
values-xhdpi
values-large (5.1' 5.4' y nexus7 tablets/phones )
values-xlarge (bigger tablets)
You can use values-sw600dp (nexus 7) and values-sw720dp(10') too.
Ah! And use sp for text, not dp. SP put the same size for all the fonts. Text Allways with SP
Remember, you allways can create values/drawables/resources in general for a lot of separate configurations: Landscape portrati, langauge, sizes, densities, night or day, version of android .... And you can merge it!
http://developer.android.com/intl/es/guide/topics/resources/providing-resources.html#SmallestScreenWidthQualifier
text size should be mentioned in sp, not in dp try like this
android:textSize="30sp"
Size of the text. Recommended dimension type for text is "sp" for scaled-pixels.
make different xml layout file for tabs and phone and try this for button
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Large"
you can fix the size of buttons by giving weight and for the text of a button you can do this to auto size them:
android:autoSizeTextType="uniform"
android:autoSizeMaxTextSize="24sp"
android:autoSizeMinTextSize="20sp"
Set the min and max size what you need and put these codes inside a button body:)

Android - text size on particular resolution - setting programatically

I am programming application, which will be used on various kinds of resolutions, but let's say, we would keep to the density. For medium, high and low I am setting it's own drawables, but it still doesn's solve my problem, so I am wondering, how to set values dependently on actual pixel screen density in Java code?
In this time I have to in XML set smaller letters and smaller textviews to fit it into low resolution screen, on the other hand, high density screen is used from about 60% and text fields are too small. I am wondering if there is some way how to, during the start of the program, in some If loop find out, what the resolution is and then set layout_height and textSize for items I want.
Thanks
edit: Solved myself - http://developer.android.com/reference/android/util/DisplayMetrics.html
well before moving ahead to set the text size programatically, I recommend you to set the Textsize property's value in dip instead of px. it seems that you have marked the textsize value in px.

Categories

Resources