Android Layout. How can I set Orientation Fixed for all activities in application Tag of AndroidMainfest.xml ? I don't want to set orientation for each activity individually. Thanks in advance.
相关问题
- 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
I got the best solution. You don't have to pass any activity as parameter and stuff.
Here's what you have to do.
Create a class and extend your application like this. Implement onActivityCreated and onActivityStarted and add the code that sets the orientation to whichever you want.
After this, add the following in your Manifest file inside the
<application block>
:End result will be like this:
(Monodroid/C# code)
You can create an abstract base class
Then all your activities must inherit this class instead Activity
Somehting like
This way avoids call the ActivityHelper in your events
The GoogleIO app has a ActivityHelper class. It has a static method called
initialize()
which handles a lot things that happen for every Activity. Then it is just 1 line of code in theonCreate()
method that you need to remember, that could handle setting that value and several others that are necessary for each activity.Edit: No importing or anything like that. Create a class called ActivityHelper
Then in all of your activies onCreate() method call
ActivityHelper.initialize()
If you are planning on developing for tables as well you may want to consider using:I wrote more about this here
Edit: Sorry... you need to pass the the Activity. see the code above
If you write your project with generics.
And you have something like "BaseActivity" than inside onCreate it you can write code like that:
For Example: BaseActivity extends AppCompatActivity, later you use YourActivity extends BaseActivity
Portrait
Landscape
The accepted answer, and anything suggesting
setRequestedOrientation
, is far from perfect, because, as stated in documentation, callingsetRequestedOrientation
at runtime may cause the activity to be restarted, which among other things, affects animations between the screens.If possible, the best is to set the desired orientation in
AndroidManifest.xml
. But since it's error prone to rely on each developer to remember to modify the manifest when adding a new activity, it can be done at build time, by editing AndroidManifest file during the build.There are some caveats to editing AndroidManifest this way that you need to be aware of though:
<activity-alias>
entries in the output manifest, you should match<activity(?!-)
instead of<activity
to avoid modifying those (and usereplaceAll
, which matches regex, instead ofreplace
, which matches string)My requirement was to update all activities to have fixed orientation, but only in release builds. I achieved it with a bit of code in
build.gradle
which does simple string replacement in AndroidManifest (assuming that none of the activities has orientation specified already):Android Studio 3.0 compatible solution example (touching only activities that match
com.mycompany.*
):Android Studio 2.3 compatible solution example (matching all activities, but not matching
<activity-alias>
entries):I used
userPortrait
instead ofportrait
as I prefer to give the user more flexibility.The above works out of the box if you just have variants (debug, release). If you additionally have flavors, you might need to tweak it a bit.
You might want to remove
if (output.name == "release")
depending on your needs.