brian
brian

Reputation: 6912

Disable auto change orientation

Below is my code.
I want to set it disable auto change screen orientation.
And enable auto change screen orientation.

class NewLoad extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progDailog = new ProgressDialog(this);
            progDailog.setMessage("test");
            progDailog.setIndeterminate(false);
            progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progDailog.setCancelable(false);
            progDailog.show();
            //set disable auto change
        }

        @Override
        protected String doInBackground(String... aurl) {
            //do something
            return null;
        }

        @Override
        protected void onPostExecute(String unused) {
            super.onPostExecute(unused);
            progDailog.dismiss();
            //set enable auto change
        }
    }

How can I do it?

Upvotes: 3

Views: 1600

Answers (3)

Zartch
Zartch

Reputation: 136

In runtime:

When you want to block sensor:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

When you want to activate:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

http://developer.android.com/reference/android/app/Activity.html

Upvotes: 1

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

disable auto change screen orientation.And enable auto change screen orientation.

if you want to do at run time

try :

  1. Keep a boolean flag as per you need as in http://thinkandroid.wordpress.com/2009/12/27/handling-configuration-changes-or-not/
  2. override the onconfigurationchanged and if it;s true pass the call to super.onconfigurationchanged if false wouldn't pass call to super

Upvotes: 2

Vipul
Vipul

Reputation: 28093

Make your activity to look something like this in manifest

  <activity
                android:name=".Hello"
                android:label="@string/app_name"
                android:screenOrientation="portrait"   <-- Screen will be forced to have portrait
                android:configChanges="orientation|keyboardHidden|screensize" > <-- No Restart in these cases

ScreenSize attribute seems to be added in 4.0 so don't mentioned it if you are ruuning below 4.0.

Upvotes: 7

Related Questions