Galaxy S4 taking values from MDPI? - android

I want to use separate values for each density, so I created different values folders for each one,
values
values-ldpi
values-mdpi
values-hdpi
values-xhdpi
values-xxhdpi
in each of the strings.xml file I wrote the density (for values-xxhdpi i have a string with the text "xxhdpi"), but when I run this on GS4, I get the "mdpi" string,
I also did create separate layout folders, but always taking layout-mdpi density
any clue of what might be going on ?

It looks a bit funny to me to define layouts according to density and not size. Drawable and other similar resources depend on density, but layouts depend on size.
See here : http://developer.android.com/guide/practices/screens_support.html
For values, you could give it a try with values-normal, values-large, etc.
I write this although what you did is supposed to work according to this thread : https://stackoverflow.com/a/9639280/693752
Also, this resource is interesting to you :
http://developer.android.com/guide/topics/resources/providing-resources.html#BestMatch
---- UPDATE ----
Look at this answer from GodFather : https://stackoverflow.com/a/20521720/693752
(Note that density qualifiers are even fuzzier -- -mdpi will be used
for any device density, in the absence of a better match, as density
qualifiers are designed to be used only on drawable directories, where
Android can apply resampling algorithms. Using density qualifiers on
anything other than drawables and maybe dimension resources is a code
smell.)

Related

How does the system find the best-matching Resource?

I have a hdpi device.
DPI: hdpi, density: 1.5, densityDpi: 240, sw: 320
values and values-xxhdpi folders.
Why Android takes dimes from values-xxhdpi not from values?
I assumed it should take it from a smaller folder.
And default values is mdpi and also hdpi is closser to mdpi then to xxhdpi.
Could you please tell me where am I going wrong?
Densities are handled differently than other resource set constraints.
Android density resource set constraints are designed for use with drawable and mipmap. For those resources, Android can automatically convert the resource from one density to another, by upsampling or downsampling the image. This is not the case for other resource set constraints. For example, Android cannot automatically convert an English string resource to Chinese.
When it comes to finding a compatible resource, if you use densities as a constraint for the type (e.g., values), Android behaves as though it can convert the resource from one density to another... even for resource types where that is impossible. So, Android is choosing the closest density, using whatever algorithm it would use for choosing the image to convert, even though you are using the density qualifier on something other than images.
Using density as a qualifier for anything other than drawable or mipmap is a code smell. You are unlikely to get what you want.

1080x1920 420dpi screen not accessing from sw420dp

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.

dimension folder for different screen sizes [duplicate]

I am creating different values folders in my app (values, values-ldpi, values-mdpi, values-hdpi, values-xhdpi, values-nodpi, values-w360dp-mdpi). But some devices that belong same category. But having different screen sizes. But I see give font size according to device densities in this the answer provided by #PankajSharma suggest to create folders like-
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
I want to know what is the difference b/w my way and the other way? I think the answer provided by #PankajSharma is easy. I also want to know which way is better?
The approach you are using is a valid approach, but a little outdated. From HoneyComb, there is a new way to fix all of this. Your resources folder should now look like this:
Please refer to the link I have posted and familiarize yourself with Smallest Width concept.
Hope this helps :)
EDIT: Adding to this post, try to establish some kind of standardization in your dimens.xml, something like this:
Doing this makes it easier to maintain code, plus it reduces the number of dimen folders. Normally rather than having values-hdpi, values-xhdpi, etc. files like values-sw480dp-xhdpi might have more values to adjust, but then again all of this is contextual.
Create a Single layout for default screens 4.7 inch (hdpi) in layout folder and dimensions in values folder. This is your Superset.
Now let say you want your layouts for 7inch devices. Create values-sw600dp folder for 7inch in Portrait orientation
Now lets say you want your layouts for 10 inch devices Create values-dw720dp folder
NOTE :- For landscape just add "-land" in front of folder names.
Now lets say you have new devices such as Xperia SP (4.7' and XHDPI) and Nexus 5(5" and XXHDPI).
For these, you can create values-xhdpi and values-xxhdpi folders and similary add -land for landscape orientation..
I hope you got the point of how to create folders..
Now your superset is defined in values folder. Most of the dimensions will be used from here only. Now run your app in other devices. Whatever mismatch is occuring just add that specific dimension in their respective values folder
To check from which folder your layouts, images are used, use my trick.
Create five same strings and put in it all the values folders like this :-
Default Screen
Screen 4.7
XHDPI Screen
MDPI Screen
Create five drawable folders, most of them will be already there : - drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi
Put the screenshots below in their respective folder under the same name
This is how my res folder looks like and i am supporting all the devices from 4.7 screen and above :-

Size of textview in different screens Android

I'm getting sick to find the best away to put the same scale for textView's in different screens density and resolution.
I tried:
Create different folders, for different density and put "dimensions". Example, folder values-xhdpi and dimensions.xml, values-ldpi and dimensions.xml too. On my code, programmatically i get textView.setTextSize(getResources().getDimensions(R.id.myDimension);
In xml, 20sp
like example above, but put this textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensions(R.id.myDimension);
Add textAppearence , like textView.setTextAppearance(this,android.R.style.TextAppearance_Large);
But all examples, i got what i wasnt expected, because got texts biggers in biggers density. like in image below
this happen except if i change my dimension in appropriate xml. But, what structure should i follow?
For an example, in drawbles, people reccomend to use scale like 3:4:6:8, but here i didnt follow a structre.
What are the best pratices ? What people normally do..?
What i want? this..
You have to use a pixel independent unit for your text size. Make dimensions with sp unit and load them programmatically.
For supporting multiple screens take a look in android documentation
For the sp unit says :
Similarly, you should prefer the sp (scale-independent pixel) to define text sizes.
The sp scale factor depends on a user setting and the system scales the size
the same as it does for dp.
In other words you have two
You have to declare in values file dimension elements. If you don't like the result and you want to declare for each screen category specific text size you have to do the following :
You can create a values folder for each dpi. For instance , the folder name can be : values-hdpi , values-xhdpi etc.
Inside the values file you can place a dimens.xml file in which you will declare as
<resources>
<dimen name="small_font_size"=10sp></dimen>
<dimen name="large_font_size"=20sp></dimen>
</resources>
It's up to you. You can create different dimensions files for different screen sizes. For example:
values-sw720dp 10.1” tablet 1280x800 mdpi and bigger
values-sw600dp 7.0” tablet 1024x600 mdpi and bigger
values-sw480dp 5.4” 480x854 mdpi and bigger
And take the value in sp for your TextView from your dimensions files.

How does Android uses values folders?

I have the font size defined in my dimens.xml (e.g <dimen name="name_text_size">12sp</dimen>)
Since this size wasn't ok on all screens I created some additional folders with different values:
values-xhdpi
values-xlarge-mdpi
values-large-mdpi
I thought this should be enough, since before creating this layouts the text look ok on ldpi, mdpi and hdpi screens, but actually the font became too large. The problem is that I am not able to figure out from which file it takes the value now for these screens. Initially, I thought it uses the default value from the folder "values", but actually no layout is using that value. Can somebody help me understand(I sow Providing documentaion and Support Multiple Screens) how this folders are working and how could I optimize the number of defined resources (e.g. xhdpi and large-mdpi have the same values)?
The solution is simple create the following folders:
values-ldpi
values-mdpi
values-hdpi
But I don't want to make things more complicated than they must be.
Thanks in advance :)
You can provide the different string text in each Values-folder. Once it will run you can check from which folder its taking the string values.
You can achieve this by this trick.

Categories

Resources