I have a (RelativeLayout) layout xml that was designed to work for 10 inch tablets. I now want the very same layout to be displayed when the app runs on a phone. Instead of it being only displayed partly, I want it to be scaled, so that the structure is the same, just everything a bit smaller.
I have already:
Searched the web - without a result
Read about LayoutInflaters - but have no idea where I could set a scale there
Tried to "override" the defaultWindow's density using Reflection - without a result
Please note that I am aware that this is bad practice, but it's just a personal project and I am curious to know if this is possible.
Any help is highly appreciated!
EDIT: What I want is to really scale the screen as a whole.
I am not the author of the original layout, as it's an open source project for tablets that I want to use on my phone.
Here is the source: https://github.com/amigold/Gui2Go
The layout I am talking about is main.xml.
My reputation is too low to ask you to include a sample layout, and my proposal does not use the exact same layout. However, You can create layouts with the same id tags and filename.xml having the 10 inch tablet version in the folder res/layout-sw600dp and the phone version in res/layout. The layout in /layout-sw600dp will be automatically used for larger screens & the layout in /layout will be used for smaller screens. You will not have to change your setContentView() call at all.
see: http://developer.android.com/guide/practices/screens_support.html
Related
I am a little bit desperate because I'm trying to know how can I know the exact list of different dimens.xml(hYYYdp) I have to create in order to support all the screen sizes. I think I have already seen all the related questions to mine but none of them has helped me to solve this doubt. This is the most related question that hasn't solved my doubt: How to define dimens.xml for every different screen size in android? .
To give you some context. The reason of having different values according to the screen size is because I have a ConstraintLayout with some items that have Top and Bottom margin and in some devices that margin is not looking good. In this post I made, someone suggested me to use different margin values according to the screen size: How to solve TextView being cropped in small devices because of layout margin?
I have thought that as the margin is dependent of the height of the device (as I am using only Top and Bottom margin) I would have to create a list of different dimens.xml files according to the height of the device (hYYYdp).
The real concern I have is that I don't know how many dimens.xml files I have to create to support all the screen sizes. What I have been doing is creating some specific dimens.xml files that are working for specific devices, but I think there has to be a general list of all the dimens.xml files that have to be created to support all the screen sizes.
For example, what I have been doing is to look the test devices of Android Studio one by one and create specific dimens.xml files for them. This is the process I have followed to create a specific dimens file:
Example:
1- First I have seen the list of the test devices of Android Studio, for example let's take this one:
2- After this, I have calculated the dp of the height of this specific test device:
240x400px (ldpi) device. So the height's dp is = 400/(120(ldpi)/160)== 533.33dp
3- Finally, I have added the dimens.xml(hYYYdp) correspondent to this specific device with the specific margin value that is looking good:
I have repeated this process for most of the test devices of Android Studio but as a result, I have a list of specific devices but I think there will remain many devices without being supported as not all the Android devices are in the Android Studio test devices list. I hope you have understand what I am trying to explain.
I have a strange bug on Samsung Galaxy Tab E (Nougat 7.0 API 24): I have 2 layout directory (layout and layout-sw600dp) when I change the "Screen Zoom" on the "Settings" to "Huge" the device for some reason is taking the XML from the "layout" directory and not from the "layout-sw600dp", for all the others density it works perfectly except the this one.
Is there a solution for this bug?
It's not only in Galaxy devices but you can have this in any device exceeding the sw system.
So instead of making specific folders like layout-sw600 etc, I recommend you to make specific layouts using ConstraintLayout.
ConstraintLayout not only makes your layout flexible but also adjusts it's size based on the increase or decrease in Density Pixels of the devices and also saves your. Development Time. I faced a similar issue long time back and decided to use ConstraintLayout for the same.
A good codelab from Google on ConstraintLayout can be found here:
https://codelabs.developers.google.com/codelabs/constraint-layout/index.html?index=..%2F..%2Fio2018#0
Hope it helps. Good luck.
The system will use the one which is closest to (without exceeding) the device's smallestWidth, but when Screen Zoom set to Huge, the smallest width of device exceeds 600dp.
And your layout files only have two folders (layout and layout-sw600dp), so finally the system choose the layout folder.
I am developing an Android Application that supports all kind of Android devices like mobiles and tablets. But it's time consuming to create multiple folders (small, normal, large and xlarge android xml layouts) to support all the Android devices. Is there any way to build an android app that runs on all android devices without creating these folders:small, normal, large, xlarge etc?
You only need to create unique layouts (ie. *layout_mdpi* ) if you want something unique for that particular screen size.
If you want to use the same layout on all different screen sizes, you will only need to create a single layout (in the layout folder).
Only if you want to customize a particular layout would you need a new FOLDER in layouts (named: layout_mdpi) in that folder you would have multiple copies of customized layout with same name (ex. my_layout.xml)
To clearly answer your question - you will only need the layout folder and no other ones in your casel
Ex.
res\layout\my_layout.xml // this folder is all you need if this layout will work on all screen sizes
res\layout_mdpi\my_layout.xml // you ONLY need this if you are presenting something unique on this screen size.
In Android we need to maintain different folders for the layouts with different resolution reason behind it is the use or the resolution of the Android Device on which the application gonna execute.
small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra large size screens.
Android OS select the specific layout it self by checking the compatible device and its resolution.
So, better to create folders to support in multiple screens
For More Info refer this
Is there any way to build an android app that runs on all android devices without creating these folders:small, normal, large, xlarge etc
Consider this, you have a button (with match_parent) that stretches full width of a screen in portrait mode of a 4 inch phone, that's fine it looks alright, but then that same layout on a 10 inch tablet in portrait is now 3-4inch wide, that's not great looking.
"So what" you say, make it wrap content, okay then so the button now only fills up part of the width on phone, still looks okay but then on a tablet you have huge amount of space now either side of the button, maybe that's looks okay, maybe not.
Maybe same button on a smaller screen takes up too much space?
Now apply the above to every single layout element in your app.
Do you think it'll look good, using the same layout, do you think your users will be okay with an app that was so little care to its UI and UX?
SO, in conclusion, yeah it's possible to only use one eg normal, for all devices but it'll probably look terrible on most of them.
I am trying to make a application in Android. I want that it should be able to run on multiple phones of different screen sizes, so i studied support multiple screen on developers and according to that i have to create 3 different xml files for supporting three different screen sizes and also 3 different types of images for each type of xml file. But on a blog i get the idea of doing this by using current screen size method. So i am confused what i should do. means which is optimized and performance increasing way. And which one will be more perfect for supporting all types of screen(except extra large screens)
Defining height, width and other parameters in the XML file is the better option rather than on run time.
Because XML files works as metadata (data carrier) to the activity and avoids alot of confusion when onCreate mothode in called.
Plus, create different folders for image quality (hdpi,xhdpi,ndpi,ldpi)
7 inch device use mhpi
10 inch devices use hdpi and xhdpi
While NEXUS tabs use hdpi and xhdpi irrespective of their size.
Mobiles use ldpi and ndpi.
Beauty lies here is that android device automatically pick-p the suitable content when found, i.e layout and image.
If not found it would first search other Layout folders,e.g a layout not found in x-large folder then it will search in large,then medium, small, which one of them suits the best ,(if a layout is not found in its respective folder).
Nexus will create alot of trouble for you.
To check how your layout would look on different devices, try using the options, which tells you how it would look on that device with those height width, present in the Graphical (view of a ) layout.
You can use three different layouts for different screen sizes ,and android will pick the suitable layout , but Using three different layouts for each type of screen format will not be a good idea , because it will cause problem in handling all layout , if screens are less then its fine but if number of screen increases it will get difficult . Like if you forget to add change in one of the screen size it shall crash with any exception .
What you can do is keep images of different size in different folders and practice layout to make standard in one layout by using layout weights , and margins in in dp .
See my this answer
Table Layout spacing issues
and check this layout will look similar for all screen sizes.
If your design is same for all screens sizes you can use dp and have only one xml for all screens.
But you should support icons for all screens.
I think it's less confusing David Ohanyan way, but forgot to say something...
Whenever you can, use styles in your xx_layout, images, etc, so you'll have 1 layout.xml and 3 styles files inside folders: values, values-small, layout-large.
At least for me, it's less confusing than opening 30 different layout files.
I am currently developing an Android Application for a company who are set on having this type of design and targeting Hires devices however I have found that this is very difficult to do and on some hires devices buttons start to stretch.
Screen design here: https://skitch.com/aaronwardle/r7kwa/screen
A few people have recommended making the buttons into Nine Patch PNG files which help with the various screen layouts.
Should I try and get a new app design for this project, which fits all screens using standard controls, or do you think this is achievable?
Looking at stats the most common devices are small screen ones, so creating this application may have a limited audience.
If any one has any pointers on what I could do, i.e. could I make this for hi res screens and within the application have a different layout for smaller screens?
Thanks Aaron!
Your question is too generic to give a simple and quick answer. Furthermore, the layout you want to make is complex and very non-native-looking which complicates matters. Please make sure you read and understand the official docs on the subject from HERE. There is no replacement for that...
The main points that come to mind from that document are:
the <supports-screens> manifest item
Resource directory qualifiers for screen size and density. Appending stuff such as -small or -portrait allows you to create resources that are loaded only on a particular screen aspect, size or DPI. For example, you can create bigger images for bigger screens, or change the layout XMLs for portrait or landscape
Use 9-patch images to scale up rectangles (buttons).
Best practices (as recommended by Google):
Use wrap_content, fill_parent, or the dp unit, instead of absolute pixels
Avoid AbsoluteLayout
Do not use hard-coded pixel values in your code
Use density and/or size-specific resources
I know my answer is a bit generic, but then again, so is your question...
You have to use 9 patch png and also will have to make some changes in the manifest file, to make it screen resolution free. These are very less changes in the manifest file so would not be a trouble.
You can use 9 patch images as well as to design application for different resolutions, you can create different layouts for landscape and portrait.