views:

143

answers:

1

I have an app which displays a large amount of text for the user to read.

I've found that when reading while lying down, I get annoyed that the screen rotates even though my head and the screen are aligned.

I do not want to set this to be permanently in portrait mode, so I think this would preclude an approach of setting the

 android:screenOrientation="portrait"

in the manifest.

Ideally, I would like to enable/disable automatic orientation changes via a preference page.

What would be my best approach?

+2  A: 

I would do this by having my preference page write to the SharedPreferences for my application, then reading this value and calling Activity.setRequestedOrientation() from the code when the text view in question is loaded or displayed.

For example:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (prefs.getBoolean("fix_orientation_to_portrait", false)) {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }

This can be in either the onCreate() or onResume() method. The only reason not to have it in the onCreate() method is if the changing of the setting happens in this activity, or on a child activity that will come back to this one when it is done.

Edit:

OK, if that doesn't work (and I am not really sure it will), maybe you could try having two different layout xml files - one with the screenOrientation set, and the other without. When you load your Activity/View, you can dynamically load one or the other based on your saved preferences. It's nasty not clean, but it should work.

iandisme
I'm sure you tried it, but out of curiosity - why does it not work? I would have expected that calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) is effectively the same as adding the line you mentioned in your question. (And there's SCREEN_ORIENTATION_NOSENSOR).
EboMike
I didn't try it - it's a big PITA to set up a new project and do this stuff... I'm just doing some end-of-day StackOverflow whoring. I'm going on jamesh's word that it didn't work, but now that you mention it, he was speaking in the hypothetical. Editing accordingly...
iandisme
Your first approach worked. I've added some code to illustrate. Thanks
jamesh