Android CSS for different screen sizes - android

I have a simple web page that displays some text and images and trying to find a non complicated way to handle multiple screens.
I have looked through the docs and the only way I can see to do it right now is having to create three separate css files, one for low, one for medium, and one for high res. Unfortunately each one of these have to have different font sizes and dimensions of every element/image.
This seems rather wasteful considering every hdpi element should just be exactly 1.5x the size of the normal one. Is there not a way to make Android just automatically scale this for us while using hdpi versions of the images instead of mdpi ones?
Am I missing something here?

You can try using Media Queries in your stylesheet.
Basically you say if viewport is smaller than X amount then initiate (or make changes to) this style or if viewport is larger then Y amount, implement (or update) this style. You can have as many possibilities as you wish and there is no need for extra styles because the change is implemented on the particular class or ID you choose once a certain width is reached.
Check out this article on Responsive Web Design
Hope this is the sort of info you are after?

I just looked on the very same issue. Well, it seems that Android doesn't automatically adjust the content of Webview (the image, the font size, etc) according to the screen size like what it does automatically with the app icon.
But, regarding the icon, actually, it's not automatically adjusted as well. You still have to provide the icon in three different sizes to accommodate the ldpi, mdpi, and hdpi resolution.
That's why we need to provide three different css files. But, actually, if you read the doc you mentioned previously, you can write those three different css in one file, like this:
header {
background:url(medium-density-image.png);
}
#media screen and (-webkit-device-pixel-ratio: 1.5) {
/* CSS for high-density screens */
#header {
background:url(high-density-image.png);
}
}
#media screen and (-webkit-device-pixel-ratio: 0.75) {
/* CSS for low-density screens */
#header {
background:url(low-density-image.png);
}
}
The upper part is for the mdpi, the middle one for the hdpi, and the lower part is for the ldpi. So, you can easily manage one file for those three different screen resolutions.

Related

Resources for multiple screen sizes

As far as I know it is possible to provide multiple resource paths for multiple dpis (mdpi, hdpi etc.). What if I want to support multiple screen sizes at the same dpi?
For example, if I have an application that draws an icon whose size is always a percentage of the screen size (e. g. 25 %) and I replace the screen by a larger one with the same dpi, then the icon has to be scaled up. This will make the icon blurry unless I provide a version with a higher resolution. Since Android only distinguishes by dpi and not by screen size, how can I do this?
If the icon is included in the APK, you may as well just put just one copy - the highest resolution you have. Large icons can be scaled down just fine, it's scaling small icons up that causes the blurriness. The reason for including different sizes is to save bandwidth when icons are downloaded, but since all the copies would already be on the device, in effect all you will be doing is making the APK bigger. First prize would be to include a vector image (infinitely scalable, small size).
You should also note that the blurriness is really only in comparison with the sharpness of the rest of the display. In reality the 2x scaled-up icons would look just as good as the same icons on a display with a density half as much.
If, however, you still want to select a certain copy of an image based on the dpi and screen size, there are ways you can detect this in Android. See this SO question, for example.

Screen sizes and densities for Android

I'm currently working on 2 Android projects and seems like I have a problem with understanding how to handle the density of resources. I have a HTC One M8, which has ~440 dpi, therefore I'm using XXHDPI. I always use XXHDPI resources and wrap content for the elements. On one of the projects, all the resources are png formats, again using XXHDPI and wrap content and when running the app, it looks great.
On the other hand, the resources from the other project are .9.pngs. Whenever I use XXHDPI and wrap content, in the preview, the design of the elements is altered, the pictures are really shrank.
However, if I use a HDPI, the size of the picture will look good, but the pixel's density will be crap. As far as I know, I should never hardcore the width and the height of the picture, but get the picture with the specific density and just use wrap content.
Am I missing something or there is a problem with the resources?

Supporting multiple screen size - Android

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.

How to make images for different screen sizes?

Getting my head around Android, I've sort of trouble understanding support for multiple
devices. I've gone through the dev-docu, now for the clear-up:
The idea behind using dp is, that e.g. a button has the same 'real-world'-size, independent of the actual screen-size, right?
If so, that means, I provide the images in ldpi, hdpi and so on. That results in a button, that has always the same size.
If still correct, I understand that now, that my buttons always has the same size, I should provide different layouts, because on a small device, there might only be space for two next to one another, where on a larger device there's room for three.
If all of the above is correct, my question would be, how I deal graphics-tool-wise with it. Say I have a button 100px x 100px at 300dpi, what sizes and dpi's would that be for the new documents in e.g. Gimp for the different screensizes?
Thanx in advance for any smoke-lifting!
Marcus
What it means when you can use your custom assets for different screen densities is that when you put those respective assets, they will not scale again and use them as they are. Now it is true that the size of the screen also varies. That is when you can consider tweaking your layout.
For example in a large screen you may be able to fit two components beside each other and use a linear layout with horizontal alignment. But when it comes to a small screen, that might not be possible and one option is to define a new layout for small screens and say pt those two components in a vertical layout.
For more information regarding how to implement this and best practices, check these Android Docs
After some more study, it seems that the graphocs-tools DPI are actually the one referred to in e.g. the baseline. So to create a baseline-doc, you indeed create a document 470 x 320 pixel in size and a resolution of 160 dpi. The rest goes from there.

Android orientation basic question

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!

Categories

Resources