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.
Related
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.
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 :-
Hello guys i know this is an old question but my requirement is different
i searched a lot and all answers are outdated not for xxxhdpi and tablets
I always create a new layout of each different screen and place it in respective folder it all works fine but in every project doing every layout is such a pain .so i came across the concept of dimens concept where you can place margins in dimens.xml in different values folder and our app will take it based on screen size
What i need is what are the exact folders i need to take and what should be the percentage of values in each folder some thing like this answer by salman
And if i give margin like
android:layout_marginTop="#dimen/dim_300"
If that button comes to centre in small screen ,it should also come in centre in xxxhdpi screen or any other device
and in dimens i will write some thing like this
<dimen name="dim_3">4.5dp</dimen>
i need answer in this format
folder name and percentage
values-sw480dp 2%
values-mdpi 1%
values-ldpi 0.75%
etc
thanks in advance
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...
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 :-