Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging orientation with onClick android
    primarykey
    data
    text
    <p>I am trying to implement a button click to be able to change the orientation of the activity however, it is going into landscape mode but not going back to portrait.</p> <pre class="lang-java prettyprint-override"><code>if(!state){ setRequestedOrientation(MainActivity.SCREEN_ORIENTATION_LANDSCAPE); } else { setRequestedOrientation(MainActivity.SCREEN_ORIENTATION_PORTRAIT); } state = !state; </code></pre> <p>What I read was to, have state = false, when the button is pressed, change the orientation, then state is set as true, at this point if the button is pressed again the activity will change back to landscape.</p> <p>I also read about using switch? But I tried implementing that but didn't go so well so I tried this method.</p> <p><strong>Edit</strong></p> <p>I've implemented using getting the surface rotation and they are able to pick up the rotation either being 0 or 90 </p> <pre class="lang-java prettyprint-override"><code>if (orientation==Surface.ROTATION_0) { Toast.makeText(getBaseContext(), "going to landscape", Toast.LENGTH_LONG).show(); setRequestedOrientation(MainActivity.SCREEN_ORIENTATION_LANDSCAPE); } else if (orientation==Surface.ROTATION_90) { Toast.makeText(getBaseContext(), "going to portrait", Toast.LENGTH_LONG).show(); setRequestedOrientation(MainActivity.SCREEN_ORIENTATION_PORTRAIT); } </code></pre> <p>However, as the activity goes into landscape and the toast is shown, when I try to go back into portrait the toast does show "going to portrait" but the setRequestOrientation does not execute. What seems to be the issue?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload