I know you can set specific layouts for a device like this:
if(android.os.Build.MODEL.equals("Galaxy S II")){
setContentView(R.layout.resultsGS2);
}
else if(android.os.Build.MODEL.equals("Nexus 7")){
setContentView(R.layout.resultsN7);
}
else{
setContentView(R.layout.results);
}
But what I want is to make a layout for all devices that have similar screen size/dimensions. I have been looking at this: http://developer.android.com/guide/topics/resources/providing-resources.html and I tried renaming the layout folder many different things and whenever I try to edit an xml file in the new layout folder I made and I change the device preview it keeps changing back to the default layout. I am using android studio, and basing the need to make a new layout off of the device previews that it shows. Is there is an easier way to do this besides making a layout for each specific device?
This can be done by having different layout folders and calling them layout- + screen size name and then edit the layouts inside these folders to match the screen size you are targeting.
So for example if you wanted to target something such as the galaxy nexus (320dpi) you would create a folder called layout-xhdpi and all the device specific layouts will be placed in here. And then if you were to run this in an emulator or a physical device you would see the layout that you made specifically for that screen size.
Related
I have a doubt. If i have one single xml file let's say res folder--> layout folder--> my.xml. i want to display this my.xml to all multiple screen size without creating layout-mdpi, layout-hdpi etc.., May i know what is the simple and best way to achieve this concept ??
Thanks in advance
Place the file at res\layout\my.xml
Every single device will get that layout by default unless you create an alternative.
If you have a single layout file in the default location (res/layout/my.xml), then it will be used on all the devices that your app can be installed on.
However, that's usually not what you want because it will look very different on a device with a different size than what you developed on. To handle this, you can start by creating another layout file in another directory (res/layout-sw600dp/my.xml). This layout would be used for devices where the smallest width (sw) is 600dp, ie, tablets.
I have a custom view with an onTouch method that launches another activity with its own xml layout. If I run the program on an emulator that has been scaled to watch size (yes only scaled, normal emulator is fine), it creates an instance of the new activity with both the round and rect layout files. Additionally logcat says eglSurfaceAttrib not implemented.
Because this only happens on scaled emulator, is it just a bug with the emulator? I don't have a real android wear watch yet. Or is there a way to package some attribute with the intent that starts the activity to let it know I want one of the layouts?
Edit: It's having this problem on the emulator regardless of size/shape
The best way to target specific screen sizes or aspect ratios is by using qualifiers for alternate resources.
Based on the developer documentation, using the watch qualifier for your resource will probably do the trick. Your directory hierarchy would look something like this:
src/
res/
layout/
(all of your regular layouts)
layout-watch/
(all of your Android Wearable layouts)
consider a requirement of an app which need to be run on different screen sizes. lets say app has text fields ediboxes list view etc... and in each screen size the width/height of each ui component has to vary. how to desing an UI for this kind of requirement. please suggest.
Use Width and Height as fill_Parent or wrap_content..
if you are using images make different sizes images and put it in drawable-hdpi,drawable-ldpi,xhdpi,xxhdpi.The Sizes for those imagaes are in developer.android.com
In the res folder,there exists folder named layout,that the default folder which the system find the layout defination.But people also can make other folder like
layout-480x800
layout-1024x768
and so on ,and put the layout files in it.
When the app run on a device which have the certain size,the app will auto find the layout defination in the same folder,if cant it will use the default defination.
I would like to know if You Could select a layaout xml Differently Depending on the size of the phone screen.
I try to be more clear, I make more layout xml file, and I would like my app handles this statement
setContentView (R.layout.main)
first verifying on that size of the display is performed, and Therefore You Set the most appropriate layout xml
What you could actually do is the following:
Create a new folder for each desired configuration and name that folder with qualifiers.
If you want your application to have a different main.xml layout for different screen sizes you should make the following folders in your project.
/res/layout-small
/res/layout-normal
/res/layout-large
/res/layout-xlarge
In each of those folders have your different main.xml file layout (or whatever resource you want). Android will pick the one it matches with the device is running your application.
You can even add different resources for different configurations for drawables and even for language.
More info: http://developer.android.com/guide/topics/resources/providing-resources.html
Have you looked at this page yet?
You can query the size of the device screen, and then select which xml layout you want to use for that size screen...
I have a regular layout file : Res/layout/test.xml
And another layout for tablets : Res/layout-large-land/test.xml
This line is used in my Activity to declare the layout : setContentView(R.layout.test);
I'm using an Eclipse AVD emulator with the customer resolution of 1024x600 (Which according to the android site is a standard 7-inch tablet size). I've declared in my Manifest <supports-screens> with both normal and large as "true".
My problem is that the layout is always the original one, the different, larger layout never gets shown (even though the conditions would suggest it should?). Essentially the difference is the regular layout is a ListView and the large landscape one is a Table.
I've tried finding examples of how to get this online and in text books, but I can't find any that show what else to do. From what I've read, Android should make a conscious decision to pick the layout-large-land file over the regular layout one?
Thanks in advance for your help.
EDIT: I've been playing around with the layout, I've tried layout-land (which works fine), and changed the resolution of the emulator to be 854x480, still with no luck.
A simple solution might work for you:::
change res/layout-large-land to res/layout-xhdpi.