I would like tablets to be able to display in portrait and landscape (sw600dp or greater), but phones to be restricted to portrait only. I can't find any way to conditionally choose an orientation. Any suggestions?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Supplement to the accepted answer
You can do the following steps in Android Studio to add the
res/values-sw600dp
andres/values-large
directories with theirbools.xml
files.values-sw600dp
First of all, from the Project tab select the Project (rather than Android) filter in the navigator.
Then right click the
app/src/main/res
directory. Choose New > Android Resource Directory.Select Smallest Screen Width, and then press the >> button.
Type in
600
for the smallest screen width. The directory name will be automatically generated. Say OK.Then right click on the newly created
values-sw600dp
file. Choose New > Values resource file. Typebools
for the name.values-large
Adding a
values-large
directory is only necessary if you are supporting pre Android 3.2 (API level 13). Otherwise you can skip this step. Thevalues-large
directory corresponds tovalues-sw600dp
. (values-xlarge
corresponds tovalues-sw720dp
.)To create the
values-large
directory, follow the same steps as above, but in this case choose Size rather than Smallest Screen Width. Select Large. The directory name will be automatically generated.Right click the directory as before to create the
bools.xml
file.Unfortunately, using the method setRequestedOrientation(...) will cause the activity to restart, so even if you call this in the onCreate method it will go through the activity lifecycle and then it will recreate the same activity in the requested orientation. So at @Brian Christensen's answer you should consider that the activity code might be called twice, this could have bad effects (not only visual, but also at network requests, analytics, etc.).
Furthermore, to set the configChanges attribute in the manifest is in my opinion a big trade-off, which could take massive refactoring cost. Android Devs are not recommending to change that attribute.
Finally, trying to set the screenOrientation somehow different (to avoid the restarting problem) is impossible, statically impossible due to the static manifest which can't be changed, programmatically it is only possible to call that method in the already started activity.
Summary: In my opinion, @Brian Christensen suggestion is the best trade-off, but be aware of the restarting activity issue.
Old question I know. In order to run your app always in portrait mode even when orientation may be or is swapped etc (for example on tablets) I designed this function that is used to set the device in the right orientation without the need to know how the portrait and landscape features are organised on the device.
Works like a charm!
NOTICE: Change
this.activity
by your activity or add it to the main activity and removethis.activity
;-)If you want to do the opposite, you must change the code to landscape (but I think it is clear how to this).
Here's a good way using resources and size qualifiers.
Put this bool resource in res/values as bools.xml or whatever (file names don't matter here):
Put this one in res/values-sw600dp and res/values-xlarge:
See this supplemental answer for help adding these directories and files in Android Studio.
Then, in the onCreate method of your Activities you can do this:
Devices that are more than 600 dp in the smallest width direction, or x-large on pre-Android 3.2 devices (tablets, basically) will behave like normal, based on sensor and user-locked rotation, etc. Everything else (phones, pretty much) will be portrait only.
Here's how I did it (inspired by http://androidblogger.blogspot.com/2011/08/orientation-for-both-phones-and-tablets.html ):
In AndroidManifest.xml , for each activity you want to be able to change between portrait and landscape (make sure you add screenSize - you didn't used to need this!) You don't need to set a screen orientation here. :
Methods to add in each Activity:
and: (if you don't override this method, the app will call onCreate() when changing orientations)
In onCreate() of each Activity :
The only thing I can't seem to figure out is how to to get the app to change layout files when switching from landscape to portrait or vice versa. I assume the answer is doing something similar to what the above link does, but I couldn't get that to work for me - it deleted all my data. But if you have a simple enough app that you have the same layout file for portrait and landscape, this should work.
You can try this way first get the screen size of the device
and then set orientation according to that