0

I want to alter something, for example a variable value, when the user rotates the screen.

I know I can have the orientation using something like:

this

but I don't want to define if portrait do this, if landscape do this.

I want to just catch the current orientation and start doing the things I want.

George
  • 5,808
  • 15
  • 83
  • 160

1 Answers1

0

You have to Override the onConfigurationChanged method in your activity and check the current orientation and do whatever you want like this

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

    // Check orientation
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            DoSomethingForLandscapeOrientation();();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
           DoSomethingForPortraitOrientation(); 
    }
}


private void DoSomethingForLandscapeOrientation(){
    Log.i("Method For","Landscape");
}

private void DoSomethingForPortraitOrientation(){
    Log.i("Method For","Portrait");
}
Muazzam A.
  • 647
  • 5
  • 17