1080x1920 420dpi screen not accessing from sw420dp - android

I am creating different values folder for supporting different screen sizes.The following are the folders:
values-sw320dp-hdpi
values-sw320dp-xhdpi
values-sw320dp-xxhdpi
values-sw420dpi
values-sw560dpi
values-sw480dp-hdpi
values-sw480dp-xhdpi
values-sw480dp-xxhdpi
values-sw600dp
values-sw720dp
In the list of devices provided by Android Studio there is 1080x1920 420dpi.It should access dimens values from values-sw420dp.But instead it is accessing from sw320dp-xxhdpi.The layout in 1080x1920 420dpi **looks slightly different from **1080x1920 xxhdpi. Can anyone explain to me why it is not accessing from its own folder?Or could you explain to me the correct way to create a layout so that it support different screen sizes with different densities?I have referred different sites.They are confusing..Please help me!!

I am creating different values folder for supporting different screen sizes.
First, using density qualifiers (e.g., -xhdpi) on resource types other than drawable and mipmap is a serious code smell. Almost assuredly, you are not going to get the results that you expect.
Second, there is no -sw420dpi or -sw560dpi qualifier. You could have -sw420dp or -sw560dp, to say that you want to use these resources for those screen size thresholds. However, such directories would never be used, because of your density qualifiers on directories like values-sw320dp-hdpi.
In the list of devices provided by Android Studio there is 1080x1920 420dpi.It should access dimens values from values-sw420dp
No, it should not.
The smallest width of that screen is 1080px. 1080px, at 420dpi, is 411dp (1080 * 160 / 420). 411 < 420. Hence, anything that is -sw420dp will not qualify.
But instead it is accessing from sw320dp-xxhdpi.
Partially, that is because 411 is lower than 420.
Partially, that is because you are using density qualifiers here, which short-circuit a lot of the "normal" rules for resource selection.
The layout in 1080x1920 420dpi **looks slightly different from **1080x1920 xxhdpi.
Your question has no layouts, so nobody can comment on that.
Can anyone explain to me why it is not accessing from its own folder?
There is no "its own folder".
Or could you explain to me the correct way to create a layout so that it support different screen sizes with different densities?
Use layout resources based on screen size and (perhaps) orientation (e.g., res/layout-sw420dp/)
Use dimen resources measured in sp (for sizing text or things that are dominated by text) or dp (for everything else) in those layout resources (e.g., <dimen name="margin">16dp</dimen>)
Use density qualifiers, and maybe size qualifiers, for drawable and mipmap resources (e.g., res/drawable-hdpi/)
Frequently, that is sufficient.
I have referred different sites.They are confusing
Perhaps consider reading a book.

Related

calculate dp for different screen for supporting multiple screens

I know there are many questions like this, I have read many blogs and questions on this and I am not satisfied by what I have understand.
I want to support multiple screens and resolutions like hdpi, xxxhdpi etc.
And i am using dimens.xml for this purpose. But m still unclear about how to calculate exact dp value for different screens.
For example if 48dp x 48dp ImageView is proper for hdpi device, what values should i define for xxhdpi device? Is there any fixed calulation just like there is for drawable images?
I am using these folder for trying to support multiple screens :
values-sw320dp-hdpi
values-sw320dp-xhdpi/xxhdpi/xxxhdpi
There are few problems I am facing currently
Though defining values in respective dimens.xml works almost for most of the devices, there are few devices which takes wrong values
For example I have Lg optimus G that is xhdpi device, but it reads values from values-sw320dp-xxxhdpi's dimens.xml instead of xhdpi one
I am not able to calculate the exact value for each resolution (hdpi,xhdpi etc), so the view which looks perfect in hdpi device, seems little large or small in xxxhdpi as I can't guess the value like if hdpi view size is 48dp then xxxhdpi should be of 64dp or something as I don't know the exact approach.
Also while searching for supporting different screens, I read many times about calculating dp at runtime based on density or calulating pixels etc.
I am too confused about all this. Please help me in understanding and learning the proper way of making responsive apps.
You can use resource qualifiers to specify different sizes for Views.
If you want the width of an ImageView to vary based on the size of the screen, create a new dimension in your dimens.xml file called image_view_width (for example).
This then essentially creates a copy of the dimens.xml but adding the 'Size' qualifier. This will create another version of the dimens.xmlfile, and any device that meets your specified qualifications will use that version of dimens.xml. You then add image_view_width to this new file and give it a different (smaller/bigger) value. You can do this as many times as you want and with as many different qualifiers as you want.
Finally, when you want to use this value in your layouts, you only have to type:
android:layout_width="#dimen/image_view_width"
and android will do the rest for you.
Hope this helps!

What order does Android read values folders?

In my Android project I have multiple values folders with extensions such as -hdpi, -xhdpi, and -sw720dp. In all of these folders I have dimens.xml and the same names for values in that xml file. If I have a device that has a width of 720 dp and is counted as an xhdpi device (as an example), then from what folder does Android read the values from? Also, in general, how does Android prioritize the different values folders?
Also, in general, how does Android prioritize the different values folders?
Generally speaking, Android takes the resources from the resource set that has the resource in question and has the most important distinct qualifier. Here, "most important" means "appears highest in Table 2 on this Web page", and "distinct" means "nobody else has that particular qualifier". Though things get a bit weird with screen sizes and densities.
I have multiple values folders with extensions such as -hdpi, -xhdpi, and -sw720dp. In all of these folders I have dimens.xml and the same names for values in that xml file.
IMHO that is a serious code smell. Use density-independent units of measure (e.g., dp), in which case you should not need different dimension resources based upon screen density. Having different ones based upon screen size is reasonable (e.g., bigger margins on tablets), though the mix then would be something like res/values/dimens.xml and res/values-sw720dp/values.xml.
If I have a device that has a width of 720 dp and is counted as an xhdpi device (as an example), then from what folder does Android read the values from?
-swNNNdp is higher in Table 2 than is -hdpi or -xhdpi, and so it should be considered first. Note that the size resource set qualifiers (e.g., -large, -swNNNdp) are for the specified size or larger. So, if your device has 720dp or more in the smallest width, your -sw720dp resource should be used.

Android : the mystical technique to set different dimensions for different device / screen sizes

My first Android App. And, am trying to get this sorted -> setting different text sizes, layout sizes for different screen / device sizes. Now, I Googl'ed a lot , then read several Stackoverflow threads, & am doing this:
Created different folders under 'res' like
values-ldpi
values-mdpi
values-hdpi
values-xhdpi
values-xxhdpi
values-xxxhdpi
Then I put a dimens.xml & styles.xml in each folder above.
Phew! But this doesn't suffice. I want to have different textsizes (and other dimensions, am not speaking about images here) for phones & different ones for tablets. I know there are these 'values-sw600dp, and 'values-sw720dp' which they say is aimed at tablets. But even within each tablet size, I need to differentiate the different densities right? For example, within 'values-sw600dp', I need to have different textsizes for, say, hdpi, xhdpi, ... How do I do this? Any help please?
Here is an example of what am doing:
TextView textView = (TextView) findViewById(R.id.text_interference);
textView.setHeight(layoutHt);
textView.setWidth((layoutWd-20)/2);
int dimensControlButtonTextSize = getResources().getInteger(R.integer.CONTROL_BUTTON_TEXT_SIZE);
textView.setTextSize(dimensControlButtonTextSize);
Then in dimens.xml, it would be:
17
This value, i.e. 17 here, will differ in each device i.e. it is given a different value in each dimens.xml under each values- folder. I hope am clear.
Drawable resources do not need to be duplicated per screen configuration, except for special cases like you have a full screen splash that would be displayed differently for landscape and portrait. Normally for icons, specifying once in the main resources folder (under hdpi, mdpi, xhdpi, xxhdpi...) should be sufficient.
I would find screen configuration specific resources to be useful for dimen values, like padding, margin, width of items, etc...

Adjust view sizes in android for custom devices

I have 3 custom made android devices.
First one has a 5 inch screen with 1280x800 res
Second has a 5 inch screen with 800x480 res
Third has 7 inch screen with 800x480 res
I tried giving sizes with dp, px and inches but it seems they cant support those screens properly with the same value (even inches seems to be not exact inches but translation to px eventually).
How can i use same code to properly adjust view sizes relatively to the screen size?
Well, as first, the correct measure unit to use is: dp for controls and sp for fonts.
These are normally for margin, paddings, widths, heights, and other attributes.
Also xml drawables can take advantage of pixel independency
Define your dimensions in a res/value/dimens.xml file (this is a PROPOSED standard name, you can call it whatever you like best), in order to have them referrable from all your code, instead of being hardcoded and often repeated in many files.
Then you must know that you should provide your graphics AT LEAST in mdpi resolution (160 dpi), which will be scaled up or down to match other resolutions.
Notice that I said at least.
For every resolution you are supporting, you should add a folder in your res path containing the graphics at the corresponding density for that resolution.
this means that you will have, let's say 3 resolutions mdpi, hdpi and xhdpi (today's favourites, excluding tablets - these ones deserve some folders on their own):
The graphics is going into:
res/drawable-mdpi
res/drawable-hdpi
res/drawable-xhdpi
Just put your graphics (with the same names) with (respectively) a dpi density of 160, 240, 320 in those directories.
Now your graphics is resolution compliant.
Now, I don't realize what the problem really is.
I mean, is it the background not fitting well? then the solution is to use a tiling or an "abstract enough" stretchable picture.Or you could use 9 patches, as well
If the problem is how the fonts and other objects interact with each other, you should always reference to an mdpi device (even emulated, if you don't own a physical one). When things scale well on a mdpi device, they are supposed to scale well on every device.
For tablets in particular, you are supposed to provide specific folders for values (where you put your dimens.xml file, containing the dimensions).
These folders normally have names like values-sw600dp or values-sw720dp-land. The suffix land indicates landscape mode, the particle sw###dp indicates the minimum dimension (width or height), so, I guess that in you case you could prepare some folders called values-sw480dp and values-sw480dp-land and there you would put your dimens.xml file, with the special dimensions for that particular device.
I guess that providing only the non-land folder would be enough.

Density-specific layouts vs. size-specific layouts in Android

For quite a long time, I have been declaring resource folders for 4 different densities:
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
In the layout's XML, I have been using fixed widths (while still density-independent) such as 128dp for those graphics.
However, when more and more large-screen phones, and especially tablets, were introduced, that approach did not work anymore. Although you provide density-independent resources this way, the layout will not look good on large screens.
This is why I think I need to add Dimension resources that depend on the screen size, for use in the XML layouts, e.g.:
values
values-w600dp
values-w720dp
values-w1024dp
But does that mean that I should drop supporting those 4 density containers? Or do I need to provide 16 resource folders, i.e. one for every combination of density and size?
I can't find any good help in the Android documentation as to this topic.
drawables and layouts are different. To answer your question, should you stop support those densities. Yes, but you should still support xdpi and hdpi. Romain Guy recently said that modern devices like the Nexus 7 (at a tvpi) can scale the assets properly enough that mdpi isn't really needed. And nobody uses ldpi anymore. Last I looked is was less than 2% of the market.
About layouts. A Nexus 7 (1280x800 tvdpi) would use something from the values-w1024dp but still get assets from the drawable-hdpi folder. Those two aren't mutually exclusive. Something like a S3 would also pull from the values-w1024dp but use drawable-xdpi. You only need to provide an alternative layout if your use-case calls for it.
So do you need 16 different things? No. You do need xdpi & hdpi (if not mdpi). You may want to include alternative layouts for different sizes. You can be as specific as you want or as generic. Unless you're doing a hybrid app for both phone & tablet (7 & 10 in) you probably don't need a lot of xxxx-sizexxx folders.
In the layout's XML, I have been using fixed widths (while still density-independent) such as 128dp for those graphics.
This is probably a source of your issues. Your layouts should be as fluid as possible using wrap_content and match_parent. Fixed sizes should be reserved for padding around the sides and image where you know the size ahead of time. If you do this, your layout should look decent at any size from a small 320 x 200 to a GTV size.
1) Regarding dimensions in your layouts (values/dimens.xml):
values values-w600dp values-w720dp values-w1024dp
But does that mean that I should drop supporting those 4 density
containers? Or do I need to provide 16 resource folders, i.e. one for
every combination of density and size?
No, you don't need to provide different dimensions per dpi bucket (hdpi/xhdpi), because the dimensions are already being scaled up or down based on the device (if you're using dp instead of px). So for dimensions, you only need to provide values for devices with different sizes (hence the name, values-smallest possible width-600-dp). Because you don't want 16dp padding on a phone AND 16dp on a 10" tablet as well. You'd want 64dp instead. And no, it doesn't matter what density the device has. It still needs to have the same padding on the respective device width. So for dimens, you only need to think about the device's actual physical dimensions.
2) Regarding drawables scaling for different resolutions (drawables/xdpi):
The system scales them appropriately for the device. You don't need to worry about this. Also, you don't need to add any other buckets here. Just use mdpi/hdpi/xhdpi and maybe xxhdpi because many new devices are going to use the new density in the future.
Conclusion: there are 2 different UI building components that vary according to 2 different rules: drawables based on screen density and dimensions based on screen size. Do not mistake one for the other and think you need tens of buckets in the values folder, because that's not only wrong, it's just mind boggling.

Categories

Resources