Ok I know that there are already many questions that ask the same thing as what I am asking, however the problem is that none of those solutions have worked for me or are what I want. My problem is that when I use the setTextSize method of the Paint class, the text appears to be too big on smaller screens, and smaller on bigger screens. What I want is for me to be able to change it to the size I want on my device, and then on any other device the size should change accordingly (scale down, scale up) and it should appear the same way (NOT the same size) on other devices. And I have already tried multiplying the density by the dp I want and all that other stuff but it doesn't work.
First off, any size you use should be based off of sp- scaled pixels. Units in sp scale with respect to the user defined default size. This is to people who are hard of seeing can choose a larger text size, and all text will scale relative to this choice. You should not be designing text in either dp or in px, as neither of those will scale with the system font size. When you call getTextSize/setTextSize it should be treated as sp.
As for it looking the exact same on all screens- you're not going to get that, because different OEMs use different default heights and different default fonts, not to mention the user can override either of those. Pixel perfect should not be your goal. Which is good, because it isn't actually possible.
Related
I'm developing Unity Android/iOS application. I need to show text with size relative to screen size and density. Material design guide says I should set size of my regular text to 14sp. How can I provide it?
What does actually fontSize mean in Text component? Is this number dependent on something? Unity Documentation description of fontSize says nothing about that.
Thanks for response.
So... Font Size is essentially "defined" in three places in Unity.
The first is on the font asset itself (the import settings). As far as I've been able to determine lately (post-Unity 4.5), this value is not used (I've tweaked it to insane values and had no affect on what is displayed on-screen; I know in Unity 3.5 this affected the final rendering of the font and I had to scale it up really big to get nice, cleanly rendered text, as it was effectively the "maximum font size available" and font sizes larger than this value (see #2) would get blurry).
The second is on the text component, called Font Size which does what it says. It's like picking a larger font size in any application. This is what the material design guide is referring to. Text component font sizes in Unity are measured in "pixels of height" and are 1:1 with screen pixels (except when the transform--or any of its parent transforms--are scaled, or if the canvas itself is in Worldspace (in which case you kinda have to guess)).
The third place is on the text object's Transform: you can additionally scale the object and get the (apparent) font size to change although scaling it up will make it look blurry and scaling it down will make it look aliased. Either way it looks bad.
All that said, I've found best results to be to double the font size and reduce the transform scale to 0.5 as the text anti-aliasing at the default scale looks blurry to me, so using a bigger font size and scaling the object transform back down sharpens it up. You'll still want to test for yourself if it needs it.
In my libgdx game I need to display a row with 8 to 14 round buttons.
I'm looking for the best way to make sure all buttons are at least the size of a fingertip (between 0.8 and 1 mm) but not much larger, because I want them to fit into the screen without scrolling if it is possible.
I've found several possible solutions, but I really don't know which is the best, or if I'm missing something better:
Use an ExtendViewport, with virtual size 480x360 for example, and find a suitable size for the buttons. This is the easiest to implement, but the result varies depending on the screen density, size and aspect ratio.
Use a StretchViewport, fix only the width of the buttons, and calculate their height at runtime using the device aspect ratio in order to keep them round. This also depends on the screen density and size, but not on the aspect ratio.
Use the device density and physical size to calculate at runtime the size of the buttons. This seems a better approach to get the buttons at the right size, but is much more complex and low-level, so I find it strange that there isn't simpler ways to do this.
So, which do you think is the way to go?
Joanq, use your (slightly modified) suggestion 2 to cover the most types of screens: Use a StretchViewport, fix only width of the buttons (as a percentage of Gdx.graphics.getWidth()), and calculate their height at runtime using the device aspect ratio in order to keep them round.
iOS is easy, because the pixel-ratio is always either 1 or 2, and the target dpi is always pixels/ratio. Android, however uses 1.3, 1.5, and in some cases 2.25 for a pixel-ratio. I'm not aware of any way to set the target dpi to pixels/ratio for android devices, which would be an easy fix, but I imagine a pixel-imperfect solution for any display whose physical pixel count is an odd multiple of it's dips count (those 1.3's, 1.5's etc).
Right now I'm basically writing css for each -webkit-min-device-pixel-ratio, and just multiplying my px values by that ratio manually (with slight adjustments for rounding). Using ems works great for fonts and a few other things, but I have to use pxs for positioning and sizing of elements if I don't want half, empty, or overlapping pixels.
Is there a better way to do this?
If not, what is the best way to deal with background-images. I really don't want to have to re-write every single background-position for every icon for each ratio simply because I am changing the background-size for each device-pixel-ratio.
You can try using the viewport meta tag. It lets you match width to the device's width. It also allows you to set initial scaling, set bounds on scaling, or even disable scaling. And here is a link to a relevant Stack Overflow question and answer on using the meta viewport tag.
Is it possible to set TextView's text size to be the same as TextView height, when TextView height isn't predefined(WRAP_CONTENT or FILL_PARENT)?
solution : Auto Scale TextView Text to Fit within Bounds
i also wanted to do something like this and the closest you can seem to get is to say android:textSize=20dp (or whatever size you think is appropriate) for either your style or each element that is displaying text. since dp is device independent pixels, if it appears to be taking up the whole screen on your device, then it is supposed to appear that way on all other devices too. you might want to check on this as you might have to choose a different dp value for each of the different size/density combinations possible (depending on what kind of devices you are aimed at, also whether you are allowing the use to change the orientation) this has all that info.
I've found the library that do exactly what I want : SizeAdjustingTextView
I'm developing an Android app targeting Android 2.3 (API9).
I want to resize the height of a simple EditText but when I set android:layout_height="10pt" the border of the EditText becomes dislocated.
The problem is shown here
I tried layout_height, height, textsize.. pt, px, sp... but the same problem.
I haven't tried the application on real device yet.
What layout contains the EdiText? You should never use exact pixel sizes when configuring items in Android, since your layout needs to work properly at many different screen sizes and densities. (See supporting multiple screens for details).
If you really want a specific size, us dp for density independent pixels. It should work to set the layout_height="10dp" However, that won't increase the size of the text within the EditText. You need to use textSize for that. You are probably better off setting textSize and setting the layout_height="wrap_content".
See this similar question about the extra line. It looks like setting a background color might fix that.
I have been having the same sort of problem (and it's a real pain). With some experimentation I have found that if you use "dp" and apply this rule of thumb, it should work for EditText fields.
textSize x 3= layout_height
I say rule of thumb because the minimum height before the field "breaks" again is about 29dp. So, for example if you have textSize="10dp", then layout_height="30dp", or textSize="12dp", then layout_height="36dp". You can have smaller textSizes, but it becomes illegible lower than 9dp. I found this works well in relative_layouts with default fonts. Good luck!