Waddas
Waddas

Reputation: 1402

Lock screen orientation within View class - Android

I know how to do this within the activity class, however this doesn't fit my need. Within my class that extends View, once a method is called i would like the screen orientation to be locked. And then once another method is called i would like it to be unlocked.

Is there a way i can do this within my class that extends View?

Thanks.

Upvotes: 0

Views: 493

Answers (3)

TopCaver
TopCaver

Reputation: 369

Maybe I've got a method, just write down that:

set layoutParams.screenOrientation value into: ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED or ActivityInfo.SCREEN_ORIENTATION_LOCKED

Then, call addView or updateViewLayout to update this LayoutParams.

Upvotes: 0

Waddas
Waddas

Reputation: 1402

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);

    setContentView(this.gameView);

    if (this.gameView.isOrientationChange() == false) {
        // Stop the screen orientation changing during an event
        switch (this.getResources().getConfiguration().orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
        }
    }
    else {
        // allow screen rotations
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
}

In the class extending View, there is an orientationChange boolean field. this is changed by method indicating whether the devices orientation can be changed. This worked but the screen size also changes when the orientation is changed. So if there is a lot of code dependent on the size and is timed, then this may cause more problems than solve.

Upvotes: 1

TheBeps
TheBeps

Reputation: 36

i think what you are looking for, is configChanges see doc -> http://developer.android.com/reference/android/R.attr.html#configChanges

Upvotes: 0

Related Questions