I'm trying to support mdpi, hdpi and xhdpi on my current App. The problem is that I'm fetching images from the web (profile pics). I'm using an imageview with height = 200dp the thing is that even tho I have read tons of tutorials and documentation about dp and dpi, I still don't get them.
So my problem is that on an hdpi phone (atrix 2) the image takes about 1/3 of the screen, which is perfect for me. but on mdpi (galaxy ace) it takes almost 2/3.
What's the best way to set a height for an imageview (from the web, not resources) to support mdpi and hdpi.
thanks
<ImageView
android:id="#+id/expositor_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/expositor_info_header"
android:layout_centerHorizontal="true"
android:layout_marginTop="-27dp"
android:contentDescription="#string/misc_placeholder"
android:scaleType="centerCrop" />
basically my problem is that the 200dp takes different % of screen on mdpi and hdpi
For starters, you are going to want to set a scaleType (try "center_inside") in your xml definition of the ImageView.
If you provide some code (in particular your layout xml file) I might be able to be of more assistance.
Update
Now, this may not fully be your problem, but it is a piece of it. DP (or DPI, they are the same thing) adjust for the screen density (pixels per inch), not the screen size. That means that an image of 200dp x 200dp will be approximately the same size in inches on both screens, not the same percentage of the screen.
Specs:
Atrix 960 × 540 (https://en.wikipedia.org/wiki/Motorola_Atrix_2#Features)
Galaxy Ace 480 x 320 (https://en.wikipedia.org/wiki/Samsung_Galaxy_Ace)
Since the Atrix is hdpi, and hdpi is a 1.5 multiplier, 200dp will take up 300px in real pixels, or approximately 1/3 (300/960) of the screen as you point out.
On the mdpi Ace, the 200dp translates evenly to 200px, which should be closer to 1/2 the screen. However, with the addition of the actionbar taking up screen real estate, it may seem like more.
The best way to deal with very small screens like the ace... well, my normal solution is to not support them very well, at least not on my first pass, as they are pretty rare. But if you want to, the best way is to provide alternate layouts.
For Example:
Create a folder named layout-large (or similar, see: http://developer.android.com/guide/topics/resources/providing-resources.html#Compatibility).
Make a copy of your existing xml file in that folder. (name must be exactly the same)
Modify the file in layout/ so that it works better on smaller screens.
Essentially, if the device has a "large" screen or above (where large is basically the standard these days), it will use the layout in the layout-large/ folder.
Otherwise, it will use the default layout in the layout/ folder.
If you think this explanation is not what is happening, please provide screen shots to verify that the layout is in fact not behaving like it should.
Related
I understand there is plenty of documentation about designing for multiple screen support in Android. and I have read the Android guide here as well as a number of similar questions such as this
However I'm still a little confused as to how I should implement it for my application. I plan to target the following device configurations
Am I correct in thinking I will need to structure the project layouts as follows:
Medium density Normal screens HVGA 320x480 (160dpi):
res/layout-mdpi (320 x 480 )
res/layout-land-mdpi (480 x 320 )
High density Normal screens WVGA800 480x800 (x854) (240 dpi)
res/layout-hdpi (480 x 800)
res/layout-land-hdpi (800 x 480)
But what about the Medium density, large screen devices?
I plan to use sets of both high and medium density drawables too. My primary concern at this early stage is using suitable background images for each layout. For example, to support both the 480x800 and 480x854 sizes, I plan to simply use an ImageView as the background such as:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bg"
android:scaleType="center"/>
The 'bg' drawable will always be 480x854 and by using:
android:scaleType="center"
I'm hoping this will take care of those two screen sizes. Whereby the image keeps its original appearance but is centred on the 480x800 screens. I will lose some pixels off the image but as long as the image isn't scaled then that suits my needs.
I plan to have a set of 320x480 assets for the normal screens.
I'm just hoping I'm following the correct procedure here so I appreciate any info/tips from you guys. Thanks in advance
In my experience you don't really need to customize your layout for the small/medium/large/etc screens, as long as you have your drawables for the different densities. Like the documentation says, Android will attempt to render your layout properly on different screen sizes.
By default, Android resizes your application layout to fit the current device screen. In most cases, this works fine. In other cases, your UI might not look as good and might need adjustments for different screen sizes.
The 'other cases' applies only if you really want to change your layout on larger screens.
Using android:scaleType="center" works for me, but it will, like you said, leave empty space around your layout on larger screens if it should fit on smaller screens as well. If you have a fully customized view with 'widgets' that should be placed exactly right, and you don't want to be programmatically determining the scaling and applying the same scaling to your widgets, this is definitely the way to go.
I understand there is plenty of documentation about designing for multiple screen support in Android. and I have read the Android guide here as well as a number of similar questions such as this
However I'm still a little confused as to how I should implement it for my application. I plan to target the following device configurations
Am I correct in thinking I will need to structure the project layouts as follows:
Medium density Normal screens HVGA 320x480 (160dpi):
res/layout-mdpi (320 x 480 )
res/layout-land-mdpi (480 x 320 )
High density Normal screens WVGA800 480x800 (x854) (240 dpi)
res/layout-hdpi (480 x 800)
res/layout-land-hdpi (800 x 480)
But what about the Medium density, large screen devices?
I plan to use sets of both high and medium density drawables too. My primary concern at this early stage is using suitable background images for each layout. For example, to support both the 480x800 and 480x854 sizes, I plan to simply use an ImageView as the background such as:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bg"
android:scaleType="center"/>
The 'bg' drawable will always be 480x854 and by using:
android:scaleType="center"
I'm hoping this will take care of those two screen sizes. Whereby the image keeps its original appearance but is centred on the 480x800 screens. I will lose some pixels off the image but as long as the image isn't scaled then that suits my needs.
I plan to have a set of 320x480 assets for the normal screens.
I'm just hoping I'm following the correct procedure here so I appreciate any info/tips from you guys. Thanks in advance
In my experience you don't really need to customize your layout for the small/medium/large/etc screens, as long as you have your drawables for the different densities. Like the documentation says, Android will attempt to render your layout properly on different screen sizes.
By default, Android resizes your application layout to fit the current device screen. In most cases, this works fine. In other cases, your UI might not look as good and might need adjustments for different screen sizes.
The 'other cases' applies only if you really want to change your layout on larger screens.
Using android:scaleType="center" works for me, but it will, like you said, leave empty space around your layout on larger screens if it should fit on smaller screens as well. If you have a fully customized view with 'widgets' that should be placed exactly right, and you don't want to be programmatically determining the scaling and applying the same scaling to your widgets, this is definitely the way to go.
I am having trouble managing my application layouts in three different resolutions; 720x1280, 1080x1920 and 1440x2560.
In drawable-xhdpi folder is the corresponding images to 720x1280 resolution.
The folder drawable-xxhdpi is the corresponding images to 1080x1920 resolution and in drawable-xxxhdpi to 1440x2560.
I began to adjust the screens in layout folders. The layout-sw360dp was setting screens for 720x1280 and the layout-sw480dp the 1080x1920.
When testing in the emulator 720x1280 all settings worked perfectly.
But to test the emulator 1080x1920, oddly taking this information in layout-sw360dp folder and not the layout-sw480dp.
In the case of adjusting each folder layout-sw360dp and layout-sw480dp, I'm using margin with values in 'dp' and emulator higher values (layout-sw480dp) are being dropped are being used and the values of the layout-sw360dp.
How can I manage three screen sizes correctly?
Well designed Android applications cater for varying screen sizes, the screen size being a function of both resolution and density. Using several layout-sw###dp folders allows you to vary the layout according to the width of the display, e.g. showing fewer elements and controls on a small screen and perhaps more detail on a large one.
The 'sw' in the layout folder name is the 'shortest width' a display must have in device independent pixels (dip). One dip = 1 real pixel on a 160 density screen. So on a 320 density screen, 2 real pixels make up one dip.
Your nexus 5 has 480/160 = 3 real pixels per dip. So with a width resolution of 1080, that is 360dip wide.
Your nexus 4 has 320/160 = 2 real pixels per dip. So with a width resolution of 768, that is 384dip wide.
Neither device is more than 480 dip wide so both use the sw360dp folder.
Both devices are physically very similar in size. The Nexus 5 (5.4inch screen) has more pixels than the Nexus 4 (4.7inch screen) but the pixels are physically smaller. So it is correct that the same layout is used for both. The UI should look the same on both devices, assuming you correctly specify the size and layout of your various UI elements in dip.
As a further example, I have an old tablet (10inch screen) with a resolution of 800x1280 and a low density of 149, hence is 859dip wide. You can comfortably display far more info on a screen that size than on a Nexus 4/5, hence you might consider creating a layout-sw720dp for that.
So you appear to be doing exactly the right thing already by designing different layouts for different screen sizes. Just remember that resolution is not the same thing as screen size. Screen size is a combination of resolution and density.
As for your drawables, you are also already doing the right thing by using drawable-xhdpi, drawable-xxhdpi etc with appropriate resolution images in each one. So for example a small device with an extremely high density would likely use the 1440x2560 images and the sw360dp layout. My low res tablet would use the 720x1280 images, unless you'd put something in drawable-mdpi which is where it would look first.
So firstly you'd create appropriate resolution images in the drawable folders so that they would look as good as possible on different resolution screens. Then create appropriate swxxxdp for your layouts so they take up the appropriate space depending on the physical screen size, i.e. make good use of available screen space on large devices and don't clutter up small ones. It's likely you would want to go further and create -land and -port versions of each as well.
It is worth noting that even if you only have one layout folder and one drawable folder, your application will still work on all devices. Android simply looks for the best choice and if there is only one, it'll use that. Adding in the various folders simply allows you to make your app look as good as possible on a range of devices.
Everything I have discussed here and more is explained in detail at http://developer.android.com/guide/practices/screens_support.html.
I have a 800 x 480 (landscape) design made in photoshop and now I'm trying to implement the same design on Android.
How am I supposed to calculate width and height for a LinearLayout? For example, a header has 800px width and 60px height. How many DPI they are?
The min and the target SDK are 14. Am I need to worry for devices that are using a smaller display? (Smaller than 480x800) ? (I don't know if older devices can run Android 4+)
I have tested on my AVD (Nexus 7) and this is how it's look (accordingly to my photoshop design):
But on a tablet:
Am I need to create different layouts for different devices?
First thing that you need to understand - for which density your design is. Most common situation is design in mdpi, which means that 1dp on device (with mdpi screen) will be equivalent to 1px of design layout. On devices with higher density it will be increased accordingly (4:6:8 rule).
Second thing - providing values in dp won't magically scale up your layout for larger devices. Note, that dpi is not the same as screen resolution. So, for example, large 10inch tablet with 1280x800 screen resolution is mdpi device (not hdpi, not xhdpi).
Third. It makes no sense to say "800x600 device is smaller that 1280x800", because they may both be, for example, 4inch phone.
Fourth. Screen resolution have nothing to do with SDK version.
What you need to do, is look for another design for larger devices or ask designer about what he wanted to see. Maybe images shouldn't be strictly sized in dp, maybe they should be sized in percentage of the occupied screen?
Having designed an interface for HDPI in Fireworks (480x800) and now moving it over ready for an MDPI version (320x480) i've come across an issue
Proportionally, the HDPI screen is slightly taller than the MDPI screen.
I know my images will resize, but even still am losing some space height wise
Evyerthing fits on screen beautifully on HDPI version....but now its resized down to MDPI the smaller proportion in height means i'm more stuck for space.
How does one avoid this issue?
I asked the developer if the space between elements can be altered for the MDPI version (eg, on a screen with 8 text fields.... the space between the fields is shortened), he said it will work on percentage of the screen etc.....that's all ok...problem is that
A 480x800 screen -- if this is downsized (proportionally width and height) to a width of 320, the height ends up as 533, not 480.
Therefore i lose 53 pixels in height.
Have I got confused? How do i sort this out?
Much thanks
First of all
Proportionally, the HDPI screen is slightly taller than the MDPI screen.
You can't say taller it or not. Density doesn't define resolution.
Evyerthing fits on screen beautifully on HDPI version....but now its resized down to MDPI the smaller proportion in height means i'm more stuck for space.
So, as I said - MDPI also doesn't means that your screen resolution is less than HDPI screen resolution. For example, I have an tablet with 1280x800 resolution and MDPI screen. Get it?
Looks like it's better for you to provide different layouts for different sizes of devices (small, medium, large, xlarge).
its better use layout weight and also make sure you use desipixel dp instead of px everywhere while designing your ui in xml. The best way to practice while designing ui is to ensure that the application will work and look fine regard less of the screen size
For more information on layout weight check this link
Use a ScrollView. However, I wouldn't recommend designing a layout for a fixed screen size, or even for a fixed ratio.