Set the drawer width dynamically

In the Xamarin.Forms Navigation Drawer samples, the drawer width is set dynamically based on the screen width:

 defaultDrawer.DrawerWidth = (float)(Core.SampleBrowser.ScreenWidth * 0.8);

But the way ScreenWidth is determined is hidden inside the Core.SampleBrowser library.

How can I set the drawer width according to the screen size? I can only get the screen width in pixels, but the DrawerWidth requires a float.

Thanks

Paul

3 Replies

NI Nick February 6, 2019 11:54 AM UTC

I got it working like this, in my case I just wanted the drawer to be 40 pixels less than the width of the screen.

   var metrics = DeviceDisplay.MainDisplayInfo;

    navigationDrawer.DrawerWidth = System.Convert.ToSingle(metrics.Width) / (System.Convert.ToSingle(metrics.Density)) - 40f;




PA Paul February 6, 2019 05:35 PM UTC

Thanks for that Nick, that's interesting. 

Clearly Syncfusion has done it in a cross-platform way (although I haven't tried the Drawer in iOS, so I'm making assumptions there). It's a pity they don't reveal the method, as I've searched for a solution everywhere.

But your solution might be the answer I'm looking for - I'll give it a try.

Thanks again


MS Mugundhan Saravanan Syncfusion Team February 9, 2019 09:54 AM UTC

Hi Paul,

Greetings from Syncfusion.

We can take the overall screen width and height of the device, this has been explained on the below stackoverflow link.

https://stackoverflow.com/questions/38891654/get-current-screen-width-in-xamarin-forms

On our SfNavigationDrawer we need to set the width based on your requirement as mentioned below.

defaultDrawer.DrawerWidth = App.ScreenWidth/2;

We have modified a sample for the same, please download it from the link given below.

Sample link : http://www.syncfusion.com/downloads/support/forum/142474/ze/SfNavigationDrawerSample-1031154501

Regards,
Mugundhan S.


Loader.
Up arrow icon