Which is standard scaling of Android TextView sp or dip - android

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

Related

Text size of TextView for multiple android screens (mdpi, hpdi, xhdpi,xxhdpi,xxxhdpi)?

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.

why not just use sp for everything instead of dp?

While defining a view in android, we use dp for margins, sizes, etc but for text sizes we use sp. sp is also same as dp but for texts. Why can't we use sp for everything then?
I know the difference in between the two. I am asking if sp is a superset of dp, why use dp at all? why not use sp to specifiy all sizes in views?
The reason we can't use sp for everything is that when we increase the font size from settings,we only want the text to resize and not the buttons and other views as well. So we use dp for the rest and just sp for the text.
The sp unit of measurement is used for fonts and is pixel
density dependent in the exact same way that dp is. The extra calculation that an
Android device will take into account when deciding how big your font will be,
based on the value of sp you use, is the user's own font size settings. So, if you test
your app on devices and emulators with normal size fonts, then a user who has a
sight impairment (or just likes big fonts) and has the font setting on large, will see
something different to what you saw during testing.

What should I use for margins, sp or dp?

I have seen some people using sp for margins like:
android:layout_marginLeft="40sp"
While many have been using dp, like:
android:layout_marginLeft="40dp"
Could anyone please confirm which is better between these two and which should be used when? Any help would be highly appreciated.
sp for font sizes, dp for everything else. sp stands for Scale-independent Pixels, dp stands for dip=density independent pixels. Detailed explanation
An XML-defined dimension value. A dimension is denoted by a number followed by a unit of measurement. For instance, 25px, 5in, 10dp and 10sp. When you use sp/dp, your Android applications will be compatible with a wide range of screen densities and resolutions.
PX: is an abbreviation for Pixels, which specifies the actual pixels on the screen.
SP: is an abbreviation for Scale independent pixels. It is the same as the dp unit, but it is additionally scaled according to the user’s font size selection.
DP: A virtual pixel unit used to communicate layout dimensions or location in a density-independent manner while creating UI layout. The density-independent pixel corresponds to one physical pixel on a 160 dpi screen, which is the system’s baseline density for a “medium” density screen. At runtime, the system handles any scaling of the dp units that is required based on the actual density of the screen in use in a transparent manner.
The terms DP and DIP refer to Density Independent Pixels, which are based on the physical density of the screen.
SP: Similar to dp, but also scaled by the user’s font size selection. When choosing font sizes, it is recommended that you use this unit so that they are adjusted for both screen density and user choice.

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.

All screen compatibility screen

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.

Categories

Resources