I want to make game for supporting all Android device(Tablets and phones).I can get screen size through coding.
Do I have make 3 0r 4 layouts small , medium and large?
Which size for small , medium , large and extra large?
Is there any other way to set layout for all devices?
Here is the Android documentation for what you need:
http://developer.android.com/guide/topics/resources/providing-resources.html
You can use the screen-size, screen pixel density, or other things to determine how to separate your layouts.
You can specify different layouts (and you should try to). Different sizes may use the same layout. What layout the device ends up using depends on a set of rules.
So for example if you wanted to use screen size, you would want to make folders under your res folder called layout-small, layout-medium, layout-large, layout-xlarge. This is because layout is the default folder for layout resources, and then you add a -. In this case, small, medium, large, or xlarge.
Edit: Alex's link above might be what you're looking for. My link is more about HOW to do it, but Alex's link is more about how to do it WELL. I wasn't sure what you were asking exactly.
(1) One way to support all the layout sizes is to put images of suitable sizes into drawable-hdpi, drawable-mdpi, drawable-ldpi and drawable-xhdpi. The image for a device of a particular screen size will be automatically picked by Android runtime system. In this approach you will NOT have to make different layout files for each size.
(2) Second way to achieve the same is to dynamically detect the size(density) of the device and then set the layout accordingly, which can be done like this:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
// your layout for small-size devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
// your layout for medium-size devices.
break;
case DisplayMetrics.DENSITY_HIGH:
// your layout for big-size devices.
break;
}
In this approach, you MAY have to make different layout files for each size separately.
Related
I'm working on a Android App. Layout working fine on every device except where device screen size 480x800 & less.
How can create separate layouts by only targeting that screen size or less?
I'm already tried layout-hdpi, layout-small-hdpi & layout-normal-hdpi since phone like Nexus S, Nexus One are in hdpi category. But when I create separate layouts like layout-hdpi those layouts are affecting phone with bigger screen like Pixel, Pixel2, Nexus 5 etc.
Thanks in Advance
Phones (as opposed to tablets) tend to come in about three size groups (as far as Android resources are concerned): those with smallest width of 320dp, those with 360dp, and those with 410dp. The resources framework gives you a way to target any device larger than a certain width, so the correct technique is to put layouts for small screens on the default folder, and layouts for larger screens in one of the qualified folders.
Since it sounds like your layouts currently work well for anything 360dp or larger, you can make two layout directories:
res/
layout/
layout-sw360dp/
Put the special layouts for the small screens inside res/layout/ and put the “normal” layouts in the other directory.
Its not easy but you can also specify layouts based on the smallest width and you you supply the numbers. You can get more detailed and separate layouts that way however in my experience you will still run into that problem from time to time. In my current project using this method I have specific folders for W360 and W400 to deal with smaller devices for one specific screen.
https://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts
What you are looking for, is a layout bucket with the smallest width qualifier.
A layout with a set smallest width will be active, unless the smaller of the both dimensions (width or height) is lower than a certain value you can choose.
So, when a device has a smallest width below some value X it will use the default layout, when the value is X or more it will use the defined smallest width layout.
To create such a layout file in Android Studio, you wanna right-click your layout directory, click on new -> Layout Resource File and select the Smallest Screen Width qualifier from the list of available qualifiers. Now you need to specify your smallest width, which in your case should be slightly above 480dp. Give it the same name as your current layout file. Place your code for devices with a smallest width above 480p here. Now change the code in your other layout file, without the smallest width qualifier, to support smaller screens.
For further reference take a look at the official Android Developer page.
I am doing a lot of work about android layout I still can't create a layout working in every phone. I am not sure about best the way to create a layout so correct me if I am wrong . There are three things to keep in mind :
Screen px (resolution, for example 1080x1920 px)
Screen dimension "inches"
Screen density dp or also called dpi ( dp is a virtual resolution, it's correct?)
To draw a layout working in every phone (my app will works for phones) do I have to create a directory "layout-kindofdensitydpi for every screen density (layout-ldpi,layout-mdpi,layout-hdpi,layout-xhdpi,layout-xxhdpi,layout-xxxhdpi) and draw "manually" or do I have to do something else?
I did a test, I created these 6 directory and drew manually for every resolution. It requires a lot of time, also device with a resolution of 768x1280 my app doesn't respect what I drew, for example spaces aren't respected, the collocation of elements doesn't result correct and frame layout with ImageView inside isn't scaled.
What I have to do? In some Android books isn't mentioned that elements could not resize and usually them explain how to put some text or image without analyse resize in every device.
Thanks in advice
First you should find the appropriate layout type for your UI (RelativeLayout or LinearLayout). Sometimes using a good layout(or nested layouts) can make the UI look good on every screen. I prefer LinearLayout cuz I can simply set layout_weight for components.
Then set different sizes in dimens.xml file for different densities or screen size buckets. Like this:
And you can also use match_parent or wrap_content
Don't forget to set the screen orientation of your activity if it doesn't need to rotate. Having one orientation makes it much easier to design.
If you couldn't make a good layout using tips above you should create multiple layouts to fit every screen size or density (Screen size and density are two different things).
You should find the best way to determine your screens according to.
Screen size bucket (small, normal, large, and xlarge) picks a layout that fits the screen (or the closest), density bucket (ldpi, mdpi, hdpi, xhdpi, xxhdpi, and xxxhdpi) picks a layout according to the density ,or the smallest width (I think it's almost the same thing as density).
480dp is the sw of these two devices
You can make layouts for different screen orientations too.
I don't like creating layouts for different screens for some reason. One of them and the most important is that sometimes same screen sizes have different densities and it makes it hard and time consuming. To create a layout and you should also provide a lot of pictures.
Use
android:layout_width="match_parent"
android:layout_height="match_parent"
to the outer layout to get access of the full screen of the device.
Also, if there are no changes in your UI then you don't need to create different layout folders.
Refer http://developer.android.com/training/multiscreen/screensizes.html
You should have a look at
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/index.html
I would suggest using layout-swXXXdp or layout-large etc instead of layout-KindOfDensitydpi
Create your relative layouts using RelativeLayout and use the weights of LinearLayout. Don't hard code any of the positions in the layout
I am going to develop new application in Android. This application should only work in portrait (even for tablet). Also the UI and layout design should be similar on phones and tablet. We can't change the layout design for tablet as it has huge area to use. We have to stretch all the images to match phones. We can use nine patch. But I am little bit confused of using images in multiple drawables.
As per my analysis (may be wrong.. : ) ) the screens are divided into density and sizes. We can use the scaling ratio of 3:4:6:8. But this ratio is based on the density. But in my case I have to stretch the entire UI to fill the Tablet screen.
So what are the drawables that can be used for a app like this which can support multiple devices. And what are the screen sizes for which we have to design.
And this application needs nearly 100 layouts. So I am planning to maintain single layout and designing the layout using weight for each layout instead of using dimension.
Also if I used multiple APKs to support different screen size what are the drawables used to support
1. Small and Normal
2. Large
3. Xlarge
I just did something very similar. To stretch the app without creating new layouts I used dimensions set in XML
res/values/dimensions.xml
res/values-sw600dp/dimensions.xml -> 7+ inches
res/values-sw720dp/dimensions.xml -> 10+ inches
Dimensions are resources files:
<dimen name="default_padding">11dp</dimen>
You can increase the dimensions by about 30% in the 600 and 720 file.
Then simply used #dimen/default_padding in your layout and it will be scaled
Regarding images, either you make sure you have all your assets in all densities, or you set fixed size to you ImageView's and appropriate scaleType
Firstly, you do NOT want to create multiple APKs to support multiple screen densities. Android provides all of the framework you need to support everything within one build, you just need to create the right resource hierarchy drawables with your desired densities.
So what exactly do you need... based on your question the following:
portrait mode: you can specify this in each Activity declared in your AndroidManifest file using the following:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation" >
...
</activity>
NOTE: Per the Android docs, if you're targeting API >= 13, and you use the android:configChanges attribute you should also use the android:screenSize attribute to help specify size changes.
dimension sizes for various screens: as touched upon, this can also be handled in resources. This is your best way to use one common layout file but configure the layout for use on numerous devices. See http://developer.android.com/guide/topics/resources/more-resources.html#Dimension for how to use dimensions if you're unfamiliar
drawables: it sounds like this is the crux of your question. As you mentioned, using nine-patches will help you reduce your app footprint and fill in extra space (see here and here for more on nine-patches). The sizes you should support and the densities needed for those sizes are discussed in great detail in Android design docs, so much detail I could not even do it justice rehashing it here. I've provided links below to as many places as I could remember that this is discussed.
Good luck!
Here are links to Android design docs that you will find useful (some of which have been mentioned):
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
http://developer.android.com/training/multiscreen/index.html
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/design/style/devices-displays.html
In addition to the pixel density specific folders, you can specify screen-size specific folders
drawable/
drawable-large/
drawable-xlarge/
drawable-hdpi/
drawable-large-hdpi/
drawable-xlarge-hdpi/
drawable-xhdpi/
drawable-large-xhdpi/
drawable-xlarge-xhdpi/
So you could design scale appropriate graphics for the various screen sizes and densities. Please note that a give screen size category (e.g. "large") will only give you a rough idea as to the actual device pixel dimensions of the device, but you'll get good guidelines for min/max dp ranges.
For example, you might have a 100x100 image you want to display on phones (screen size "normal"), you'd create image assets at 100x100, 150x150, 200x200 for drawable, drawable-hdpi, drawable-xhdpi folders respectively. But on 7" tablets, i.e. "large" screen size devices, you might display this same image at 200x200, so your "drawable-large" folder assets would be 200x200, 300x300, 400x400, and on 10" tablets, i.e. "xlarge" screens, you might display the same image at 300x300, 450x450, 600x600, so these go in "drawable-xlarge-*" folders.
All the details are here:
http://developer.android.com/guide/practices/screens_support.html
First you need all the possible screen layouts
drawable
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi // phones like s4
drawable-xlarge
drawable-tvdpi // nexus 7 etc
drawable-xlarge-xhdpi //tablet like nexus 10
layout : for phone
layout-sw600dp
layout-sw720dp
then you need to put all use 9- patch images for buttons etc ... you can also make your custom drawable it would be easy and handy to work on ..Also you can take dpi for each screen by using switch and scale it the layout accordingly.
As, in one of my project I had used this technique for showing respective thing to each resolution device .
DisplayMetrics metrics = getResources().getDisplayMetrics();
int densityDpi = (int) metrics.density ;
switch (densityDpi) {
case (int) 1.5:
break;
case (int) 2: //1.75 will be 2 in INT.
break;
default:
break;
}
also keep all the values you are going to used for margin padding etc values-sw600dp for tvdpi tablet ,value-sw720dp for tablets
Last but not least keep all thing generic as much as you can and put it in drawable ..
I have seen some ppl who used background patterns of different dpi's and put it in respective drawable .. if there is such thing like pattern make your custom drawble and repeat it accordingly
that will save your time .. hope it may help you
In order to stretch all the images to match phones you can specify the image size using the sdp size unit. This size unit is relative to the screen size so it can fulfill your requirement.
I have finished creating an app but after taking a look on other phones I have seen that the content of the app does not fit or has moved on the screen from what I set it out as on Eclipse.
So I was looking around on StackOverflow to find out how to do this and I found this short code.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
// layout for small sized devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
// layout for medium-sized devices.
break;
case DisplayMetrics.DENSITY_HIGH:
// layout for big-sized devices.
break;
}
I am a little confused on where to put this and what to do, I would guess that this code would go into the pages XML files? and in the //layout for xxxxx would go the xml code for that size phone.
But how do I know how to space the app out without having lots of phone to test them on? just keep using the inbuilt emulator to test on the smallest screen size the medium and the large screen size till it all fits?
Create a single layout using WRAP_CONTENT attribute and use different type of images for all resolutions like drawable-mdpi,drawable-hdpi etc. the key is to use different drawables in proportion and let the OS calculate things and draw the layout for you.
Also refer to this answer.
I read info about "Supporting Multiple Screens" and other post here... but Im really confused about how can I develop my application that run on multipple devices.
I was starting develop on a determinate screen (normal size layout), then I run my apk on a S4 galaxy so I see that every object of my apk was diferent size to my xperia to the S4.
what I need to do to make my apk compatibility for all the devices?
I read information that the only source to make that is:
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
For:
res/drawable-mdpi/my_icon.png // bitmap for
medium density
res/drawable-hdpi/my_icon.png // bitmap for high
density
res/drawable-xhdpi/my_icon.png // bitmap for
extra high density
is this true? so I need to make different layouts for different devices?
guys please help me with this issue cz I relly dont understand how is the deal for multiple devices compatibility.
For the layouts:
You can most likely get away with one set of layouts for your application. By default, Android's gui system is done in such a way that you should be able to make good use of the screen of any device, for example use layout_weight and specify things in dp (density-independent pixels). By providing only one layout though, you're most likely going to have it optimized for phone or tablet sized screens. If you create it for phones, then tablets will probably have a lot of empty space, whereas creating another set of layouts would allow you to put extra info on the screen than the phone layouts could fit. It depends how willing you are to customize your app experience for each users device. To create multiple layout, it's recommended to use the Android 3.2 size qualifiers. For tablets, you could use res/layout-sw600dp. See here under "Using new size qualifiers".
For the drawables:
You will most likely not want to have just one set of drawables. You can, and the Android system will scale all your drawables to maintain the same physical size on all screen sizes, but due to scaling, the images will not look very good. That's why you provide an image that will look good for each "category" of screen density, and the Android system will choose the one for that device's screen density, and any scaling it has to do will be minimal.
It is kind of confusing at first, but you most likely don't have to worry about the layouts specifiers just yet. I would make sure to read the supporting multiple screens article and writing your xml layouts such that they make use of the screen size in a relative way.
Have you read this page? Or this one?
You create resources with duplicate names located in folders that are qualified a certain way. At runtime, the system will choose the resources that best match the current configuration of the device. If you want your device to work on multiple screen sizes and multiple screen densities, you will need to create different resources (layouts, drawables, etc) and qualify them appropriately.