I'm new from android, my question is how can I force the font size like 12sp when showing in different android phone?
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="hello world"
android:textSize="#dimen/btn_text"/>
Create multiple values folders, one for each screen size you want to support. ( values, values-large, values-xlarge and so on).
In every values folder you will include a dimens.xml with the dimensions you wish to use when the screen size is the same as this values folder.
And then your code will work fine.
android:textSize="#dimen/btn_text" will make your button text, a different size if the screen is normal, or large and so on.
Related
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"/>
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
I know the Internet is overwhelmed with questions about DPI px inches and so on.
But after several hours of googling my situation doesnt seem to happen to anyone else!
I have 2 devices custom build with android studio which are both mdpi.
BUT one device is 3.65inch and the other device is an 10.1 inch.
I have created a folder with 2 images 250x125 with the dpi set to 160 dpi
If normally I would declare my 2 images in my XML with dp units instead of pixels...I would suppose on both screens the result should be the same right ?
Well it seems the images keep remaining the same size and don't look # how many inch the device is
So to set things clear:
What do I have to change at my resources or my code so that my layout scales identical for different Inch sizes ?
This is my GOOD layout for my mdpi 10.1 tablet :
This is my BAD layout for my mdpi 3.65 device
How can I make it so that even on the 3.65 inch screen the buttons will scale to the same PROPORTIONS as the 10.1. Not the inches...not the pixels...the proportions....
This is my XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/buttonEnglish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/english"
android:layout_marginBottom="5sp"
android:layout_marginLeft="5sp"
android:layout_marginRight="2sp"
android:layout_marginTop="0sp" />
<Button
android:id="#+id/buttonNederlands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/nederlands"
android:layout_marginBottom="5sp"
android:layout_marginLeft="20sp"
android:layout_marginRight="5sp"
android:layout_marginTop="0sp"
/>
</LinearLayout>
I'm desperate...
Thanx in advance
This might help explain the problem you are facing...
You have an image that is 250x125 - that is 250 pixels wide by 125 pixels in height.
You have specified 160 dpi - which means that 1 inch = 160 pixels.
So, both screens are doing what you ask and displaying the 250 pixels across 1.5625 inches. On the large screen it looks "proportionally" correct. On the 3.65" screen the button takes up more than half the screen - just like you asked it to.
If you want the smaller screen to look like the larger screen, then you have three options:
adjust the size of the image and provide 2 image assets (or more for a wider variety of screens). This is why you can have resource folders for mdpi, hdpi, xhdpi, etc. You adjust the pixels in the image to accommodate the screen size.
You use a "weighted" LinearLayout that adjusts the size of the space provided based on the available screen space. For this type of layout you should not worry about performance.
Do runtime scaling of the image based on screen size - use DisplayMetrics to get the size and density of the screen and adjust your image to fit the screen appropriately.
The last option is the most flexible in many ways, because if you end up with a screen that is either very large or very small, you can make adjustments to do things like move buttons or text to another place on the screen (above or below other content). But for your specific problem, any of them will suffice.
There is no need of Designing two xml layout.
You can use Dimension for margin and padding according to device.
You are giving static value for margin.
Use dimen.xml in value folder each device.
Following code in your layout.xml will work for you.
android:layout_marginLeft="#dimen/margin_button"
Value folder name for dimen.xml:
values-mdpi
values-hdpi
values-xhdpi
values-xxhdpi
values-sw600dp
create dimen.xml in each values folder.
and in dimen.xml you have to define value for margin in all values folder but value of that property is different according to device size like this:
values-mdpi
<dimen name="margin_button">20dp</dimen>
values-hdpi
<dimen name="margin_button">23dp</dimen>
like wise in all values folders.
Thanx everyone for the answers. Due to answer from #Iacs I discovered that I had to made changes to my folder structure.
I have completely overlooked the fact that in the /res folder there can be more directories then just the standard "layout" directory. You can create other directories with these names : layout-large, layout-xlarge, layout-small, and so on...
In these folders you can paste your layout.xml and adjust the values...
This is how things look now in my android studio
note the layout folder structure:
And now ofcourse my 2 devices with both the same DPI but different screen size are showing my buttons the way I want them to be showned!
I am making Game with android studio everything is completed but only one issue i have. Text size's not changed. How to make it for auto fit on any screen like phones and tablets.
Try this Demo Git Project Android-autofittextview.It may help you.In your layout add like this to mention size android:textSize="#dimen/_15sdp".
you can use this library
to auto fit your text depend on TextView width.
also you need to set android:textSize in dimens.xml like this:
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/textsize"/>
for have different size for different devices just define dimens.xml for different screen size (for example: Value-large)
I'm new developer for android. I build new program for android, but when I install it in different device (different screen size) , do not change automatically the size of buttons in my program,
for example: i built the program on 4 inches device(emulator), i install it in Samsung galaxy S , every things are in their place, but when i install the program in Samsung galaxy S III, the buttons are smaller then the screen size.
now, how can i fit the size of buttons by devices screen size????
Some code would help a lot in this situation.
However, Please insure you are using DIP instead of pixels. And if you really need to, you can use a Linear Layout and add weights.
<Button
android:id="#+id/button_name"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
You should use Dimension. For reference go through Dimension
FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The <dimen> element's name will be used as the resource ID.
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
and define the size here
<dimen name="font_size">18sp</dimen>
and use it like
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
But you need to define dimension for each screen size.