handle screen rotation with 3 fragments, every one with 3 different layouts - android

I stuck in a little bit problem. I have situation like this:
Login activity with one layout.
Three Fragment every one with three different layouts, (Phone portrait, landscape, Tablet landscape)
For better image, Fragments function is: login, register and confirmation code.
What i want to achieve is, good screen rotate + after rotate open correct fragment with stored register data.
I'm not sure if it will helpful but i use in project greenrobot event bus.What i need is suggestion how i could implement it correctly. Which is best approach etc.
Thank you for your feedback!

You should use quantifiers to load the great layout depending on your screen size and orientation.
For example, activity_main.xml from layout-large-land and layout folders wont react the same. The first one will only be loaded if you are on a tablet and landscape oriented. The second one will be the default layout.
You will need to use small, large, landscape (land) and portrait (default) quantifiers.
About loading datas, you can use saveInstanceState bundle. Note that views with an id will automatically handle it.

Related

What is the best option to create a dynamic board game on Android?

I want to develop a dynamic matching game for Android. I want the change the board size from 4x4 up to 8x8 (eg. 7x4 and 5x4 would be valid sizes), depending on the level of difficulty chosen. This means I can have a 5x4, 7x4 board, and so on.
I also want to handle orientation change in this game. This means I want to change the board dimensions in the orientation changes (eg. a portrait 5 x 4 can became a 4 x 5 landscape).
I came up with two different approaches.
Create #N different layout files using Constraint Layout. Then I put some logic in my fragment to decide which layout file to pick up. This would handle almost all the complexity of making it dynamic once the constraint layouts will shrink and stretch automatically.
The downsides I realized:
a. code will look messy. I would have to findViewById depending on the layout chosen;
b. configuration change might be an issue, once I'd have to change the layout file on the fly;
c. maintenance would be hard. Imagine maintaining all these XML files.
Use GridView. I can make it all dynamic. The Adapter binds it for me, I just need to tell the GridView how many columns I want.
Downsides:
a. I need to come up with magic math to make it fit;
b. height is an issue and onBind might mess me up is the ViewHolder decides to rebind the views.
At this point, I'm inclined to go for the first option. But I'm worried about making a decision I'd regret after.
Any advice or suggestion?
Thanks in advance,
Daniel

Why fragment adjust better to different screen size as compared to activity?

I found below statements as an advantage of using fragments-
Fragments are used to efficiently use the space in wider screen size.
Fragments are used as means of solving compatibility issues when
targetting an android application for different screen
sizes,especially for both tablets and mobiles.
Does this mean an activity doesn't efficiently use the space in wider screen size and it does not adjust well to different screen size?Why is fragment better than activity in terms of using screen space on different screen sizes?
The basic idea is, that by using Fragments, you can reuse that Fragment in different layouts, for example to put more information on a larger screen.
Most basic example would be a list where a click on an item brings you to a detail screen.
On Phones you might have seperate screens for both fragments, while on a tablet you might as well show them side-by-side.
List Fragment:
Detail Fragment:
Tablet Screen with both Fragments next to eachother:
[Screenshots are taken from a project I made for my Udactity Android Developer Nanodegree]

Is there any way to change the orientation of the layout shown in the second screen when using cwac-presentation library?

I have an app that uses the cwac-presentation library. When starting the service and displaying the layout on the second display, I want it to be landscape oriented but keep the layout on my device on portrait, but I cannot find a way to these different orientations. If I request an orientation change, it happens on both screens (as expected). Has anyone found a solution to this issue?

Which layout should be in landscape in android?

I have downloaded the google io application and I noticed that for landscape layout, its only the dashboard layout that have a landscape layout (in the layout-land folder).
My question is the following, is it for all the layout available in our project that we must create a landscape layout or it is only for the dashboard layout?
Thanks.
Kind Regards.
You are allowed to create a landscape layout for any layout in your app. You should do this when you want to have a different layout in portrait vs. landscape. Sounds to me like the Google IO app specifies a different landscape layout for only one screen (the home screen/dashboard), presumably because that's the only screen the developers felt needed a different landscape layout. Point is, it's up to you which screens have alternate layouts.
It depends on the data you have to show.
Sometimes there is more data which can be shown because of extra width in landscape and you have to compress data vertically in landscape. For such cases use different layouts

Screen Orientation of Android

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".

Categories

Resources