How to maintain screen orientation?

Asked

Viewed 740 times

2

I would like to know the equivalent code in Java for this command:

android:configChanges="orientation"

Justification: I’m creating an app with Layout NavigationDrawer, and need that on certain screens (or all) keep settings when the user rotates the device.

  • 2

    Have you tried putting it in the manifest.xml? There you can inform the possible orientations of Activity, then just leave a fixed if you do not want to change the orientation of the device.

  • I already put in... Because like, this command (as far as I know) should be placed in each Activity, as we only have one in the case of Navigationdrawer, I ended up putting and did not work!

  • As far as I know this does not inhibit the change of orientation. Is this what you want? The title doesn’t quite match the question... If it’s just the question of configChanges programming, there is no solution.

  • Do you want to block the rotation on your screen? If that’s it, I can tell you how

  • 2

    @Wakim actually I changed the title according to what I understood of the problem, I even researched what was making the code in question but it was not 100% clear to me. The previous title was Código equivalente em Java, which means nothing at all. If you understand the problem and have a better title to suggest can edit the question to correct, just tried to give my contribution (which now I was in doubt whether it was good or bad, rs).

  • Hello, actually the title was exactly what I wanted, IE, wanted another way to block the screen by java command instead of xml on Androidmanifest.

  • And I already get it, I was actually putting in Manifest only orientation in configChanges, and I actually have to take into account that the screen size will also modify. Thus, it follows the solution in xml itself: android:configChanges="keyboardHidden|orientation|screenSize"

  • @Math the equivalent code would be this one: public void setUserVisibleHint(Boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); Log. v("Passed", "1"); if(isVisibleToUser) { Activity a = getActivity(); if(a != null) a.setRequestedOrientation(Activityinfo.SCREEN_ORIENTATION_PORTRAIT); } }

  • 1

    You can use a Java command as well: int currentOrientation = getResources().getConfiguration().orientation; 
if (currentOrientation == getResources().getConfiguration().ORIENTATION_LANDSCAPE)
 setRequestedOrientation(6); 
else 
 setRequestedOrientation(7);

  • 1

    @Taironedias like you said, you will be forcing guidance always PORTRAIT, already the solution I passed you only blocks the turn, I mean, the user can use in LANDSCAPE also however will not be able to change once entered the screen.

Show 5 more comments

1 answer

2


Browser other questions tagged

You are not signed in. Login or sign up in order to post.