I have a layout with 8 buttons 2 image views and 2 text views. On my device (Galaxy S) it fits but on devices with smaller screen the bottom side is not appearing.How could I adapt my layout for all screens?
if you want your app to support all kind of screen sizes,
you need to create different layout xml file for each kind of the screen types.
place each one of the above on resource folder:
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
on run time - android will automatically choose the right resource according to the device's screen type
you can read more about it on -
http://developer.android.com/guide/practices/screens_support.html
use RelativeLayouts or LinearLayouts with layout_weight property. If you can post some xml, i would give you more suggestions.
The problem occurs generally with the ImageViews mostly, as they occupy more space on smaller screen, since smaller screen has lesser no.of pixels per inch. It's a general practice to restrict the sizes in dp.
You can define more layouts with different width and height.Then check the height and width and density of device and use that specific layout for that device.
Use screen dependent pixels dp and sp. Design a different layout for different screen sizes. http://developer.android.com/guide/practices/screens_support.html <-- refer here
For most width and sometimes height problems you can use layout_weight to assign the exact ratio catered for each View.
If there is no other choice and that your layout have to be that way you might want to consider using a ScrollView to wrap your parent layout so that they can be scrollable. http://developer.android.com/reference/android/widget/ScrollView.html <-- refer here.
Good luck.
Related
I am working on updating a legacy project and I need to create additional layout files for a smaller screen size. In the past, when creating multiple versions of a layout for various screen sizes, I have put the layouts for the smallest screen size in the base layout folder and created secondary layout folders with smallest width qualifiers to support larger screen sizes.
For example:
res/layout //base layouts
res/layout-sw720dp //layouts for screen with at least 720dp width
However, I have been unable to find a way to handle the reverse case where I already have the layouts for the larger screen size in the base layout folder and I want a new layout folder for a smaller screen size. I was hoping to find a "maximum width" qualifier but it doesn't seem to exist.
If I had to update every layout for the smaller screen size then I would simply move my larger screen layouts to a folder with a smallest width qualifier and add the smaller screen size layouts to the base layout folder, but I only need to update 10% of the layouts for the smaller screen size.
I have put the layouts for the smallest screen size in the base layout folder and created secondary layout folders with smallest width qualifiers to support larger screen sizes.
That is the correct approach, both for your original scenario and your new one. More specifically, your "secondary layout folders" would only contain layouts where you need to change something based on that larger screen size. Everything else would only be in res/layout/.
I was hoping to find a "maximum width" qualifier but it doesn't seem to exist.
Correct.
If I had to update every layout for the smaller screen size then I would simply move my larger screen layouts to a folder with a smallest width qualifier but I only need to update 5 of the 50 or so layouts for the smaller screen size.
Sorry, but you do not really have a choice. res/layout/ needs to hold one copy of every layout, set up for the smallest screen size that you are going to support. You override for larger screen sizes for the specific layouts that need to change for those larger screen sizes.
There is no means of saying that res/layout/ is in the middle of the screen size range, and you want to override for smaller sizes as well as for larger sizes.
I have created a layout that consists of imageviews and textviews. When I run the App every thing is fine on portrait screen orientation, but when I rotate the device to be in lanscape the imageviews shrinks (smaller in size).
I want to have (regardless of the screen orientation) the same size of views. I do not want the views to look smaller or bigger, I want the same sizes across different screen orientations.
Note: All dimesnions in the layout are in dp for width and height and the text font it is in sp.
Are your ImageView's being sized by attributes that align to edges, and then margin distances? This would distort your image as the parent boundaries change on device orientation.
Also, please provide your xml code, I don't have the reputation to ask as a comment.
Android tries to help you create responsive layouts (layouts that change the position and size of elements depending on how large the device screen is) through the use of things like layout_weights, settinging the width/height to match_parent, etc. Because of this, if you use these attributes and then rotate the phone screen, the size of your images is going to change because the system will think that you want to text/images to change size depending on orientation or device screen size.
Even if you mix layout weights and hard coded dp pixel sizes this will happen - what happens is that android measures all of your hard coded values and wrap content values, and then for any extra room on the screen, it expands the views that have weight, proportional to the weight number you give them. This is why you'll sometimes see people setting layout_height="0dp" and then setting a weight.
If you want an image that is always the same size, you can hard-code a certain number of dp pixels and remove any mention of layout_weight or match_parent. You can hard-code margins too. Depending on the size of the image, this will mean if you view the image on a phone that's too small, part off it will end up off-screen. There are a variety of ways to deal with this depending on what you want to happen. For example, if you want to elements on the screen to stay the same size but rearrange themselves depending on the device's screen size, you can make multiple layouts for different screen sizes and use resource folder qualifiers.
Currently, i develop a android application. In my activity, i would like to place my elements according to their size. i explain : i have 3 LinearLayout in a GridLayout like this :
GridLayout (2,2, horizontal) allow to put the LinearLayouts like this :
I would like to do the same, but no with GridLayout(2,2,horizontal), but just according to the size of the element.
For example, if the screen is large, it's possible to have the 3 LinearLayout on the same line and inversely if the screen is small, juste 1 LinearLayout by line.
I hope I have been clear, Thanks in advance.
This layout would be a "FlowLayout" which doesn't exist in standard.
But there are some open-source implementations of it around :
You should take a look at
https://github.com/ApmeM/android-flowlayout
or
https://github.com/blazsolar/FlowLayout
Try these instead of gridlayout
https://github.com/blazsolar/FlowLayout
https://docs.google.com/file/d/0B_VKZCqEnHblNDdiY2UxODgtYWNhNS00MmU4LWE4NDMtZjQ1OWI5MDMxZTVh/edit?num=50&sort=name&layout=list&pli=1#
Yes, of course you can do.
You need to create different layout directory for different density devices.
For example layout-ldpi, layout-hdpi, etc. and define the required kind of layout inside these.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
And inside the different my_layout.xml file define either one, two or three linearlayouts you need.
I am making an app that is readily available and MOSTLY used on phones, but would be CONVENIENT to use on a tablet if available.
I am familiar with landscape/portrait layouts for the separate XML files, but what about for the different resolutions? This is also an issue for phone-only applications, what is the best way to have scalable layouts.
I use the dip values which are density independent but not as effective as just using percentages that will scale your layout effectively.
I also hardcode the XML. I IMAGINE that I could programmatically generate the layout by checking the screen width and height ON EVERY ACTIVITY, but tell me there is a better way?
You can provive extra layouts by specifying the screen size (small, normal, large and xlarge) in the same way you specify layouts for portrait or landscape.
These are some examples taken from the android documentation related to this subject
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
The book The Busy Coder's Guide to Android Development has an excellent explanation about this subject.
How wrap_content, fill_parent or the dp will guarantee that the view is given an appropriate size on the current device screen in android platform.
Only wrap_content and fill_parent will adapt to the size of the screen.
Sizes declared in dip will be adapted to the resolution of the screen, but not to the size (100dip will be the same physical length (for example 1 inch), so it won't take the same proportion of the screen on a small device and a large one.
Sizes declared in px will not be adapted at all.
So if you want to develop an application for different devices sizes and resolutions, i suggest you create layouts and drawable directories, as described in the framework.
wrap_content takes the actual size of the child....
fill_parent takes the size of the parent....
dp takes the size of what you given...
For the the difference between fill_parent and wrap_content, see the answers for this question
And about dp, see this documentation on how android support a range of screen sizes and resolutions
For the better understanding of layout objects, please have a look on below documentation pages
User Interface
Common Layout Objects