Search code examples
c#androidxamarin.androiddrawerlayout

Xamarin.Android : Gravity parameter in DrawerLayout.LayoutParams


I'm trying to create a DrawerLayout programmatically using Xamarin.Android, but I faced a problem when trying to add the ListView which should be dragable form the left ..

Here is my code :

DrawerLayout myDrawerLayout = new DrawerLayout(this);
myDrawerLayout.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
SetContentView(myDrawerLayout);


FrameLayout myFrameLayout = new FrameLayout(this);
myFrameLayout.LayoutParameters = new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
myFrameLayout.SetBackgroundColor(Android.Graphics.Color.Gray);
myDrawerLayout.AddView(myFrameLayout);


ListView myListView = new ListView(this);
myListView.SetBackgroundColor(Android.Graphics.Color.Green);
myListView.LayoutParameters = new DrawerLayout.LayoutParams( width: 240,
                                                  height: DrawerLayout.LayoutParams.MatchParent,
                                                  gravity: ????????
                                                           );
myDrawerLayout.AddView(myListView);

As you can notice.. I didn't know what to pass as a gravity parameter

The definition of the function in DrawerLayout class is like that :

public LayoutParams(int width, int height, int gravity);

So I have to pass int for the gravity, but how ??

I've tried the following :

myListView.LayoutParameters = new DrawerLayout.LayoutParams( width: 240, height: DrawerLayout.LayoutParams.MatchParent, gravity: Gravity.LEFT);

It gives me error:

'Gravity' does not contain a definition for 'LEFT'

Also tried :

myListView.LayoutParameters = new DrawerLayout.LayoutParams( width: 240, height: DrawerLayout.LayoutParams.MatchParent, gravity: GravityFlags.Left);

but gives me an error:

cannot convert from 'Android.Views.GravityFlags' to 'int'

Hope you have an idea about the solution .. and thanks in advance


Solution

  • You have to cast the flag to int like

    new DrawerLayout.LayoutParams(240, DrawerLayout.LayoutParams.WrapContent, (int)(GravityFlags.Start | GravityFlags.Left)).