Setting the orientation for only 1 fragment in my activity while the rest is in portrait
AndroidAndroid FragmentsOrientationAndroid Problem Overview
My app needs to be in portrait mode so I set it in the manifest by:
android:screenOrientation="portrait"
But I just recently added another fragment (FragA) that just looks and functions 10x better in landscape. Is there something I can put inside of my FragA to just make that fragment in landscape while retaining the rest of the app in portrait or by doing this will I have to add something to my other fragments to keep them retained as portrait?
Android Solutions
Solution 1 - Android
Use the following code line in the fragment where you want a specific (in this case portrait) orientation.
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
If you want to have a orientation in a fragment, that is based on the way the user holds his device, then use the following code line.
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Hope, this will give you the intended solution.
Solution 2 - Android
In each of your fragments, set the requested orientation.
Reference doc: http://developer.android.com/reference/android/content/pm/ActivityInfo.html
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fragment locked in portrait screen orientation
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Fragment locked in landscape screen orientation
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Fragment screen orientation normal both portait and landscape
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
Solution 3 - Android
Orientation attribute is per activity so you can declare the orientation for only the activity that contains the fragment so that it is in landscape and the rest of the activities will remain as they are.
Solution 4 - Android
So I'm dealing with this issue now. We have only portrait mode application (for now). But there is one fragment that needs to be in landscape. We are using single Activity approach so the accepted answer will not help me.
The fastest solution I could think of is this one.
private var swappingOrientation = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if(savedInstanceState == null) {
swappingOrientation = true
activity?.apply {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
}
}
override fun onDestroy() {
super.onDestroy()
if(!swappingOrientation) {
activity?.apply {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
swappingOrientation = false
}
You hold the information if you are swapping orientation or not in swappingOrientation variable. At the beggining when the fragment is created it will change orientation, only when there is no saved state. And orientation is changed back again only when it is not being currently changed.
This is a super quick solution and it can produce screen blinking when you return to previous fragment. I also did not fully test it so it can have other issues, so keep that in mind.
Solution 5 - Android
First Fragment:
@Override
public void onResume() {
super.onResume();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Second Fragment:
@Override
public void onResume() {
super.onResume();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
Solution 6 - Android
This is old question but answering this just incase anyone wanted the solution as the OP needed. The simple way to achieve this is as follows.
public class LandscapeFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@Override
public View onDestroyView() {
super.onDestroyView();
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Then extend
this LandscapeFragment
from any of your fragment to which you want to launch it in landscape mode.
Solution 7 - Android
ok after i almost blew my head off this worked for me with jetpack navigation components fragments
so this is the base fragment class
abstract class BaseFragment : Fragment(){
var rotated = false
fun rotate() {
val currentOrientation = activity?.resources?.configuration?.orientation //this is diffrent from Configuration.ORIENTATION_LANDSCAPE
Log.e("currentOrientation--->",currentOrientation.toString())
if (currentOrientation != null) {
if (currentOrientation != Configuration.ORIENTATION_LANDSCAPE) {
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}else{
rotated=true
}
}else{
//impossible
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
}
override fun onDestroyView() {
super.onDestroyView()
if (rotated)
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
}
and i put this in any fragment i wish it to be landscaped
override fun onStart() {
super.onStart()
rotate()
}
Solution 8 - Android
In your fragment FragA :
@Override
public void onResume() {
super.onResume();
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@Override
public void onPause() {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onPause();
}
onPause method code ensure your activity back to portrait orientation