Android - what's wrong with sp? - android

UI designer drawn this design for Android application:
Size is 1080x1920 px. This is same to Nexus 5 display which has xxhdpi density. In this density 1dp = 3px.
I have this letter on my design JPEG image. Its height - 45px.
So I calculated my TextView size 45px/3=15sp
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Large Text"
android:textColor="#5B5C5D"
android:singleLine="true"
android:id="#+id/tvFolder" />
But when I am running my app on Galaxy Nexus - 720x1280px. Density - xhdpi. 1dp = 2px. I am getting this result - my letter 'd' height is 24px which is equal to 12dp.
Of course, I can recalculate (by multiplying to 1.25) and make TextView 19dp. But I want to understand why this is happening and can we solve this. Maybe I am confused and making a mistake. So my question, what is wrong with sp?

Did you check in your device settings that the font scale is set to 1?
If you want to define your font size in device specific pixel, you have to use dp, if you want the user to be able to scale according to his wishes, you use sp.

Related

Android how can I reduce the text size based on screen size (and NOT density)

I've got a simple view with some text on it, and all I want to do is come up with a good way of adjusting the text size and padding for smaller devices, my issue though is that I don't want to consider density, just the screen size itself. The idea is to make the apps look the same between a small and large device (including padding etc.)
I've seen a lot of posts about creating value resource directories with qualifiers but this hasn't worked, as any small device with a high DP gets the padding and text size of a larger device.
For a simple example, I want the text size to be 17sp on a Pixel 3 XL and be 14sp on a Nexus 5, but there just doesn't seem like a way to do this because they have similar pixel densities even though they are vastly different in actual screen size. Am I missing something??
only one way to set dynamical change text size without density use android default font size like this
style="#android:style/TextAppearance.DeviceDefault.Small"
style="#android:style/TextAppearance.DeviceDefault.Medium"
style="#android:style/TextAppearance.DeviceDefault.Large"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="#android:style/TextAppearance.DeviceDefault.SearchResult.Title"
android:text="Hello"/>

Android button size changes at different devices (width is set to dp)

I am designing an app which needs the customized button. I made it by inheriting the Android Button and I set its width to 300dp in the layout xml. However, I found the size of button is changing on different devices. Here are the example:
My UI in the original device: Galaxy S5
My UI in the new device: Nexus 5X
My UI in another device: Galaxy Note4
(In this trial, I set the left button to use sp instead of dp)
It seems the whole layout scale is smaller in my new device even though both of them are 1080 x 1920 pixels.
I also try to use sp but it behaves the same (problem). My guess is the style? Can any one gives me more thoughts. Thanks!
--- UPDATE ---
Hi, let me rephrase my problem.
I am looking for a way to make a button which width is fix to like (relatively) 1/3 screen width at any device. And its text also changes accordingly. I think the ultimate way is to translate everything to pixel and assign those value manually in program. I used to think using dp and sp is an easy alternative to achieve it, but it turns out not :(
To set width of each child programmaticaly,
view.getLayoutParams().width = getScreenWidth() / VIEWS_COUNT_TO_DISPLAY;
And Screensize as below
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics).widthPixels;
Now, If you want to set Text than use SP for that.
If you are using dp then it will change according to device's screen density. In order to keep the button size same in different screen density conditions you need to use pt,in or mm in your button's width.
Please check link : What is the difference between "px", "dp", "dip" and "sp" on Android?
I think here is what you want.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/test"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Normal Charge (Testing)"/>
</LinearLayout>
</LinearLayout>
With the use of weightSum and layout_weight, you can fix the item as 1/3 width of your screen.
Inside the imageview, android:scaleType="fitXY"
It means that the image will be changed to size in order to match the size of imageview.
You can delete that attribute if you do not want that.
For the image, the best way is to prepare different size of the image inside different folders:
for mdpi it should be 100X100
for ldpi it should be 75X75
for hdpi it should be 150X150 (e.g. Xperia U)
for xhdpi it should be 200X200 (e.g. Galaxy S3, Galaxy Note II, Xperia S)
for xxhdpi it should be 300X300 (e.g. Galaxy S4, Galaxy S5, Xperia Z, Xperia Z1, Xperia Z2)
The above size is just an example, you have to find the base case, i can explain more about that if you need.
Hop that can help, thanks!

Adapter layout screen other devices android

guys... i have developed my first app... i have tested this, in Galaxy SIII, it's work fine... but if i run my application in a Galaxy SII the TextView is big...
XML layout:
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tv4"
android:layout_alignBottom="#+id/tv4"
android:layout_alignLeft="#+id/tv2"
android:layout_marginLeft="23dp"
android:text="#string/my"
android:textSize="15dp" />
How do I adjust the resolution automatically with all the screens?
What is the easiest choice?
I'm confised :|
Thank you!!!
Try using sp units instead of dp units for the text, as follows:
android:textSize="15sp"
From the Documentation:
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).
Also, you can find the differences between these units on Difference between px, dp, dip and sp in Android?

Image size should not vary in android devices

I am having an image in imageview of size 5.64mm.
As android having different resolution,image is varying for different devices.
In my case image should not vary at any device.
please help me with some code for drawing the image or setting the imageview Fixed size.
you have to create the same image for different resolutions and put those images in the hdpi,xhdpi etc folders.
Lets assume your image is 100 x 100 in pixel size, so a device with 320 x 480 pixel will show it big but a device with 1280 x 800 will display your picture smaller.
In other words the more pixels a device has in its per square inch space the smaller space your icon will take.
so you have to create multiple versions of your image so that it size remains relatively same.
Do this way. Use dp unit to specify height and width.
<ImageView
android:id="#+id/ImageThumb"
android:layout_width="55dp"
android:layout_height="60dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="10dp"
android:background="#color/icms_white"
android:padding="1dp"
android:scaleType="fitXY" />
use
android:layout_width="5.64mm"
android:layout_width="6.15mm"

Android DIP size wrong

This one is bedeviling me. What am I overlooking? I want a button exactly one inch wide, on any density screen. I declare it in the layout like this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="160dip"
android:text="How wide?"
/>
What I get is a button that is exactly 7/8ths of an inch wide on all screens, not the expected 1 inch. Is the difference maybe the margin or padding around the button? If so, how to set that to zero?
What about this ?
android:layout_width="1in"
dip are based on virtual densities, not actual physical densities. For instance, on a T-Mobile G1, the device's display physical density is around 180dpi, but we treat it as a 160dpi display. Android currently uses 3 "density buckets," 120, 160 and 240. Each device uses whatever is most appropriate. If you want accurate physical measurements, use physical units like in, mm, etc.

Categories

Resources