Wednesday, October 30, 2013

How to handle orientation changes on Android

Hi there!
Today i'm gonna share a very common task, i used to have while developing android apps. Most of the scenarios we can handle by using the conventions of foldername-port or foldername-land for Portrait and Landscape mode while developing app which supports both orientations. That´s a good practise and should be done whenever possible. But in some special cases, we need to thread the orientation changes by listening to it. If this is also an issue by you, you'll may find helpful to see how we could do it. The code segment bellow shows a class which was designed to be implemented in line in your activity every time you may need the capability of handling orientation changes. Specially when the phones stands or is lying on the side. The instructions on how to use it, is described on the class's javadoc.

The abstract, reusable class definition


import android.view.Surface;

/**
 * Handles screen orientation changes. It was designed to be used/implemented in line,

 * directly in your activity. To be able to work properly, this class needs a manifest entry in the

 * activity tag called:

 * 
 * ... 
   
 * android:screenOrientation="fullSensor"

 * ... 
 * 

 * INSTRUCTIONS ON HOW TO USE IT:

 * 1) Implement the behavior you need in the methods:

 * {@link AbstractRotationHandler#onPhoneStands()} and

 * {@link AbstractRotationHandler#onPhoneLyingOnTheSide()}

 * 2) Then pass the current orientation constant to the method:

 * {@link AbstractRotationHandler#onOrientationChanged(int)} by calling:

 * getWindowManager().getDefaultDisplay().getRotation();

 * from your activity.
 * 
 * @author Ricardo Ferreira 30/10/2013
 * 
 */
public abstract class AbstractRotationHandler {

 /**
  * Pass the current orientation constant by calling:

  * getWindowManager().getDefaultDisplay().getRotation();

  * from your activity.
  * 
  * @param rotation
  *            the current rotation constant provided by your activity
  */
 public void onOrientationChanged(final int rotation) {
  switch (rotation) {
  case Surface.ROTATION_0:
  case Surface.ROTATION_180: {
   onPhoneStands();
   break;
  }
  case Surface.ROTATION_90:
  case Surface.ROTATION_270: {
   onPhoneLyingOnTheSide();
   break;
  }
  }
 }

 /**
  * Implement the behavior you need, when the phone stands.
  */
 protected abstract void onPhoneStands();

 /**
  * Implement the behavior you need, when the phone is lying on the side.
  */
 protected abstract void onPhoneLyingOnTheSide();

}

The implementation of it


 private AbstractRotationHandler concreteRotationHandler = new AbstractRotationHandler() {

        @Override
        protected void onPhoneStands() {
               // do what you need here...
        }

        @Override
        protected void onPhoneLyingOnTheSide() {
               // do what you need here...
         }
    };

The usage of it


int rotation = getWindowManager().getDefaultDisplay().getRotation();
this.concreteRotationHandler.onOrientationChanged(rotation);

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†