I am new to android programming. I have a background in my layout file and some image buttons on top. When the orientation of the phone is changed, I dont want the background to change orientation, but I want the image buttons to rotate appropriately. What is the best way to get this functionality?
Also I want to know the resolution of the images for the background for hdpi, mdpi and ldpi. Does it matter if I keep the same pixel size for each of them?
You can read about rotation here: http://developer.android.com/resources/articles/faster-screen-orientation-change.html
There is variety of different resolution for hdpi, mdpi and ldpi. http://developer.android.com/resources/dashboard/screens.html - here you can see what sizes and densities are the most popular. However, you can create layout specially for resolution you need (i.e. several layout for hdpi devices with different resolutions). The most popular resolutions are 320x480, 800x480, 854x480 (often I use the same layout as for 800x480).
You can read about supporting multiple screens here: http://developer.android.com/guide/practices/screens_support.html
No it doesn't matter I think, but can be there some resolution changes, like pixeled parts. I think that the best way to do it, if you make a land suffixed map for your layouts and drawables like: layout-land
Hope it helps!
Related
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 kind of confused about managing graphic resources in Android.
Tried to read this doc but It only confuses me more. Can anyone give me some example of how should I handle the following case?
Lets assume that I have an image in my layout that will be scaled to fill screen width. What image sizes (in pixels) should I produce and what configuration qualifiers (drawable-{qualifier}) should I apply to those resources to cover all major dpi and screen sizes (both for tablet and handset)?
Thanks.
If you want to have an image, that is supposed to fill the screen it is best to use 9-patch images. This way your image can automatically scale to fit the device. Because even if an image has the correct density, the actual screen size can vary. For example a smartphone and a tablet can both be hdpi, but have completely different screen sizes (and actual pixel count).
So the easiest way to target most devices, when it comes to images that are supposed to fill up the full width of the screen is to have a 9-patch image and create ldpi, mdpi, hdpi and xhdpi versions of it. This way the image will automatically be choosen depending on the density and then stretched to fit the device.
The android sdk also provides a tool that helps with creating 9-patch images http://developer.android.com/tools/help/draw9patch.html
Dear All I’m going to work in android application I should design the interface. I read articles on http://developer.android.com/design/index.html but I still have some missing point.
Do I need to design back ground for each device size ?
Check for this Link for supporting multiple screen. This link will show you how to create the different layout for different screen size devices.
But even if you want to make a Unique layout for supporting multiple screen in orientation or portrait mode then,
For image, use 9 patch images.
Here is the link http://developer.android.com/tools/help/draw9patch.html
For width and height of any widgets, use wrap_content or match_parent or you can use android:layout_weight and android:weightSum
Avoid using the pixels i.e. px
Use dp
By this you can create the unique layout working for multiple screens, but that screen should be in orientation or portrait mode.
If you want to support for for both,then you have to createlayout-landfor landscape mode in the res folder.
Actually, you should. There are 4 main screen sizes in android those get layout xmls from res/layout-xlarge, res/layout-large, res/layout-normal and res/layout-small. At the same time, you should use different image resolutions for different screens those get images from drawable-hdpi, drawable-xhdpi, drawable-mdpi and drawable-ldpi.
It depends on the background type. If your background is an image with high resolution you should provide different images for different screen sizes if the whole background is an image.
You can also:
Make background from tiles (like icloud.com)
Specify a ninepatch image if your image is something like a frame where corners should remain the same and the body should be scaled (not very good if you have some picture)
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 sw<N>dp - you can specify the smallest width of the screen where your image should be used. This is a qualifier name for a resource folder.
Some values you might use here for common screen sizes:
320, for devices with screen configurations such as:
240x320 ldpi (QVGA handset)
320x480 mdpi (handset)
480x800 hdpi (high density handset)
480, for screens such as 480x800 mdpi (tablet/handset).
600, for screens such as 600x1024 mdpi (7" tablet).
720, for screens such as 720x1280 mdpi (10" tablet).
You should also have a look at other qualifiers that make Android choosing image at runtime (screen density, Available width, Available height, Screen size or Screen aspect). By combining these qualifiers and testing carefully you will be sure that the user has the best experience on each device.
I have designed attached screens for my app. The app targets android 1.6 and above. Currently the app is running on majority of hdpi and mdpi devices and few devices having some odd resolution like 1024x600 with 169 dpi, 1024x600 with 179 dpi, 1024x600 with 240 dpi. I have provided alternative layouts to support multiple screens in below mentioned folders.
layout: Default layout resources.
layout-hdpi: For hdpi screens. Few layouts in my projects need to be specifically optimized for this.
layout-mdpi: For mdpi screens. Few layouts in my projects need to be specifically optimized for this.
layout-large: Default layout resources for large screens.
layout-large-hdpi: Layout resources for hdpi screens having resolution 1024x600
layout-sw400dp-v11: This is mainly for 7" tablet. Only devices running Honeycomb or above can recognize this.
Now I also need to add support for qHD screens(540x960 having 256 dpi or similar). Below is the example screen I need to design and add support for qHD screens. This screenshot is taken on HTC Incredible S having resolution of 480x800 with 240 dpi.
I have designed layout for qHD screens so that it will look same on those screens as well.
Now, I have below questions:
1. Am I having right directory structure for providing alternative resources considering I need to support hdpi, mdpi and above mentioned odd screens? If not, how should I provide alternative resources as for those screens? I really need to design separate file so that the UI looks the same across multiple screen resolutions and densities.
2. qHD Screen are having 256 dpi. That means those screens are hdpi screens. I already have resource for hdpi screens which looks as above on most of the devices. but when I view it on qHD screens, it looks like below screenshot. So, how do I provide alternative resource for qHD screen such that it won't affect my current layout resources which are showing good on most of the devices?
#Espiandev
A qHD, hdpi device has a display-independent pixel (DIP) screen size of 360x640, whereas the Incredible S's is 320x533 hence why some of the images are higher up the screen relative to others. So essentially, the qHD device can show more stuff that the Incredible S, per screen. See here for an explanation of DIPs and some more tips
-- Thank you for this insight. I am aware of this. Also I have read post by Dianne Hackborn,; "New Tools For Managing Screen Sizes" - http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html
From what it looks like, you're currently using some margin or padding above the goal/gain/to go parts, I'd instead suggest changing their gravity to bottom (i.e. align them to the bottom of your screen) and then use a margin or padding below the item.
-- No I am not using padding or margin for these parts. Its a relative layout where goal/gain/togo buttons are below that meter tape button. As you have suggested setting gravity to bottom and have bottom padding; yes, I had tried that as well but then distance between that meter tape button and these circles increase and I have to modify sizes and margins between those glasses, button below that and meter tape button which created different layout file. When you view this screen on small screen devices, views will overlap as small screen devices can show less views.
But I'd work on making your app adaptable to screens rather than trying to divide screen sizes up like this, as you shouldn't really need separate layout resources for mdpi, hdpi etc.
-- I agree that Normally I shouldn't need separate layout for mdpi and hdpi screen. But the requirement here is that screen should look exactly the same on all screens. Like, all views within that yellow circle should not go outside of that circle. That button at the center should be always at the center. Also, the relative sizes of those 3 little circles at the top and below meter tape button should be same. This left me no choice than creating different layout for mdpi and hdpi screen.
I have created different layout file for qHD screen. Now my question is where do I put that so that qHD devices will use that file and other devices will continue to work as those are working now.
A qHD, hdpi device has a display-independent pixel (DIP) screen size of 360x640, whereas the Incredible S's is 320x533 hence why some of the images are higher up the screen relative to others. So essentially, the qHD device can show more stuff that the Incredible S, per screen. See here for an explanation of DIPs and some more tips.
There's no real way to make the UI look exactly the same on the Incredible S and qHD devices. Unlike iOS devices, screen sizes are very varied. From what it looks like, you're currently using some margin or padding above the goal/gain/to go parts, I'd instead suggest changing their gravity to bottom (i.e. align them to the bottom of your screen) and then use a margin or padding below the item. That way it will hopefully stay aligned with your lunch/dinner boxes. If you post your XML file, that will help diagnose the problems.
Before Android 3.2, I don't believe there is a way to distinguish the two cases above, but after 3.2, you can use the swXXXdp (shortest width) attribute. But I'd work on making your app adaptable to screens rather than trying to divide screen sizes up like this, as you shouldn't really need separate layout resources for mdpi, hdpi etc.
I have a game which displays a full screen image in the background. At the moment I have one image size (1280x800). This works well on large resolutions but on smaller screens the shrinking somewhat degrades the image. You can see jagged edges and it is noticeably worse than what you could achieve using photoshop software.
I have different image sizes, but I am unsure how to utilize them. I know there are different dpi folders, but you can have resolutions of 480x320 and 1280x768 with the same dpi so I don't think these can be of use here.
I believe you can have different layout files for different screen sizes, but the image is not drawn using xml (and in fact would not be possible for my game).
I can only think that I must create a different file name for each size. Then when choosing which image to use I could take the screen dimensions and select the correct one? I am struggling to see how I can make an image look good on both 240x320 and 1280x800 resolutions.
All of the resource qualifiers in the framework can be applied to drawables, not just the dpi designators. In other words, you could create folders like this to segment your images as well:
drawable-ldpi
drawable-xlarge
drawable-normal
drawable-sw480dp
drawable-sw720dp
Even examples like these work...
drawable-v10
drawable-land
And so on...
You can create as many or as few different qualified directories for your image assets as you think necessary to preserve the quality. The Supporting Multiple Screens article in the SDK docs helps describe most of the qualifiers that best fit scaling image assets.
HTH!