Change screen orientation programatically in android

In android there are different orientations for screen. While rotating the phone android has a default property of changing the orientation from portrait to landscape or vice versa. In this tutorial we will learn how to change the orientation on  a button click without rotation the phone.

It can be simply achieved by making use of setRequestedOrientation method in android. In this example we will have a button and while clicking on button it will change to landscape if it is in portrait mode and vice versa if it is in landscape mode.

To start , just add the below code to your layout for adding a button to your screen.


Now use the below code in your activity

Button btn_change_orientation = (Button) findViewById(;

btn_change_orientation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

int orientation = getResources().getConfiguration().orientation;
if(orientation == Configuration.ORIENTATION_PORTRAIT){






About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *