I am working with different screen resoultions for android devices, i have added the layout folders for default, small and xlarge size devices and also the dimens as below :
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="activity_horizontal_margin_small">8dp</dimen>
<dimen name="activity_vertical_margin_small">8dp</dimen>
<dimen name="activity_horizontal_margin_large">64dp</dimen>
<dimen name="activity_vertical_margin_large">64dp</dimen>
and added the below code into the manifest file :
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
folder format for layouts looks as :
res-
--layout
--layout-large
--layout-small
The layout file consists dimensions specific to the resolution and everything is being followed as per the guideline
But the changes are not reflected on the actual devices with different resolutions. i.e. I always see small margin on all devices. Am i missing something.
The grade version I am using is, 2.14.1
Related
I created my application in only one layout. but now I need to support high resolution screens. I have a ListView getting images from JSON.
android:layout_width="fill_parent"
android:layout_height="174dp"
Images are shown good size on 4 inch device, but on tablet images are cropped from top and bottom. so i added
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ellidokuz.com"
android:versionCode="1"
android:versionName="1.0" >
<supports-screens
android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"
android:xlargeScreens="true"
/>
this code to manifest. then copied list_v.xml to drawable-xhdi and drawable-xxhdi.
and then I changed
android:layout_width="fill_parent"
android:layout_height="174dp"
to
android:layout_width="fill_parent"
android:layout_height="350dp"
but still my application running the xml file in the layout folder. How can I set it to run xml file in drawable-xxhdpi folder?
just simple copy and paste your xml into different Layout folders
You don't have to put your XML layout file into drawable folders!!
Instead just just put it in:
layout-small
layout-large
layout-xlarge
...
There is some convention to achieve this as per google developers blog recommended. If you want to achieve your layout should work for both phones and tablets then there is a way to do is, create different folders for layout like layout-small, layout-mdpi, layout-hdpi, put your xml file into all the folders, but you must have same drawables folders as well with different sizes of images.
Refer this : link
High-resolution screens exists in different screen sizes. That means you should create different layout folder like layout-sw720dp for 10-inch tablets. Then you should create a corresponding drawable folder like drawable-sw720dp-xhdpi were you can put your pictures in. I wrote a blog about this. It covers more than your question and should help getting you started.
http://objectofclasshuman.blogspot.de/2014/04/android-user-interface.html
I am currently working on an Android app. I am not an UI guy, and just trying out which number fits the best when I make the layout xmls like below:
<ImageView
android:id="#+id/my_icon"
android:layout_height = "30dp"
android:layout_width = "wrap_content"
android:layout_gravity = "center_vertical"
android:scaleType="centerCrop"
/>
I know that 30dp will work ok on xxhdpi devices because that is what I am using and testing, but I think this will cause trouble when the screen resolution is lower (say, a mhdpi device).
I am wondering if there is any guideline on how to make a layout support different res?
You can find giudelines in following links
How to support different screen size in android
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/guide/practices/screens_support.html
http://developer.android.com/training/basics/supporting-devices/screens.html
Here you can find some guidelines:
http://developer.android.com/design/style/devices-displays.html
For Different screen size, The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for small, medium, high, and extra high density screens.
res/layout/layout.xml
res/layout-small/layout.xml
res/layout-large/layout.xml
res/layout-xlarge/layout.xml
res/layout-xlarge-land/layout.xml
res/drawable-mdpi/icon.png
res/drawable-hdpi/icon.png
res/drawable-xhdpi/icon.png
The following code in the Manifest supports all dpis.
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
I am having an issue with scaling Android across multiple screens. According to the documentation Android OS will assume your dimens.xml is created for mdpi and scale accordingly for hdpi, xhdpi, etc...
However, I am not able to see this work. If I have a folder for values-hdpi my SGS4 looks fine with the 1.5 scale ratio applied from the dimens.xml file located in the values-hdpi. However, whenever I remove the values-hdpi folder from the project to allow Android OS to scale it up to hdpi, the SGS4 just runs mdpi values. I'm not sure what I'm doing wrong.
Android Manifest:
<!-- Screen Support -->
<supports-screens android:resizeable="true"
android:smallScreens="false"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"/>
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="15"/>
Folder structure includes
values
values-es
values-fr
values-sw600dp
values has the mdpi values stored in the dimens.xml file all done in dp and sp.
dimens.xml:
(NOTE*) Whenever I tried to add the xml snippet from the dimens.xml it kept formatting it funny and removing all the dimen name= stuff and just lists out the dp and sp used in it. Not sure why.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="center_text_margin_right">20dp</dimen>
<dimen name="answer_row_top_padding">7dp</dimen>
<dimen name="answer_row_bottom_padding">7dp</dimen>
<dimen name="answer_number_text_size">12sp</dimen>
<dimen name="answer_left_margin_from_answer_number">18dp</dimen>
<dimen name="answer_text_size">12sp</dimen>
<dimen name="answer_points_right_margin">20dp</dimen>
<dimen name="answer_points_left_margin">18dp</dimen>
<dimen name="answer_number_left_margin">60dp</dimen>
<dimen name="center_text_size">12sp</dimen>
<dimen name="center_text_margin_left">20dp</dimen>
<dimen name="center_text_margin_right">20dp</dimen>
<dimen name="answer_title_height">28dp</dimen>
<dimen name="round_circle_margin_left">6dp</dimen>
<dimen name="round_circle_height">6dp</dimen>
<dimen name="round_circle_width">6dp</dimen>
<dimen name="total_score_right_margin">26dp</dimen>
<dimen name="total_score_left_margin">5dp</dimen>
<dimen name="score_text_size">12sp</dimen>
<dimen name="trivia_logo_height">25dp</dimen>
<dimen name="trivia_logo_width">84dp</dimen>
<dimen name="trivia_logo_margin_left">8dp</dimen>
<dimen name="question_margin_left">26dp</dimen>
<dimen name="question_margin_right">26dp</dimen>
<dimen name="question_top_margin">15dp</dimen>
<dimen name="question_margin_bottom">20dp</dimen>
<dimen name="question_text_size">12sp</dimen>
<dimen name="fun_fact_margin_bottom">100dp</dimen>
<dimen name="fun_fact_text_size">12dp</dimen>
<dimen name="answer_row_height">25dp</dimen>
</resources>
If I create a values-hdpi folder with this file
dimens.xml
30.00dp
10.50dp
10.50dp
18.00dp
27.00dp
18.00dp
30.00dp
27.00dp
90.00dp
18.00dp
30.00dp
30.00dp
42.00dp
9.00dp
9.00dp
9.00dp
39.00dp
7.50dp
18.00dp
37.50dp
126.00dp
8dp
39.00dp
39.00dp
22.50dp
30.00dp
18.00dp
150.00dp
18.00dp
37.50dp
everything will work fine
The values-es and values-fr are just folders containing strings.xml NO dimens in there. I reference all throughout my layout xml files with #dimen/answer_row_height for example.
As I stated before it works perfectly if I drop in the values-hdpi with a scaled dimens.xml file in it, but if I remove it Android does not scale it. Has anyone hit this before or know what i'm screwing up?
Values will not be automatically scaled, only drawables will be.
It's up to you to define all the alternative resources (values included) that your app will need, the framework cannot and will not make those decisions for you.
According to the documentation Android OS will assume your dimens.xml
is created for mdpi and scale accordingly for hdpi, xhdpi, etc...
That is incorrect. Whatever you put in dimens.xml is going to be used as is, if the dimensions are in pixels (px). You'll need to put them in density-independent-pixels (dp) to make them scale. dimens.xml should not be based on resolution but on screen size. You might have a dimens.xml in values and in values-large, specifying different spacing, or even different font-sizes for larger screens.
You should not have a dimens.xml in values and values-hdpi, because the dp unit already takes care of density-scaling.
Based on the comments you left in various places, it appears that you are not accessing the dimensions resources correctly. You should be using something like:
getResources().getDimensionPixelSize( R.dimen. round_circle_height );
if retrieving values from code, or
#dimen/round_circle_height
if referencing from xml.
I am adapting an application written for a Galaxy Tab 2 (10".1) screen size to a Nexus 7 (7").
Following the suggestions here in stackoverflow I created different image sizes for all densities (l|m|h|xh)dpi and also a specific layout-sw600dp folder which contains my layouts modified for the 7 inches screen.
[EDIT]
And I added
<supports-screens android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
to my Androidmanifest file.
[/EDIT]
Notwithstanding all these modifications I am still struggling having the drawableBottom (and Top, Left, Right) of my Buttons correctly scaling between the two devices.
The physical size of the rendered images is the same. So the Nexus is making the icons as large as the Galaxy (or viceversa).
From what I understood the two devices, although being of different physical size, they share the same density of pixels (xhdpi), that makes the Nexus picking up from the xhdpi folder.
Am I missing something important?
How should I adapt my drawableBottoms?
Have you made sure the app is designed for high density displays? Try adding this to your manifest after permissions:
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
OK.
Probably this is not "THE" solution as it is meant to be. But it worked for me straight away and with a minimum effort considered the size of the application I had to adapt.
This following solution basically works quite nicely but (probably) only for this specific case since the Galaxy Tab 2 and the Nexus 7 share the same resolution.
How I fixed it with almost no work:
Do not create the smaller resolution images (you don't really need it), unless you are going to install (or have installed) your app in many other devices.
Create two values- folders. One for the Galaxy Tab 2 ( = xlarge), the other one for the Nexus 7 ( = large). The modifier is the size of the screen.
Inside each of the folders place a dimen.xml file which you will fill respectively like.
For the Galaxy Tab 2:
<?xml version="1.0" encoding="utf-8"?>
<resources>
[...]
<dimen name="s25sp">25sp</dimen>
<dimen name="s30sp">30sp</dimen>
<dimen name="s35sp">35sp</dimen>
[...]
<dimen name="s25dp">25dp</dimen>
<dimen name="s30dp">30dp</dimen>
<dimen name="s35dp">35dp</dimen>
[...]
</resources>
For the Nexus 7:
<?xml version="1.0" encoding="utf-8"?>
<resources>
[...]
<dimen name="s25sp">17sp</dimen>
<dimen name="s30sp">21sp</dimen>
<dimen name="s35sp">24sp</dimen>
[...]
<dimen name="s25dp">17dp</dimen>
<dimen name="s30dp">21dp</dimen>
<dimen name="s35dp">24dp</dimen>
[...]
</resources>
Each value for the Nexus 7 is obtained like: Galaxy_value/10.1*7
After that you have to use the #dimen/s[0-9]*dp whenever you want to use that specific dimension.
PS:
If you have a big application with many layouts already formatted you could use this PERL script to convert every [0-9]*dp and [0-9]*sp into #dimen/s[0-9]*dp #dimen/s[0-9]*sp automatically.
Here it is:
#!/usr/bin/perl
$filename = $ARGV[0];
open (FILE, $filename);
while (<FILE>) {
chomp;
if ( $_ =~ /\"[0-9]*(di?p|sp)\"/) {
my $pre = $`;
my $mat = $&;
my $pos = $';
$mat =~ s/["]+//g;
print "$pre\"\#dimen/s$mat\"$pos\n";
} elsif ( ($_ !~ /\"[0-9]*dp\"/) && ($_ !~ /\"[0-9]*sp\"/) ) {
print "$_\n";
}
}
close (FILE);
exit;
I am designing for supporting multiple screen size application using dip like margin_top etc. in 3.2. What are the folders I have to create for the value folder? Where do I have to specify dip? Can anybody tell me how to do this? If I had given value-11 folder and loading layout. I got a compilation error no resource found error, but I have layout there.
Thanks
There are various ways to switch your layouts. Here are the top two:
Suppose in your code, you are going to inflate R.layout.my_layout.
Switch directly by adding selectors to layouts
Implement layouts:
res/layout/my_layout.xml
res/layout-land/my_layout.xml
res/layout-w600dp/my_layout.xml
Switch indirectly by adding selectors to values
Implement layouts:
res/layout/my_layout_narrow.xml
res/layout/my_layout_wide.xml
Implement values:
res/values/layouts.xml
<resources>
<item name="my_layout" type="layout">#layout/my_layout_narrow</item>
</resources>
res/values-land/layouts.xml
<resources>
<item name="my_layout" type="layout">#layout/my_layout_wide</item>
</resources>
res/values-w400dp/layouts.xml
<resources>
<item name="my_layout" type="layout">#layout/my_layout_narrow</item>
</resources>
res/values-w800dp/layouts.xml
<resources>
<item name="my_layout" type="layout">#layout/my_layout_wide</item>
</resources>
Use dp instead of dip and put the below lines in The Android Manifest.xml file above <application/> tag & put your all images in simply drawable folder .Doing all these changes your app support all screen sizes.`<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" >
</supports-screens>
See the guide topic Supporting Multiple Screens > Declaring Tablet Layouts for Android 3.2. You can specify resource folders for available width and height or for smallest width. For instance, you might have
res/values-sw600dp in which you would define margin_top when the smallest screen dimension available to your app is at least 600dp
res/values-w1024dp in which you would define margin_top when the screen width available to the application is at least 1024dp
etc.