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.
Related
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 want to set the text size in my app. I have created different layouts (layout, layout-small, layout-normal, layout-large, layout-xlarge) and for values (values, values-ldpi, values-mdpi, values-nodpi, values-hdpi, values-xhdpi). But the problem is some devices in emulator have different sizes but having same dpi. So I am little confuse how to write different text sizes in dimens depending upon screen sizes not on dpi value. And how its work?
I think depending on device size android pick layout and then depending on dpi its choose values. Is it correct or not?
Edit-
I have one activity in my app to show text. I have scroll on text. And I set the values of text size in values-mdpi. But when I run my app on emulator 3.2"QVGA(ADP2)(320 x 480:mdpi) scroll on text work. But when I run my app on emulator 5.1"WVGA(480 x 800:mdpi) all text on half screen. I want size of text big on large screen although they belong mdpi.
Yes it is correct. Depending on the device screen size and specification, android automatically loads the desired layout and all its elements and fonts.
style="#android:style/TextAppearance.DeviceDefault.Medium"
no need to create all layouts, just see this example it is working for me
Note :textviewtwo.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.textsize));
http://polamreddyn.blogspot.in/2014/01/adjust-text-size-in-all-android-devices.html
I have a normal-hdpi-480x800 layout, which includes buttons and similar graphics. I mostly use RelativeLayout, since it's recommended for better performance, and position my elements from the upper left bound relatively to each other. According to screens_support.html this group might also include 600x1024 resolution devices. So, when I test the application on LG L9 (540x960), which also falls into normal-xhdpi, it looks horrible - it seems there's no difference between using pixels and dp. Here's an example of code:
<Button
android:id="#+id/fb_post_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/share_fb_m_l"
android:layout_marginTop="#dimen/share_fb_m_t"
android:background="#drawable/fb_btn" />
Any help ? What I might be doing wrong ?
Thanks.
Just specify different dimensions in folders values-ldpdi, values-mdpi, values-hdpi, values-xhdpi if you need more control over button sizes according to screen density. Like this:
<resources>
<dimen name="share_fb_m_l">47dp</dimen>
<dimen name="share_fb_m_t">57dp</dimen>
</resources>
Update: You can get more control over this:
Specify multiple different images for different screen resolutions and densities. To do so read supporting multiple screens. If you need even more precision in image scaling and quality you can use the fundamental size of screen attribute which is swdp - you can specify the smallest width of the screen where your image should be used. This is a qualifier name for a resource folder.
Place your dimensions in these folders values-sw540dp, values-sw540dp, values-sw600dp - these should serve as subfolders for more control.
I know this has been answered in other questions, but nothing is working for me. I have a text view where I want the text to appear the same size in a variety of density/screen size combinations. I have tried specifying
android:textSize="30dp"
and
android:textSize="30sp"
and have also used
TextView text = (TextView) findViewById(R.id.text);
text.setTextSize(30 * (getResources().getDisplayMetrics().density));
30 is my font size *****
the text doesn't scale properly. It becomes way too small at lower densities and as screen size increases. Are there additional techniques I can use to scale my font?
create layout-large, layout-small, layout-xlarge etc.. versions of your layout xml file.
You can set the size differently in each so that the font will look right across a variety of screen sizes.
After years of constantly having everything slightly off on different devices, I recently discovered that the inches and cm measurements for dimensions are literally that.
So even on devices where 160dp isn't quite an inch, specifying 1inch will give exactly that, on everything, even if they have a custom scaled ROM.
I now specify anything I want very tight control over with these.
If you just want to have separate font sizes for separate screens, you can create dimens in your value.xml for sw600dp and sw720dp folder too and put different values there. You don't need to create separate xml layout for each, if you just need to change font sizes.