user1424489
user1424489

Reputation: 63

Landscape and portrait android app

I have created two folders layout and layout-land with two xml files, one for portrait and the other for landscape. Both of the xmls work but here is the problem.

My first screen is a login screen and my second screen is a main screen. If I login in portrait and then turn my phone landscape at the main screen. The layout will landscape turn but it uses the portrait xml for the main screen.

The same error occurs if I start in landscape and try to move to portrait later on.

It seems like whatever layout I do for my main then that's the layout that will be used for the rest of the app. Is there anyway to go around this?

Also. I'm already using android:configChanges="orientation" in my manifest for the activities.

Upvotes: 1

Views: 3515

Answers (3)

Shubham AgaRwal
Shubham AgaRwal

Reputation: 4815

Make sure both xml files present in two different folders have same name.

Upvotes: 0

Alex Lockwood
Alex Lockwood

Reputation: 83303

If you are using android:configChanges="orientation", then you can override onConfigurationChanged to inflate the new layout after a configuration change.

@Override
protected void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);                
    setContentView(...);
}

Make sure you have a good reason for preventing the Activity from being recreated on an orientation change... and most importantly don't just do it because orientation changes are crashing your app. Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.

Upvotes: 2

Geobits
Geobits

Reputation: 22342

Using android:configChanges="orientation" means you will be handling the orientation change in code. If you want it to automatically switch layouts, you shouldn't have that there.

Upvotes: 2

Related Questions