Rotation on Android

TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai

Hi all,

We have gotten a small slew of issues on Android due to rotation. This is primarily caused by us screwing up the templates and forgetting to add a single parameter to the attribute for the main launcher. To fix this open your Activity.cs file in your android project and find the ActivityAttribute. Once you have found it, make sure you pass the following parameter to it:

ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)

This should allow rotation to work as expected in Xamarin.Forms.

Posts

  • StefanXamStorePantkeStefanXamStorePantke DEMember
    edited June 2014

    Thx! As answered at Stackoverflow.

    And import the class like so:

    using Xamarin.Forms.Platform.Android;

  • ChaseFlorellChaseFlorell CAInsider, University mod

    Is this fixed in the latest release? If so, maybe we can unpin this post :)

  • TheRealJasonSmithTheRealJasonSmith USXamarin Team Xamurai

    It is not fixed until the templates get updated. Unfortunately if you have already made your project even if you get new templates it wont fix the issue for you (since template creation is one time). Thus we will leave this posted for quite a while.

  • JonayRguezJonayRguez ESMember ✭✭

    @JasonASmith: Thanks for the fix. Works fine with the latest update (Version: 4.12.5 ).

  • nikolaj.skovnikolaj.skov DKMember ✭✭

    Thanks for this post, @JasonASmith‌. It was just what I needed.

  • FemilShajinFemilShajin USMember

    @jasonASmith Is there any event handler in Xamarin forms to detect rotation??

  • deckertron9000deckertron9000 USUniversity ✭✭✭

    @FemilShajin‌ I don't believe Xamarin.Forms implements the override to detect rotation at this time.

  • EidosMobileEidosMobile ITMember ✭✭

    @FemilShajin‌ you can detect rotation with this simple "trick"

    protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);
    
            if (width < height)
            {
                //Portrait
            }
            else
            {
                //Landscape
            }
        }   
    
  • MihaMarkicMihaMarkic SI ✭✭✭✭

    Here is an another, XAMLish friendly, solution

  • FemilShajinFemilShajin USMember
    edited August 2014

    @Remmo‌ OnSizeAllocated(double width, double height) is a good solution but it's getting invoked at so many occasions like changing the Imagesource for Imageview and also on entering text in entry view. So I think If there is rotation event handler It would make every thing easy.

  • AlecTucker.2208AlecTucker.2208 AUInsider, University, Developer Group Leader mod

    Sounds like Windows Mobile all over again :)

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @JasonASmith‌:

    I have a "Blank App Xamarin.Forms (Shared)" based project.

    Where can I find the Activity.cs file?

    I only see a MainActivity.cs-file (without entry fro ActivityAttribute)

    Thanks

  • deckertron9000deckertron9000 USUniversity ✭✭✭

    @FredyWenger‌
    MainActivity.cs is what he's referring to.

    i.e.

    [Activity(MainLauncher = true, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize)]
    public class MainActivity : AndroidActivity
    
  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @deckertron9000‌ :

    Thanks - I have changed my .cs-file (see below)

    Now I have 2 similar error-messages (translated to English):

    "The name 'ConfigChanges' is not found in the actual context" ...\MainActivity.cs

    What do I have to do further...?

    Thanks

    using System;
    
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    using Xamarin.Forms.Platform.Android;
    //using Shared;
    
    
    namespace MatrixGuide.Droid
    {
    //    [Activity(Label = "MatrixGuide", MainLauncher = true)] Original
        [Activity(Label = "MatrixGuide", MainLauncher = true, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize)] // Angepasst
        public class MainActivity : AndroidActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                Xamarin.Forms.Forms.Init(this, bundle);
    
                SetPage(App.GetMainPage());
            }
        }
    }
    
  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited September 2014

    you're not

    using Android.Content.PM;
    
  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @ChaseFlorell‌ :

    Thanks.. that fix the problem: (why not write the full needed information's from beginning...?)

    So for all other newbies (like me;-):

    I have a Master-Detailpage in my project.

    My problem was, that - if I have changed the orientation of my android-tablet - the Master-Page was reloaded automatically - this problem is fixed with the change. Maybe also other bugs are related to this missing entry.. who knows...



    The file is named "MainActivity.cs" and stored in the android-project

    You have to change the Line to "[Activity..." (add information's) and to add a new using

    See code below:

    using System;
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    using Xamarin.Forms.Platform.Android;
    using Android.Content.PM; // Add this using
    
    
    namespace MatrixGuide.Droid
    {
        //[Activity(Label = "MatrixGuide", MainLauncher = true)] // Original
        [Activity(Label = "MatrixGuide", MainLauncher = true, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize)] // Changed -> fix the promblem: load mainpage after orientation-change
        public class MainActivity : AndroidActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                Xamarin.Forms.Forms.Init(this, bundle);
    
                SetPage(App.GetMainPage());
            }
        }
    }
    
  • ChaseFlorellChaseFlorell CAInsider, University mod

    Thanks.. that fix the problem: (why not write the full needed information's from beginning...?)

    @FredyWenger‌ I did post the needed info from the beginning. You said you had an issue, I posted what you were missing. Take a look at some of the examples to see what the full solution needs to look like... there are lots of working examples in the Xamarin Forms Sampes Repository on Github.

    to name a few.

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @ChaseFlorell‌:

    Please don't understand my comment false - it was not thought for you - I have give you a "Like" already yesterday for your answer :-).

    I have spent some time with the bug and also have filled in a bug in bugzilla at 09-02 with no answer...

    Now I see randomly this posting without clear description, which problems are solved an without clear description, how to solve it.

    So... once again - thank you (you also have helped me to other problems.

  • StefanKoellStefanKoell ATMember ✭✭

    @JasonASmith Could this be broken in the current version?

    I created my project after the templates have been changed, so my MainActivity already contains those parameters. Still when I rotate my device, the OnPause and OnCreate methods are called. My app uses a NavigationPage. The current page is lost and it starts over at the root page again.

    I'm using

    • Xamarin.Forms.dll 1.2.3.6257
    • Xamarin.Android 4.18.0.34
    • Android 4.4.4 on a Google Nexus 10
  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @StefanKoell‌:
    I also have 1.2.3.6257 installed and don't have the problem (after I have made the changed the MainActivity (see above) in a earlier version).

  • StefanKoellStefanKoell ATMember ✭✭

    I wrote it directly to the AndroidManifest.xml

    <activity android:name="..." android:configChanges="orientation|screenSize">
    

    This does the trick.

    I defined the activity there directly before. In http://developer.xamarin.com/guides/android/advanced_topics/working_with_androidmanifest.xml/ stands that the settings from the AndroidManifest.xml and the ActivityAttribute are merged. Seems like the merging works in a way I didn't expect.

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @StefanKoell‌:

    You don't have to expect in XF - you have to try it out....
    Joke;-)

  • JasonASmith: Thanks for the first post with rotation support :)

  • DemiVisionDemiVision USMember ✭✭

    Look likes this broke again with the latest 1.2.3 Beta channel release. Bug'd here:
    https://bugzilla.xamarin.com/show_bug.cgi?id=24422

  • StefanKoellStefanKoell ATMember ✭✭

    @TikiMotel, that's a different problem not related to this thread.

  • DemiVisionDemiVision USMember ✭✭

    OK, sorry for the false alarm.

  • DemiVisionDemiVision USMember ✭✭

    OK, sorry for the false alarm.

Sign In or Register to comment.