Handling Orientation Changes in Android

Handling orientation changes in Android is the first common problem to deal with.
Proper handling of orientation changes makes rich user experience (not lost UI state) for the application and it also avoiding memory leaks.

There are a couple of ways to handle the orientation changes in Android. In this post, we will focus on 3 methods:

1) Lock screen orientation
To lock the screen orientation change of any screen (activity) of your android application makes your activity display only in one mode i.e. either Landscape or Portrait. This is the simplest way to handle screen orientation but not generally recommended unless you want your activity to be displayed with a unique view.

2) Handle layout change manually ( Setting different layout for each mode )
Which means you will create 2 different layouts: Portrait and Landscape. Then, you will assign the Portrait one with the Portrait orientation while the Landscape one when the device is rotated. This is probably the most effective way since you can freely decide what your layout gonna be when you rotate the phone. 

This one contains very detail tutorials on 2 methods above:

Handling screen layout changes in Android - CodeProject

How to handle screen orientation changes programmatically in Android.; Author: Ali Chousein; Updated: 5 Sep 2012; Section: Android; Chapter: Mobile Development; Updated: 5 Sep 2012

3) Use FlexboxLayout
FlexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android.
FlexboxLayout extends the ViewGroup like android relative layout and linear layout. You can add different attributes to the flexbox layout like flexDirection, flexWrap, justifyContent, alignItems and alignContent. There are other many attributes that you can add to your child view of a FlexboxLayout.

This is a new layout which was introduced at Google I/O Conference 2017. This layout was mentioned before in my previous post Exploring New Android Layouts

Flexbox can be used to build Responsive Layouts. Which means, if your phone is rotated into landscape mode, then the dialogue changes. The editor tags are aligned differently compared to the portrait mode, but it still fits the screen nicely.

You can find this library documentation on Github:

google/flexbox-layout

flexbox-layout - Flexbox for Android 

This layout seems very interesting. So, let 's try it out!

1) Add the following dependency to your build.gradle file (make sure it 's in the module: app)

Then, Android Studio will ask to sync the project with new settings. Click "Sync now" to apply. When the sync operation complete, you can use Flexbox Layout in your android app now.

2) Edit your XML file


You can use my code for reference:



3) Run the app on Genymotion, here is the result:
PORTRAIT MODE

LANDSCAPE MODE

Looking good and Deadpool is cute either, right? However, the biggest weakness here is the automation. When the screen is rotated, all your components will be restructured automatically to fit your screen in the most appropriate way. Assume the screen is now more complicated, you won't see your desired layout anymore because everything has been automated. But in most cases, it 's still beautiful. So ...



Finally, here are some other study resources with more advanced tutorials that I'm referring to:


No comments

Hey, buddy. What's on your mind :)

Powered by Blogger.