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.
Related
I am making an Android app. There is a rotatable ImageView in the middle of the screen with two TextViews (in bold) above and two TextViews below. It shows different on different screens.
Just make sure in your XML layout you define the TextSize attribute in dp. Since it is density-independent-pixel, it won't be affected by different screen sizes.
From Android Docs:
When specifying dimensions, always use either dp or sp units. 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).
When you specify spacing between two views, use dp.
I have kept my EditText font size as 16sp. However when I change the device font size, the EditText font size also changes. I tried using dp instead of sp which worked but I doubt if it will work well on different screens. How to I keep font size constant using sp as the unit irrespective on device font size and screen density?
You should use dp. The only difference between dp and sp is that dp will ignore the user's text size setting for the device. The result will be that the font size measured in real-world units (points, inches, etc.) will be the same on all devices, regardless of pixel density.
Note that screen size never affects any of this. If you want the text size to be defined in terms of a certain fraction of the screen dimension (e.g., width), there's nothing built into Android's unit system to support that. You'll have to either do the calculation in code (taking into account both screen size in pixels and pixel density) or define the size using alternative resources for different screen sizes.
Hi Iam developing android Titlebar , so I need to clarify which is use to android text either sp or dip
or
Which is standard and what is the difference between sp and dip? Thanks!
SP is always used for textSize
DP is used for pretty much everything else
See "Supporting Different Densities" for more info.
From the docs:
One common pitfall you must avoid when designing your layouts is using absolute pixels to define distances or sizes. Defining layout dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels may correspond to different physical sizes on different devices. Therefore, when specifying dimensions, always use either dp or sp units. 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).
sp is used for text size and dp for everything else. These are mainly to achieve the best support for multiple screens with different sizes and density
I'm designing an android activity which I want it can be compatible with all screens resolutions. Right now, I did it knowing that the screen will have 1024x600pixels, and in the layout_weidth and height, I used absolute pixels... (I started with dp's but it doesn't work in my mobile, but it works in the emulator... very confusing, so I decided to try with pixels and they worked) but if I get another resolution, then it crash...
So I though in creating a xml with percents of the actual resolution of the screen, so it can be compatible with all the screens...
But thoughting, what can I do with the textSize, for example? How can I make it compatible?
I need some advice... thanks
Using Relative layout is a better option
Use dp - density independent pixels for UI components and sp for Text sizes
sp
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 user's preference.
I am looking for a way to convert from UIFont's size to Android TextView's textSize attribute, for example:
[UIFont fontWithName:#"HelveticaNeue" size:14];
The iOS Dev Doc specifies the unit of the UIFont size attribute as point, however on Android's Text View uses px, dp, sp, in, or mm.
On Android what should I write for:
<TextView android:textSize="?" />
Thanks!
If you check the Android Developers site it will give you the appropriate format and sizing units for TextView. You have: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), or mm (millimeters).
A font in defined in points is actually defined in physical inches. That conversion is the most straightforward, and probably the poorest way to do this, but you can convert to inches by simply multiplying the point size by the ratio of points to a physical inch: 1/72
In order to convert the point size to pixels you can make use of a calculation available at this site: https://web.archive.org/web/20120415064052/http://www.emdpi.com/screendpi.html
Some of the information there is Windows specific, but the basic method should work for Android as well, provided that you can identify your screen dpi. This method will give you the appropriate value in pixels (px), which is valid for the TextView font size.
Note, TextView recomends the use of scaled pixels (sp) for use with TextView, so you may need to do some additional work to scale your font to your screen size programmatically.