Is There A GridLayout SizeChanged Event In Xamarin.Android?

Nathan Sokalski 4,126 Reputation points
2021-07-12T20:07:16.363+00:00

I would like to rearrange a set of buttons when the screen size changes. Is there an event similar to SizeChanged in Xamarin.Android for a GridLayout (because GridLayout is the root of my Activity)? I would also be able to use an event that detects when the screen size changes instead. I have found the LayoutChange event, but I am worried that if I use this I will end up in some kind infinite loop due to the fact that I will be changing the layout during the event. Any ideas? Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2021-07-13T04:12:58.05+00:00

    Hello @Nathan Sokalski ,​

    Welcome to our Microsoft Q&A platform!

    In Android, the View class provides the OnSizeChanged event which will be called when the size of this view has changed. Try creating a custom GridLayout to rewrite the event. To define the function in the Activity class, you could use delegate to pass the event.

    Check the code:

       public class MainActivity : AppCompatActivity  
       {  
           protected override void OnCreate(Bundle savedInstanceState)  
           {  
               base.OnCreate(savedInstanceState);  
               Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
               SetContentView(Resource.Layout.activity_main);  
         
               CustomLayout layout = FindViewById<CustomLayout>(Resource.Id.parent);  
               layout.TheChangeEvent += Layout_TheChangeEvent;  
           }  
         
           private void Layout_TheChangeEvent()  
           {  
               //perform the work  
           }  
       }  
    

    Csutom control:

       public delegate void SizeChangedEventHanlder();  
       namespace App19_7  
       {  
           public class CustomLayout : GridLayout  
           {  
               public CustomLayout(Context context) : base(context)  
               {  
               }  
         
               public CustomLayout(Context context, IAttributeSet attrs) : base(context, attrs)  
               {  
               }  
         
               public CustomLayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)  
               {  
               }  
         
               public CustomLayout(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)  
               {  
               }  
         
               protected CustomLayout(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)  
               {  
               }  
         
               public event SizeChangedEventHanlder TheChangeEvent;  
         
               protected override void OnSizeChanged(int w, int h, int oldw, int oldh)  
               {  
                   base.OnSizeChanged(w, h, oldw, oldh);  
         
                   TheChangeEvent.Invoke();  
               }  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.