In Html we could use em, and it would adjust the size and format of everything to the appropriate size despite Resolution. Does android have a unit of measurement that adjusts views to its appropriate sizes and formats despite the different screen resolutions?
Android uses scalable points or SP
here is a sample
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text - 26sp"
android:textSize="26sp" />
Reference
You normally use dp for View sizes.
And sp for font sizes.
you can normally use dp,sp, for ref
Related
When I see on the XML layout file in the android studio I can see the padding is there but on the device, I can not see it. I am not adding padding programmatically on that button. I only set the background of the button programmatically.
In Android Studio
In Device
Here is my button code
<Button
android:id="#+id/LoadBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:minWidth="0dp"
android:layout_marginTop="16dp"
android:background="#color/colorAccent"
android:foreground="?android:attr/selectableItemBackground"
android:text=" LOAD MAP "
android:textAlignment="viewStart"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold"
android:paddingStart="7dp"
android:paddingEnd="7dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
app:layout_constraintStart_toStartOf="#+id/fileTypeRadioGroup"
app:layout_constraintTop_toBottomOf="#+id/fileTypeRadioGroup" />
Here is the java code that I am doing with that button.
loadBtn = findViewById(R.id.LoadBtn);
loadBtn.setEnabled(false);
loadBtn.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.disabled_button_background));
loadBtn.setEnabled(true);
loadBtn.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.color.colorAccent));
Try setting the text of the button to dp, instead of sp. Sp will scale the text based on the device's preferences, while dp will not. It looks based on the example that they are just differently sized texts.
It could be that inside your android studio you are watching a display of one phone but when you run it you actually run it on another phone with a totally different screen resolution.
So you may see it as you would like to in your preview but this screen preview and your phone screen are different, and because different phones got different screen sizes and pixel densities you are seeing this differently.
When using a fixed size (7dp) value for padding for example for larger screens you will need to use more than 7dp if you want it to look like the preview or less if you are using a smaller screen than what you have in your android studio preview.
A good solution could be to use sdp library:
From the library github page:
An android lib that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size. It can help Android developers with supporting multiple screens.
How to use:
You will first need to implement this:
dependencies {
implementation 'com.intuit.sdp:sdp-android:1.0.6'
}
And now on your layout file simply add the wanted padding:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Text"
android:textSize="30sp"
android:textStyle="bold"
android:padding="#dimen/_18sdp"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</Button>
And it will look like this:
Something extra
If you want to make your texts scale in size as well according to the screen size (as I just showed for dp) you can use
AutosizingTextViews or you can even use
ssp library
I finished the java codes and function of my little project. At the end, i check to support a big amount of android devices according to their size. But it was fail.
While researching, i understand that i should use sp for textsizes and dp for all other parameters. The layout -xml- is existed via sp and dp. But it is not like that i expected.
I create a new project for example.
My xml; (in ConstraintLayout)
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:text="Hello World!"
android:textSize="105sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="144dp"
android:text="Check"
android:textSize="160sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
For 1080 x 1920 xxhdpi; Click here to see layout
For 1440 x 2960 hdpi(samsung galaxy s8 ) Click here to see layout
In galaxy s8, elements are really small and there is problem in view. I guess that i misunderstand a basic concept. Can you clear up my mind please?
You're missing something here: when you set android:layout_marginTop="72dp" the 72dp wil lbe interpreted the same way in both layout files.
A solution is to use dimens instead of values directly in your xml file.
Watch here : https://stackoverflow.com/a/32861248/5778152
Hope it helps.
You have to be careful at screen size and pixel density.
The best way to treat all screen sizes is to use ConstraintLayout, or weightSum in LinearLayout (but it slows UI performance). This will help you keep the same position of the elements on all screens.
Pixel density is harder to treat. For text size, for example, I find it useful to use different dimens files.
Right click values folder and click Values resource file, put the name dimens and then choose Density from the left. There you can select what density you like to treat. In each file you make you can make a text size with the same name, like this:
<dimen name="normal_text_size">15sp</dimen>
and each time you set a text size use this tag. This way depending on the phone density the appropriate text size will be automatically selected.
You can read about ConstraintLayout here
Read about screen size here
And about pixel density here
Why does text of x size not fit within a TextView of that same size?
For example, this TextView:
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:padding="0dp"
android:textSize="60dp"
android:text="12:22" />
Renders like this on my Nexus 5:
I'm not asking for how to scale the text (like many other questions). I am just trying to understand why android does this, and a formula for enough buffer to avoid clipping.
It should help to disable the implicit font padding
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:drawablePadding="0dp"
android:padding="0dp"
android:textSize="60dp"
android:includeFontPadding="false"
android:text="12:22" />
I think this tool may be helpful for you. It allows you to translate various UI units, like SP to DP. Seems like you should be able to figure out the correct conversions for various screen densities.
The unit sp can be changed in settings by the user to be different sizes. If you need the text to fit inside a bounding box you should use dp as the unit.
See Android sp vs dp
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?
I have written a SmartWatch 2 control extension which runs fine, but I am seeing a different font size when the application is installed on different phones. Most of the time the font is displayed correctly, but on some phones, the font on the SmartWatch is unusually large.
Here is the XML of the text widget:
<TextView
android:id="#+id/watch_main_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/watch_title_bar"
android:gravity="center"
android:padding="5dp"
android:text="#string/watch_choose_workout_string"
android:textColor="#color/white"
android:textSize="10sp" />
Is there some sort of global font size setting that overrides the font size specified in the XML resource?
Thanks!
Try defining your textSize in terms of px and not sp. The reason the font size varies from phone to phone is because since the extension is actually running on the phone and not on the watch, it will try to scale the assets according to the phone even though the watch itself is a constant size.
Best way to do it is to use the #dimen constants defined in the SmartExtensionUtils project.
For example:
android:textSize="#dimen/smart_watch_2_text_size_large"