1

my program is supposed to run on landscape so in the manifest i used android:screenOrientation="landscape", recently i got a device that should work in portrait, i made a few changes on the code and added a gui landscape and portrait, in my setting page i added a checkbox where it ask if should run in vertical or landscape...

now i have the problem if i remove on the manifest the screenorientation i can't manually force it in the code, i tried to use

if (prefs.getBoolean(Settings.ORIENTATION_KEY, false)) {
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    else
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

but this partially works, the activity is created first on portrait (or the current phone rotation) and then rotated in the right direction, this make the app crash because in portrait i have differents keys and most important thing i see the activity that get launched first in portrait then closed and launched again in landscape...

all this just for ask, is possible to set the orientation before the onstart (like manifest) but with a value from a saved value? i saw that i can use build.gradle for make some kind of var in the manifest, but i don't know how get the value from my settings file (i use getSharedPreferences on code)

Trigun
  • 73
  • 9
  • Have you considered this answers https://stackoverflow.com/questions/25600621/android-gradle-build-how-to-set-global-variables and https://stackoverflow.com/questions/43706401/how-to-use-environment-variables-in-gradle-build-files ? Also please consider to publish two android apks where each of them has screen orientation defined in the manifest. It could be done over gradle flavours. Here is more details on publishing different screens https://developer.android.com/google/play/publishing/multiple-apks – Gleichmut Mar 01 '23 at 06:28
  • Publishing two separate apks for each orientation might be the worst solution I could possibly think of. Why do you have different keys for each orientation? – Rob Mar 01 '23 at 07:12
  • i use a ean code scanner, i bought a phone with the reader integrated, that 1 was with the reader on the top side (and a small keyboard on bottom) so i should use the portrait, on the other devices i use the landscape and in the right side of the screen i add a small keyboard (just the numbers), since on the vertical device i have a physical keyboard i removed it from the layout (and this generate the errors) – Trigun Mar 01 '23 at 13:49
  • 1
    for now i have manually coded the orientation, but would be cool to have the option back :-) – Trigun Mar 01 '23 at 13:51

0 Answers0