Home » Android » Orientation Changed Event or Listener Android

Orientation Changed Event or Listener Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

Is there any event or listener which fires @ time of orientation changed?

or,
how to find out that the orientation of phone is changed?

Thank You

How to&Answers:

You need to read up on Handling Runtime Changes.

It explains the old way of doing handling stuff like orientation change.

Since Fragments was introduced, they have implemented a new way of doing it (similar to the old one) – but if you don’t use Fragments it makes no sense using it.

Answer:

As far as I know there is a listener for this,

Check out http://developer.android.com/reference/android/view/OrientationListener.html

That is the class have a look through there.

Hope that helped.

Answer:

You can use OrientationEventListener class instead !!
http://developer.android.com/reference/android/view/OrientationEventListener.html

Answer:

If you are more interested in when an orientation change triggers a redisplay of the layout, you might want to check out this: http://developer.android.com/reference/android/view/View.OnLayoutChangeListener.html

“Interface definition for a callback to be invoked when the layout bounds of a view changes due to layout processing.”

Answer:

Since 180° rotation are not detected by onConfigurationChanged,

See better answer here:
How do I detect screen rotation

Answer:

On some phones orentation change event fires before actual resize event. Do not recommend u to use orientation change event when u need to know when form is resized.

Just use $(window).resize(function(){ stuff to do })

If u need to have dynamic resize event and change it at some point u can use structure like that

function ResizeEventHandler (){
  var resizeEvent = function(){}

  this.ResizeEvent = function(value){
        if (value!=null) resizeEvent = value;               
        else return resizeEvent()
  }
}

var ResizeEventHandlerInstance = new ResizeEventHandler();

$(window).resize(function(){ ResizeEventHandlerInstance.ResizeEvent();   });

function YourDinamicalEventSetterFunction(){
    ResizeEventHandlerInstance.ResizeEvent(function(){
               alert('resized');
    });
}

basically when u need to change resize event -> change ResizeEventHandlerInstance… and you will not need to unbind event all the time when u want to change it