Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to create DrawerLayout and NavigationView programmatically


I have a base activity that implements a navigation drawer. I create this layout programmatically. The issue i am having is that when i run the application the NavigationView covers the entire screen. I know the issue is the NavigationView because when i remove it the app is displayed correctly.

The function below sets up the views for my NavigationDrawer.

`private View SetupNavigationView()
            fullLayout = new DrawerLayout(this);
            fullLayout.LayoutParameters = new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.MatchParent, DrawerLayout.LayoutParams.MatchParent);

            LinearLayout relLayout = new LinearLayout(this);
            relLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
            relLayout.Orientation = Orientation.Vertical;
            relLayout.WeightSum = 100;

            toolbar = new SupportToolbar(this);
            LinearLayout.LayoutParams tParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent,0, 10);
            toolbar.LayoutParameters = tParams;

            activityContainer = new FrameLayout(this);

            activityContainer.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent,0,90);


            navigationView = new NavigationView(this);
            DrawerLayout.LayoutParams navLayout = new DrawerLayout.LayoutParams(DrawerLayout.LayoutParams.WrapContent,                                      DrawerLayout.LayoutParams.MatchParent);
            navigationView.LayoutParameters = navLayout;
            //navLayout.Gravity = GravityFlags.Start; 
    //Had to comment this out as DrawerLayout.LayoutParams does not implement Layout Gravity.

            return fullLayout;

I am unable to set the Layout Gravity as i am using the DrawerLayout.LayoutParams, i am not sure if this is the correct way or not.

I have added this to the base application so each activity that inherits from the base activity can use the navigation drawer, i understand the other approach is to use fragments but each activity in the app is already broken up into multiple fragments.

Thank you for any help you can give me with this issue.

Best Answer


Sign In or Register to comment.