0

I need to detect screen orientation change on a fragment and to do so, I am currently using this method:

public void onEvent(OrientationEvent event){...}

which works absolutely fine on my Nexus 4. The problem I have is that on a Samsung Galaxy S3, the method is not called when rotating the screen. Anybody has an idea?

Many thanks.

thomaus
  • 6,170
  • 8
  • 45
  • 63

3 Answers3

1

There is a good Google webpage on this @ Handling Runtime Changes. This covers when user changes screen between horizontal/vertical view, and switching applications. Code snippet:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

The code above can be placed in the Activity or the Fragment subclass.

In your manifest xml, you set:

<activity android:name=".MyActivity"
          android:configChanges="orientation">

Please keep us posted, I would like to know of your progress. Someday I may want to do this also. And I would code it like this, if I do

The Original Android
  • 6,147
  • 3
  • 26
  • 31
  • Many thanks for your help. But your solution goes for Activities only I think. I want to detect screen detection on a Fragment. – thomaus Mar 11 '15 at 09:16
  • @thomaus, I tried it and, believe it or not, it works! Remember that a Fragment can easily get the parent Activity by getActivity(). I admit you had me worried, and I thought I had to tell you that you have to add onConfigurationChanged override method in your Main activity and pass the info to the Fragment, which is an option. – The Original Android Mar 11 '15 at 21:49
  • For better clarity, onConfigurationChanged() can be placed in the Fragment subclass too. – The Original Android Mar 12 '15 at 03:52
0
private String getScreenOrientation(){    
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
    return "ORIENTATION_PORTRAIT";
else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
    return "ORIENTATION_LANDSCAPE";
else
    return "";

}

0

You need to override the method onConfigurationChange() in your fragment simplly. You can have look at this developer android post :

public void onConfigurationChanged (Configuration newConfig)

Called by the system when the device configuration changes while your component is running. Note that, unlike activities, other components are never restarted when a configuration changes: they must always deal with the results of the change, such as by re-retrieving resources.

At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.

Parameters

newConfig The new device configuration.

Inside onConfigurationChanged, You need to check the current orientation like :

newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE
newConfig.orientation == Configuration.ORIENTATION_PORTRAIT

and add logic

Community
  • 1
  • 1
Kushal
  • 8,100
  • 9
  • 63
  • 82