Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control landscape and portrait programmatically in android?

Tags:

android

I developed on application using android sdk 4.0 and I install that .apk file in my samsung tab. When I run that application it is working properly. If I change the tab portrait to landscape or in reverse the screen also changed.

But my requirement is irrespective of changing the mode either portrait to landscape to Landscape to portrait, my application should run in portrait mode only.

like image 697
user1632949 Avatar asked Oct 19 '12 06:10

user1632949


2 Answers

You can define mode in AndroidManifest.xml as follows,

android:screenOrientation="portrait" after the android:name=".activityName"

for example like this,

<activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait">

and from activity class you can use following code,

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
like image 40
Lucifer Avatar answered Sep 20 '22 15:09

Lucifer


add android:screenOrientation="portrait" for each activity in your manifest.xml file.

You can do this programmatically too, for all your activities making an AbstractActivity that all your activities extends.:-

public abstract class AbstractActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}
like image 80
Jainendra Avatar answered Sep 19 '22 15:09

Jainendra