I have a problem when I launch my Android app on an Acer 7 inches tablet (Android 6.0). Until changing the tablet orientation, the fontSize is bigger that it should be. I don't have this problem with other tablet.
At the end of an onCreateView, I get resources information with getResources().getConfiguration().fontScale. With a normal system fontSize, it return 1.15 when app is launched and return 1.0 when changing orientation and remain 1.0 after even if I changed orientation again and again. With normal size font on the other tablet fontScale is always 1.0.
I don't know where to look to solve this problem.
For font change for tablet UI you create the new layout for tab and set the text size according to the tab UI.
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml
Related
When I create small,normal,large,xlarge layouts:How Android Detects Screen Size phone for use this layouts?base on dp or dpi or px?
Is the result always right?Because some devices are smaller in physical size but the density is higher so if based on density this detect is not always correct.
Am I right?
Android uses dp, but there are a lot of variants you can create in your resources for example
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
You can cover more resolutions using different configs, but I think is hard work to cover everything, because how I said we have different resolutions, screen sizes and now we have notch.
res/layout-560dpi/
res/layout-ldpi/ ...
Bellow, I copy a link about this
https://developer.android.com/training/multiscreen/screensizes
I want to set one layout for sw360dp in portrait, and one layout for sw360dp in landscape.
I have read: http://developer.android.com/guide/practices/screens_support.html#qualifiers and http://developer.android.com/training/multiscreen/screensizes.html and How do I specify different layouts for portrait and landscape orientations?
I followed the guide but I cannot make it success. I have made the following directories and layout files:
/res/layout/mylayout.xml
/res/layout-sw360dp/mylayout.xml
/res/layout-sw360dp-land/mylayout.xml
I can see on my device (a phone) with large screen that use the "layout-sw360dp" layout. However when I rotate my device to horizontal, it cannot change to show my "layout-sw360dp-land" layout, it just use the "layout-sw360dp" and rotate it horizontally.
I am using Android Studio to develop the app.
Thank you.
Try to read this blog explains just what you wan't to know...
res/layout-sw600dp/main_activity.xml # For 7” tablets
res/layout-sw720dp/main_activity.xml # For 10” tablets
res/layout-sw600dp-land/main_activity.xml # For 7” tablets // for Landscape mode
res/layout-sw720dp-land/main_activity.xml # For 10” tablets // for Landscape mode
Try removing the 's' from 'sw' and just specify the width which will change its value when you rotate the device.
Look in Table 2 under "Available screen width" for more information at https://developer.android.com/guide/practices/screens_support.html
Quoting from it:
The system's corresponding value for the width changes when the screen's orientation switches between landscape and portrait to reflect the current actual width that's available for your UI.
You should get the desired effect.
I'm newly on android development and i build an app on android studio and i want to make my app have same design in all devices:
Samsung Galaxy S4 => 1080x1290 ; 5.0”
Galaxy Nexus => 720x1280 ; 4.7”
Nexus 4 => 768x1280 ; 4.7”
Motorola Droid Razr M => 540x960 ; 4.3”
Nexus S => 480x800 ; 4”
Galaxy S2 => 480x800 ; 4.3”
Galaxy Ace => 320x480 ; 3.5”
Galaxy Note => 800x1280 ; 5.3”
Galaxy Note II => 720x1280 ; 5.5”
Nexus 10 => 2560 x 1600 ; 10.1”
Galaxy Tab 10.1 => 1280*800 ; 10.1”
Galaxy Note 8.0 => 1280*800 ; 8.0”
Galaxy Tab 7.7 => 1280*800 ; 7.7”
Nexus 7 => 1280*800 ; 7.0”
Galaxy Tab => 1024*600 ; 7.0”
and i read these and lots of question here
http://developer.android.com/training/multiscreen/index.html
http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts
i used the width/height in "dp" and made this with all items
android:layout_margin
android:paddingLeft
android:paddingRight
android:paddingTop
android:paddingBottom
and it works fine, but as i know there is a method that read the device screen then decide which layout will open in the app, assume i have one layout and i have these size for it
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
My question is : how i code my java activity to read the device screen which my app run on it then decide which layout will open like if the device is S2 then the java make something like this:
if (tabletDeviceSize) {
//use tablet support layout
} else
{
//another layout for mobile support
}
i see this code, but i need the complete one because i'm not perfect in coding :(
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
break;
case DisplayMetrics.DENSITY_MEDIUM:
break;
case DisplayMetrics.DENSITY_HIGH:
break;
}
According to the official documentation, you don't need to programmatically decide which layout to use with the respective screen size.
To optimize your application's UI for the different screen sizes and
densities, you can provide alternative resources for any of the
generalized sizes and densities. Typically, you should provide
alternative layouts for some of the different screen sizes and
alternative bitmap images for different screen densities. At runtime,
the system uses the appropriate resources for your application, based
on the generalized size or density of the current device screen.
In other words, if you follow the recommendation stated in the documentation, as I can see that you've done, placing your layout files in their respective resource folder like so:
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
Then the system will decide, which layout to use. No additional code is needed for you to specify it at run time.
If you however would want to make changes depending on your screen resolution, you could get the width and height in pixels using the following code
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Then do something cleaver depending on the width and height variables, e.g. in your case with the S2:
if(width == 480 && height == 800){
//Do work that's related to the S2 screen resolution
}else if(...){
}
If you're asking how to get the density of the device's screen, it took me 3 seconds to find that answer (not including the time to type "android get device density" into my favorite search engine):
getting the screen density programmatically in android?
I saw one android app my galaxy tab(1024*600), it has fixed resolution 800*480.
How to fix my app's resolution like 800*480?
my client wants to fix app resolution..
You can explicitly define your layout width and height in layout xmls to be 800*480 for all the un-supported resolutions like 1024x600.
A list of sample layout folders:
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
In case of 3.2 or later, you can define layout files for tablets as well:
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
I don't recommend fix your width and height to a specific value but since it's client request, I guess you have to take it.
Or you could set target sdk version to 1.5 or 1.6. I haven't really tried it yet, but I read somewhere that doing so limits the application to be displayed in a lower resolution.
I would like to create different layouts for tablets and handsets in Android. Where should I put the layout resources in order to make this differentiation?
I know this is an old question, but for the sake of it...
According documentation, you should create mutiple asset folders like this
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
If you are using Fragment concept in the code(means Multi-Pane layout) then its best to use wdp instead of swdp
res/layout-w600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-w720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
res/layout-w600dp-land/main_activity.xml # For 7” tablets in landscape (600dp wide and bigger)
res/layout-w720dp-land/main_activity.xml # For 10” tablets in landscape (720dp wide and bigger)
Refer the table for understanding wdp
Table 2. New configuration qualifers for screen size (introduced in Android 3.2).
In the following link
http://developer.android.com/guide/practices/screens_support.html
With layouts, I believe you can only current differentiate by the following:
res/layout/my_layout.xml // layout for normal screen size
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-large-land/my_layout.xml // layout for large screen size in landscape mode
You can find more info on what you can add to the folder structure to differentiate between different settings here.
The biggest problem is that the Android SDK hasn't really incorporated tablets officially. Hopefully that will be resolved in the next version of Android. Otherwise, you just need to make sure you use scaling layouts that will work for any screen size.
According documentation, you should create mutiple asset folders like this..full list......
res/layout/main_activity.xml // For handsets (smaller than 600dp available width)
res/layout/main_activity.xml // For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml // For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml // For 10” tablets (720dp wide and bigger)
res/layout-sw600dp-land/main_activity.xml // For 7” tablets in landscape (600dp wide and bigger)
res/layout-sw720dp-land/main_activity.xml // For 10” tablets in landscape (720dp wide and bigger)
"Orientation for preview" dropdown in Android Studio as shown below can help generate quick landscape and tablet layout xmls. It also creates separate folders i.e. layout-land and layout-sw600dp for these layout variations and place the layout xmls within these folders.
This source also providing how to call any resources based on device configurations, like: language, screen width/height, layout direction, screen orientation...etc.
You've to be careful to make a default resource as the source mentioned, like calling high quality of icons for tablets.