Android: How to set different layout for different orientation? - android

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.

Related

How does the OS detect the screen size?

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

Can anyone explain me how to make android app which can fit on most of devices screens?I did not understand any thing from documentation

When I try to make some android app then it seems very good for some devices but on some other devices(mostly which have a lower android API version) it does not look so much better. Sometimes some view (like button)goes out of the screen size.
I have read the documentation provided by Google.But I did not understand anything.As I am a new learner so it is creating many problems for me. If anyone has some time then please help me to understand this thing in the easiest way.
Thanks in advance.
most of the time i uses ssp and sdp. The ssp a scalable size unit for texts is android SDK that provides a new size unit - ssp (scalable sp). This size unit scales with the screen size based on the sp size unit (for texts). It can help Android developers with supporting multiple screens.and sdp This is the sibling of the sdp size unit that should be used for non text views.
[https://github.com/intuit/ssp][1] and [https://github.com/intuit/sdp][1]
use gradle and compile libraries like below
compile 'com.intuit.sdp:sdp-android:1.0.4'
compile 'com.intuit.ssp:ssp-android:1.0.4'
in your layout activity xml for example for text you can provide like this
android:textSize="#dimen/_16ssp"
for layouts
android:layout_height="#dimen/_16sdp"
this will take care for any screens enjoy.
Please go through this https://developer.android.com/guide/practices/screens_support.html
For layouts define additional smallest width layouts:
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 # layout for landscape mode.
For example, see below pic

Font size changing with orientation

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

different layouts for different screen size

I finished to develop (application part) the app for my project, but now I have problems with the layout. I created the following folders and edited the xml file:
layout (default)
layout-large
layout-normal
layout-small
layout-xlarge
On my smartphone (Galaxy S Advance - screen 800x480), the layout is ok, colleague's smartphone (HTC MINI ONE - screen 720x1280) the app haven't got the appropriate layout. In both cases (my smartphone and colleague's smartphone), android loads layout-normal.
What's wrong ???
Thank you
Please mind screen densities for layouts. Even though you put layout files in appropriate layout containers it does not mean in anyway that Android will respect the folders as densities too play an important role in the way how Android selects the correct layout.xml file.
Any device less than 5 inches will fall under sw320dp category your solution would be then to create separate folders named layout-sw320dp-ldpi, layout-sw320dp-mdpi, layout-sw320dp-hdpi, layout-sw320dp-xhdpi and layout-sw320dp-xxhdpi and put the corresponding xml files in them.
Similarly you can approach other screen sizes like layout-sw600dp and layout-sw720dp etc with above screen densities.
You can create different layouts for different Screens width instead of just create small and large dpi folders, in this method you will just have one drawable folder contains all of your resources and multiple layout folders, the main one called layout and contains the default layouts in order to run the specified layout in case it didn't find it in another layout folder, and the folders hierarchy as described in android developers page will be like :
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)
and for example if you want a customized layouts to 340dp screen width you will create layout-sw340dp folder inside res folder, So it will be like
res/layout-sw340dp/main_activity.xml
and the program will automatically assign the layout to the appropriate folder according to the device width.
As you provide layout-large layout-normal layout-small layout-xlarge 4 layout categories. The layout folder(without any qualifier) will be useless. All devices will fall into the other 4 screen buckets.
From your description, I guess you should remove layout-normal folder from your resource.
In addition, normal, large and the other two are just "ranges". If you want more accurate control over screen sizes, you can use layout-sw
Please Remove loyout folder for Your app Besouse The layout folder will be useless
Galaxy S Advance - screen 800x480 layout-hdpi
For devices having 720 * 1280 resolution, you can use layout-w720-h1280 folder

Supporting Multiple Screens with size qualifier

We would like to design layouts for specific resolutions as below:
Samsung Duos: 480 X 800 Android Version 4.0.4
HTC Velocity: 540 X 960
To do so we have created layout folders for each as below:
layout-w480dp-h800dp
layout-w540dp-h960dp
Assuming that if we run application on samsung duaos it should pick the layouts from
layout-w480dp-h800dp folder but it is giving error and unable to start activity.
The error is:
android.content.res.Resources$NotFoundException: Resource ID #0x7f030029
It means it could not locate the layout required by the activity. Same happen with HTC Velocity.
Are we doing something wrong?
Please let us know if you have any idea.
must have been a problem with some resource try to delete a image file from drawable and add it again build the project it should run
Did you try to Clean and Build? It usually help as looks like R generation was corrupted
Your assumption is wrong.
layout-w480dp-h800dp isn't for 480x800 px screen.
DP is way Different than PX. Please read this http://developer.android.com/guide/practices/screens_support.html.
Are you sure you really need different designs for so similar handsets? I would say this is pointless.
In my opinion at most U should distinguish 3 types of devices.
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 still want to do what you are trying to do you can try px instead of dp. But Im not sure if this will work ...
Do you have a default layout that relate to your activity?I mean each folder containing the layout of the same name.

Categories

Resources