I am a newbie to the android world, and as of today I completed my first application.
I test my work on a physical device, and quite recently it has came to my attention that whenever I turn/rotate my device, my application tries to adapt itself to the new resolution 800x400.
Since I have designed the whole app for 400x800 resolution, this change messes up the original design, as well as sending a new call to "onCreate" method of the last activity it was on, before turning/rotating the device.
I would like to learn whether it is possible or not, or which class I should use to stop the adaption to resolution.
This line (in AndroidManifest.xml) will lock the activity in portrait mode:
<activity android:name="MyActivity"
android:screenOrientation="portrait" />
There are things that you can do to make your layouts work well in different orientations and resolutions, such as using dips instead of pixels to measure your views.
When you have time, consider making layout files for landscape mode. Create a "layout-land" directory within your res directory and drop he landscape layout files there, using the exact same file names for their portrait counterparts.
Related
I need to prevent restarting the activity when the user changes the orientation, so I need to set:
android:configChanges="orientation"
But in the document, there this a note on orientation,
Note: If your application targets Android 3.2 (API level 13) or
higher, then you should also declare the "screenSize" configuration,
because it also changes when a device switches between portrait and
landscape orientations.
I'm a little afraid of using with screenSize together because I only need that for orientation.
I'd like to know when the screenSize event will occur?
I set 'orientation|screenSize' together and tested with changing device's font size, but the screenSize is not changed so the activity has been reloaded.
When(in what case) the screenSize event will be called? and is there any side effects if I set the screenSize to configChanges?
I looked at the Android source code for this. The interesting bits are in ActivityRecord method crossesSizeThreshold and getConfigurationChanges. The screenSize event occurs when the dimensions of the screen changes and the change might be "important" for the application. So what does "important" mean:
When a screen resize happens for an app, Android tries to decide if the activity needs to be relaunched. If the size change is small a re-layout of the activity content might be enough, while for a bigger resize a full configuration change needs to occur.
To decide if the configuration change has to occur Android collects the app's size-sensitive resource qualifiers (e.g. layout-h400dp). If the resize crosses a qualifier boundary a configuration change occurs, while if the resize does not cross the boundary the activity is kept and only a re-layout gets executed.
Having said that, I tried to reproduce this behavior. I couldn't come up with a conclusive test because other configChanges events happened during the resize too.
My initial point still stands. It's hard to predict all possible reasons why a configuration change might occur and trying to prevent the recreation of the activity is very difficult to do. Especially with the amount of API versions and device models we have today. A well architected app that separates business logic and state from the UI layer, is still the way to go.
I am making my 1st ever android app using android studio,
Everything works correctly except switching between portrait and landscape layouts.
What code do i need to insert and, where to get my app to see the layouts I have made.
Thanks
Michael
You have to create a new folder called layout-land and place it in there.
Ex:
Your layout's current location is layout/my_layout.xml, you would place the corresponding landscape layout in layout-land/my_layout.xml
Check: http://developer.android.com/guide/practices/screens_support.html under "Using configuration qualifiers"
Similar Question:
How do I specify different layouts for portrait and landscape orientations?
I have a custom view with an onTouch method that launches another activity with its own xml layout. If I run the program on an emulator that has been scaled to watch size (yes only scaled, normal emulator is fine), it creates an instance of the new activity with both the round and rect layout files. Additionally logcat says eglSurfaceAttrib not implemented.
Because this only happens on scaled emulator, is it just a bug with the emulator? I don't have a real android wear watch yet. Or is there a way to package some attribute with the intent that starts the activity to let it know I want one of the layouts?
Edit: It's having this problem on the emulator regardless of size/shape
The best way to target specific screen sizes or aspect ratios is by using qualifiers for alternate resources.
Based on the developer documentation, using the watch qualifier for your resource will probably do the trick. Your directory hierarchy would look something like this:
src/
res/
layout/
(all of your regular layouts)
layout-watch/
(all of your Android Wearable layouts)
I have a regular layout file : Res/layout/test.xml
And another layout for tablets : Res/layout-large-land/test.xml
This line is used in my Activity to declare the layout : setContentView(R.layout.test);
I'm using an Eclipse AVD emulator with the customer resolution of 1024x600 (Which according to the android site is a standard 7-inch tablet size). I've declared in my Manifest <supports-screens> with both normal and large as "true".
My problem is that the layout is always the original one, the different, larger layout never gets shown (even though the conditions would suggest it should?). Essentially the difference is the regular layout is a ListView and the large landscape one is a Table.
I've tried finding examples of how to get this online and in text books, but I can't find any that show what else to do. From what I've read, Android should make a conscious decision to pick the layout-large-land file over the regular layout one?
Thanks in advance for your help.
EDIT: I've been playing around with the layout, I've tried layout-land (which works fine), and changed the resolution of the emulator to be 854x480, still with no luck.
A simple solution might work for you:::
change res/layout-large-land to res/layout-xhdpi.
Which is more appropriate to use in Screen Orientation.
Declaring <activity android:screenOrientation="sensor" />
or
the long process of creating a new folder layout-land & a new layout? and why? I'm using the first
one now and seems it has the same function as the latter. Thanks!
I think the two are different. When we use android:screenOrientation we're telling android what orientation we want to be displayed in. Having two layout's, one for portrait and another for landscape helps us in cases where we want to layout our views such that it takes advantage of the increased width. If you don't think if need this then just use a single layout. Android will use the same for both orientations.
If you want to prevent android from displaying your view in a particular orientation, etc you can use the former.
sensor just returns the orientaton of the device. It does not optimize the layout like using layout-land would.
From http://developer.android.com/guide/topics/manifest/activity-element.html#screen
The orientation is determined by the device orientation sensor. The
orientation of the display depends on how the user is holding the
device; it changes when the user rotates the device. Some devices,
though, will not rotate to all four possible orientations, by default.
To allow all four orientations, use "fullSensor".