1

Is there a way to place values which defined in the Manifest such as Activity screenOrientation in a resource xml file?

For example, Activity screenOrientation attribute can have values such as "portrait", "landscape" etc'

Well, these values are constant once defined. If I want to have different values for phones and tablets, like "portrait" for phones, while on tables I want it to be "sensor" how can this be done? I don't want to set it in code.

At the end, these values are integer constants, and can be found here: https://developer.android.com/reference/android/content/pm/ActivityInfo

Therefore, I thought maybe I can define two files, integers.xml for phones and integers-w400dp.xml for large screens, then define an attribute like

<integer name="orientation">portrait</integer> //for phones in the integers.xml
<integer name="orientation">sensor</integer> //for tablets in the integers-w400dp.xml

Then in the manifest i can refer to them do like this:

android:screenOrientation="@integer/orientation" //This actually accepted with no error

However, the integer xml wouldn't accept "portrait" or "sensor" values and instead I must put their numeric values like this:

<integer name="orientation">1</integer> //For portrait
<integer name="orientation">4</integer> //For sensors 

This I believe will work but I don't want to use hard coded values as they might change between api's level.

I've tried to add xmlns to the resource file (the same ns as in the manifest) but it didn't worked.

* Edited * At the bottom line, this question is about how to lock Activity to portrait on phones, while NOT to lock it on Tablets, but doing this from xml and not in code.

Any idea will be appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Moti Bartov
  • 3,454
  • 33
  • 42
  • please check this solution, it might helpful for you https://stackoverflow.com/a/14669511/3946958 – Ravindra Kushwaha Dec 26 '18 at 07:42
  • 2
    Possible duplicate of [How to use onConfigurationChanged() and newConfig.orientation == Configuration.ORIENTATION\_LANDSCAPE in android 2.3.3](https://stackoverflow.com/questions/9566633/how-to-use-onconfigurationchanged-and-newconfig-orientation-configuration-o) – Martin Zeitler Dec 26 '18 at 07:47
  • I don't think it answers my question, I've put some explanation at the end of my question. – Moti Bartov Dec 26 '18 at 09:02
  • https://stackoverflow.com/questions/9627774/android-allow-portrait-and-landscape-for-tablets-but-force-portrait-on-phone – Martin Zeitler Dec 26 '18 at 11:08

1 Answers1

0

This may help you, check the device type and set activity orientation at run time

if(isTablet())
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
else
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

public boolean isTablet() {
    return (this.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
jessica
  • 1,700
  • 1
  • 11
  • 17
  • This solution is ok and I know I can do this in code. The problem with this approach is, on a phone, if you start the app when the phone is in landscape, what will happen is that you will see the app opens in landscape and then rotates to portrait because of this code. – Moti Bartov Dec 26 '18 at 14:32
  • you can add if statement in onCreate method of activity. – jessica Dec 27 '18 at 06:14