Change Screen Orientation Programmatically Using a Button

Change Screen Orientation programmatically using a Button

Yes it is implementable!

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

ActivityInfo

http://developer.android.com/reference/android/content/pm/ActivityInfo.html

Refer the
link:

Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait);
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape);

buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

});

buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

});

http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html

Android change orientation programmatically?

Programmatically you can change orientation by:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

or

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Just put it in a onClick event or whatever you desire.

Change orientation programmatically with button - iOS

I don't have a swift code. Below are the objective c code, It worked for me. You can convert it into swift as per your requirement.

Ojective C

UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];

Update

Swift 4.0

var value  = UIInterfaceOrientation.landscapeRight.rawValue
if UIApplication.shared.statusBarOrientation == .landscapeLeft || UIApplication.shared.statusBarOrientation == .landscapeRight{
value = UIInterfaceOrientation.portrait.rawValue
}

UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()

Above code already tested by me and it is working fine. Incase above code is not worked for you, Then execute it after some delay using performSelector.

How to change the orientation of the screen on button click?

Try this and change as what you need.

- (IBAction)orientation:(id)sender {

if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
NSLog(@"landscape left");

}

if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
NSLog(@"landscape right");

}

if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
NSLog(@"portrait");

}


}

Programmatically change orientation

I think Apurv Jha's answer should work. I also think it's stolen from here: Flutter: How to set and lock screen orientation on-demand.

Have you tried this: you might want to use the code from initState in a different place. For example, in onPressed or onTap parameters for widgets. Then create a state that indicates whether the device was rotated or not. It might be a simple bool or some way to get the device rotation at the moment of button press. Then depending on the state set the orientation to be different from one on the state and update the state.

Set initial screen orientation

you can set the screen orientation programmatically in the onCreate()- method:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

just right before the call of super.onCreate();.

I hope, I could help you.

Best regards,
G_J



Related Topics



Leave a reply



Submit