3

I am developing an Android application that contains a chat space and I want to made the chat layout unable to be in landscape orientation if the the device has small or normal size (other layouts must have the two options, portrait and landscape, for every device, but my chat doesn't fit in small or normal devices with landscape orientation). My resources distribution is this:

res

  • layout
    • chat fragment
    • other fragments...
  • values
    • chat_dimens
      • chat_dimens
      • chat_dimens (large)
      • chat_dimens (large-land)
      • chat_dimens (small)
      • chat_dimens (xlarge)
      • chat_dimens (xlarge-land)
    • other fragment dimens...

Can anyone help me? Thanks

2 Answers2

4

use this:-

View rootView = inflater.inflate(R.layout.activityxml, container, false);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Nikul Rao
  • 83
  • 8
  • Thanks for the response! It will block the landscape for the large and xlarge screens? I don't want it. – Maurici Guell Hernandez Nov 19 '15 at 11:35
  • Ok, i was not understanding your response but now I do it. I'm using your code line in the fragment and ActivityInfo.SCREEN_ORIENTATION_UNESPECIFIED for the activities that need the two options. It work. Thank u!!!!!! – Maurici Guell Hernandez Nov 19 '15 at 11:54
1

Take a look at this answer. It seems to be possible using these lines:

In the portrait only fragments:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

in the the portrait/landscape fragment:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser) {
        Activity a = getActivity();
        if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
    }
}
Community
  • 1
  • 1
Chris
  • 3,329
  • 1
  • 30
  • 44