EasyLayout.Droid 1.0.5

EasyLayout.Droid makes it easier to read, write, and maintain relative layouts in Xamarin Android. It's a port of Frank Krueger's EasyLayout (https://gist.github.com/praeclarum/6225853).

Homepage: https://github.com/lprichar/EasyLayout.Droid

Platform: NuGet

Language: C#

License: MIT

Keywords: Xamarin, RelativeLayout, Android, EasyLayout

View on registry: https://www.nuget.org/packages/EasyLayout.Droid/1.0.5

Direct download link: https://www.nuget.org/api/v2/package/EasyLayout.Droid/1.0.5

Install: Install-Package EasyLayout.Droid -Version 1.0.5


EasyLayout.Droid makes it easier to read, write, and maintain relative layouts in Xamarin Android. It's a port of Frank Krueger's EasyLayout (https://gist.github.com/praeclarum/6225853).

Sample Project

The best place to get started is to take a look at the sample project.

Sample Project Screenshot

Example 1 - Parent Align

If you want to align an image to the edges of the frame you used to do this:

var layoutParams = new RelativeLayout.LayoutParams(
_image.LayoutParams = layoutParams;

Now you can do this:

relativeLayout.ConstrainLayout(() =>
    _image.Top == relativeLayout.Top
    && _image.Right == relativeLayout.Right
    && _image.Left == relativeLayout.Left
    && _image.Bottom == relativeLayout.Bottom

There's no need to set LayoutParams at all. If they don't exist EasyLayout.Droid will add them, if they do EasyLayout.Droid will append to them. And if you don't add them it will take care of choosing LayoutParams.MatchParent or WrapContent, EasyLayout.Droid.

Example 2 - Relative Alignment and Constants

If you wanted to align an image 20 dp under another image and center align it to the parent you used to do this:

var layoutParams = new RelativeLayout.LayoutParams(
layoutParams.AddRule(LayoutRules.AlignBottom, image1.Id);
layoutParams.TopMargin = DpToPx(20);
_image2.LayoutParams = layoutParams;

There's a couple of gotchas.

  1. If you set the TopMargin to 20, then Android assumes you mean pixels not device independent pixels. To fix that you need to remember to call a function like DpToPx().
  2. Your relative view (image1) needs to have an Id. If you forget to set it there's no error, it just does strange layout things.

EasyLayout.Droid replaces the code above with:

relativeLayout.ConstrainLayout(() =>
    _image2.Top == _image1.Bottom + 20
    && _image2.GetCenterX() == relativeLayout.GetCenterX()

That's less code and easier to read plus there's some other small benefits.

  • If you forget to add an Id to _image1, EasyLayout.Droid will automatically add one for you.
  • EasyLayout.Droid always assumes every number is in Dp, so it automatically converts all literals for you.

Incidentally, GetCenterX() is one of a couple of new extension methods along with GetCenterY() and GetCenter().

Example 3 - Constants

Constants weren't difficult to work with previously, but for completeness they used to work like this:

var layoutParams = new RelativeLayout.LayoutParams(
_image2.LayoutParams = layoutParams;

With EasyLayout.Droid you can do this:

relativeLayout.ConstrainLayout(() =>
    _image.Width == 50
    && _image.Height == ViewModel.SomeHeight.ToConst()

As mentioned previously 50 will be assumed to be in dp and will be auto-converted to pixels. Also arbitrary properties such as SomeHeight will need the .ToConst() extension method applied in order to tell EasyLayout.Droid that they should be treated as constants.

Example 4 - ListView's

Displaying list data in Android using a custom layout is traditionally accomplished by inflating an AXML file for each row of data.

Check out the ViewProductsActivity.cs example or this blog post for how to build custom lists without AXML files.


If you want to add this to your project you can either install via NuGet (safer):

Install-Package EasyLayout.Droid

or if you think it's perfect as is (you don't want updates) you can copy EasyLayoutDroid.cs into your source.


For more information read Introducing EasyLayout.Droid.


All code is MIT Licensed.



1.0.5 March 31, 2017
1.0.4 January 12, 2017
1.0.3 January 11, 2017
1.0.2 January 10, 2017
1.0.1 January 08, 2017
1.0.0 January 08, 2017

Project Statistics

SourceRank 7
Dependencies 0
Dependent projects 0
Dependent repositories 0
Total releases 6
Latest release
First release
Stars 9
Forks 2
Watchers 2
Contributors 2
Repo Size: 129 KB

Top Contributors See all

Lee Richardson Dan Dombrowski

Something wrong with this page? Make a suggestion

Export .ABOUT file for this library

Last synced: 2017-04-01 15:19:54 UTC

Login to resync this project