How to set orientation fixed for all actions

Android platform. How to set the orientation fixed for all actions in the application AndroidMainfest.xml tag? I do not want to set an orientation for each type of activity individually. Thanks in advance.

+6
source share
5 answers

GoogleIO application has an ActivityHelper class. It has a static initialize() method that handles many things that happen for each Activity. Then this is just 1 line of code in the onCreate() method that you need to remember, which can handle setting this value and several others that are necessary for each type of activity.

Edit: No imports or anything like that. Create a class called ActivityHelper

 public class ActivityHelper { public static void initialize(Activity activity) { //Do all sorts of common task for your activities here including: activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } 

Then, in all your actions, calling the onCreate () method ActivityHelper.initialize() If you plan to also develop tables, you can consider using:

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 

I wrote about it here

Edit: Sorry ... you need to pass the action. see code above

+17
source

The accepted answer and everything that setRequestedOrientation offers are far from perfect, because as stated in the documentation , calling setRequestedOrientation at runtime can cause activity to restart, which, among other things, affects animation between screens.

If possible, it's best to set your desired orientation in AndroidManifest.xml . But since there is an error that tends to rely on each developer to remember to modify the manifest when adding a new action, this can be done during build by editing the AndroidManifest file during build.

There are a few caveats for editing AndroidManifest in such a way that you need to know about this:

My requirement was to update all actions with a fixed orientation, but only in the release of the assembly . I achieved this with a bit of code in build.gradle , which does a simple line replacement in AndroidManifest (provided that none of the above actions are already specified):

An example solution compatible with Android Studio 3.0 (touching only the actions corresponding to com.mycompany.* ):

 android.applicationVariants.all { variant -> variant.outputs.all { output -> if (output.name == "release") { output.processManifest.doLast { String manifestPath = "$manifestOutputDirectory/AndroidManifest.xml" def manifestContent = file(manifestPath).getText('UTF-8') // replacing whitespaces and newlines between `<activity>` and `android:name`, to facilitate the next step manifestContent = manifestContent.replaceAll("<activity\\s+\\R\\s+", "<activity ") // we leverage here that all activities have android:name as the first property in the XML manifestContent = manifestContent.replace( "<activity android:name=\"com.mycompany.", "<activity android:screenOrientation=\"userPortrait\" android:name=\"com.mycompany.") file(manifestPath).write(manifestContent, 'UTF-8') } } } } 

An example solution compatible with Android Studio 2.3 (corresponds to all actions, but does not correspond to <activity-alias> elements):

 android.applicationVariants.all { variant -> variant.outputs.each { output -> if (output.name == "release") { output.processManifest.doLast { def manifestOutFile = output.processManifest.manifestOutputFile def newFileContents = manifestOutFile.getText('UTF-8') .replaceAll(/<activity(?!-)/, "<activity android:screenOrientation=\"userPortrait\" ") manifestOutFile.write(newFileContents, 'UTF-8') } } } } 

I used userPortrait instead of portrait , as I prefer to give the user more flexibility.

The above crashes if you only have options (debug, release). If you have additional tastes, you may need to modify it a bit.

You can remove if (output.name == "release") depending on your needs.

+3
source

If you are writing your project with generics.

And you have something like "BaseActivity", and inside onCreate you can write code like this:

For example: BaseActivity extends AppCompatActivity, later you use YourActivity extends BaseActivity

Portrait

 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

Landscape

 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
+2
source

(Monodroid / C # code)

You can create an abstract base class.

 public abstract class ActBase : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); RequestedOrientation = clsUtilidades.GetScreenOrientation(); } } 

Then all your actions should inherit this class, and Activity

Something like

 [Activity(Label = "Orders", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.Mcc | ConfigChanges.Mnc)] public class ActOrders : ActBase { .... 

This method avoids calling an ActivityHelper in your events.

0
source

I got the best solution. You do not need to pass any actions as a parameter and so on.

Here is what you have to do.

Create a class and expand your application like this. Implement onActivityCreated and onActivityStarted and add code that sets the orientation depending on what you want.

 public class OldApp extends Application { @Override public void onCreate() { super.onCreate(); // register to be informed of activities starting up registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityStarted(Activity activity) { activity.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { } @Override public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { } @Override public void onActivityDestroyed(Activity activity) { } @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { activity.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } }); } } 

After that, add the following to your manifest file inside <application block> :

 android:name=".OldApp" 

The end result will be like this:

 <application android:name=".OldApp" ...other values... > <activity android:name=".SomeActivity"></activity> </application> 
0
source

All Articles