How to use Constraint Layout in Xamarin.Android
Tags: androidlogin, constraintlayout, signup, xamarin
Published: 2019-11-13 17:18:55

---
title: How to use Constraint Layout in Xamarin.Android
published: true
date: 2019-07-27 22:02:00 UTC
tags: Android. Login,Constraint Layout,Signup,Xamarin
canonical_url: https://logeshpalani.blogspot.com/2019/07/how-to-use-constraint-layout-in.html
---

 **Introduction:**  
  
                         In this article demonstrates, what is Constraint Layout and how to use it in Xamarin.Android and Xamarin.iOS to improve your page performance and the response of UI. There is no default package for constraint layout in Xamarin.Android. So, we need to install a plugin for this.  

  

[![](https://1.bp.blogspot.com/-SbhPt4cIiek/XTy36EqoGWI/AAAAAAAAOxA/2NkUGCtKRXEnOEl3eB_YKWnxEvA44rNgACLcBGAs/s400/JPEG%2BFile.jpg)](https://1.bp.blogspot.com/-SbhPt4cIiek/XTy36EqoGWI/AAAAAAAAOxA/2NkUGCtKRXEnOEl3eB_YKWnxEvA44rNgACLcBGAs/s1600/JPEG%2BFile.jpg)

  

  

  

**What is Constraint Layout?**

  

** **                

** **  Constraint helps to reduce the hierarchy of views and increase the layout performance and flexibility of views. It helps to design based on positing, size, dimension, and alignment of different views. Constraint Layout has different attributes to set views in the layout. You can set the view of any position of the layout easily rather than Relative Layout. It will support Android and iOS platform. Constraint layout comes under ViewGroup.  

  

**Advantages of Constraint Layout?**

-  Flat View Hierarchy and no nested view.    
- Better performance and high responsiveness. 
- Align position, size, and alignment of the screen.
-  Support Android (API 9) and iOS Platform.
  

  

**Some Important Constraint Layout Properties and Attributes?**

  

These attributes take only id or parent. Align view from any side of the target view. 

- layout\_constraintLeft\_toLeftOf - align left of the desired view is left of the target view.

- layout\_constraintLeft\_toRightOf - align left of the desired view is right of the target view.

- layout\_constraintRight\_toLeftOf - align-right of the desired view is left of the target view.

- layout\_constraintRight\_toRightOf - align-right of the desired view is right of the target view.

- layout\_constraintTop\_toTopOf - align the top of the desired view is top of the target view.

- layout\_constraintTop\_toBottomOf - align the top of the desired view is the bottom of the target view.

- layout\_constraintBottom\_toTopOf - align the bottom of the desired view is top of the target view.

- layout\_constraintBottom\_toBottomOf - align the bottom of the desired view is the bottom of the target view.

- layout\_constraintBaseline\_toBaselineOf - align the baseline of the desired view is the baseline of the target view.

- layout\_constraintStart\_toEndOf - align the start of the desired view is the end of the target view.

- layout\_constraintStart\_toStartOf - align the start of the desired view is the start of the target view.

- layout\_constraintEnd\_toStartOf - align the end of the desired view is the start of the target view.

- layout\_constraintEnd\_toEndOf - align the end of the desired view is the end of the target view.
  

Margin Attributes - These attributes take only positive values.

  

- layout\_marginStart - Margin from start side of the desired view.

- layout\_marginEnd - Margin from the end side of the desired view.

- layout\_marginLeft - Margin from the left side of the desired view.

- layout\_marginTop - Margin from the top side of the desired view.

- layout\_marginRight - Margin from the right side of the desired view.

- layout\_marginBottom - Margin from the bottom side of the desired view.
  

  

**Let's start how to use Constraint Layout to design or align views in Xamarin.Android?**

  

**Android output:**

  

[![](https://1.bp.blogspot.com/-8wJNgxcp-jo/XTy-UamY4XI/AAAAAAAAOxQ/RZjyer7OyV4yXZJ9Y7LBXUt4H4tfkhEdwCLcBGAs/s400/60339134-8c1f9d80-99c5-11e9-8dd9-6f34af7469cd.png)](https://1.bp.blogspot.com/-8wJNgxcp-jo/XTy-UamY4XI/AAAAAAAAOxQ/RZjyer7OyV4yXZJ9Y7LBXUt4H4tfkhEdwCLcBGAs/s1600/60339134-8c1f9d80-99c5-11e9-8dd9-6f34af7469cd.png)
[![](https://1.bp.blogspot.com/-VXxcN0KQmVw/XTy-UYI3YKI/AAAAAAAAOxM/6uPtTofBWBgBsCHWiJuGU3O27wJo3pSPgCLcBGAs/s400/60339137-8f1a8e00-99c5-11e9-9c61-dde5caa245fc.png)](https://1.bp.blogspot.com/-VXxcN0KQmVw/XTy-UYI3YKI/AAAAAAAAOxM/6uPtTofBWBgBsCHWiJuGU3O27wJo3pSPgCLcBGAs/s1600/60339137-8f1a8e00-99c5-11e9-9c61-dde5caa245fc.png)  

  

  

Follow these steps to archive the above view using Constraint Layout.

  

  

**Let’s Start** 

  

**Step 1: Create new Project**

  

            You can create Xamarin Android app by going to File \>\> New \>\> Android under select App \>\> In the General section select Android App then click Next \>\> In the new window, enter and select your application name, theme and compatibility followed by click Create project.  
  
  

[![](https://1.bp.blogspot.com/-E2PDbYKBk6g/XTzJbrlEkyI/AAAAAAAAOxg/VYHJlVIc2yAyAkIW5h5oWa5drK8efV0bgCLcBGAs/s400/Project%2Bcreation.jpg)](https://1.bp.blogspot.com/-E2PDbYKBk6g/XTzJbrlEkyI/AAAAAAAAOxg/VYHJlVIc2yAyAkIW5h5oWa5drK8efV0bgCLcBGAs/s1600/Project%2Bcreation.jpg)
  

  

**Step 2: Add Constraint Layout Plugin**

  

After project creation add the following NuGet package to your project

-           [Xamarin.Android.Support.Constraint.Layout](https://www.nuget.org/packages/Xamarin.Android.Support.Constraint.Layout)
-           Material Design.

  

For that, Right click packages and select add packages \>\> New dialog appear, right top corner search “Xamarin.Android.Support.Constraint.Layout” and click Add Packages button.

  

**Step 3: Design Instruction**

  

                Now, design Sign up page. For that, Go to Solution Explorer \>\> Resource \>\> layout \>\> double click to open content\_main.xml. First, we need to set the root of the layout is Constraint layout and declare app namespace.   
  
_\<android.support.constraint.ConstraintLayout  _  
_        xmlns:android="http://schemas.android.com/apk/res/android"  _  
_        xmlns:app="http://schemas.android.com/apk/res-auto"  _  
_        xmlns:tools="http://schemas.android.com/tools"  _  
_        android:id="@+id/container"  _  
_        android:background="@drawable/gradientcolor\_rose"  _  
_        android:layout\_width="match\_parent"  _  
_        android:layout\_height="match\_parent"  _  
_        android:paddingBottom="@dimen/activity\_vertical\_margin"  _  
_        android:paddingLeft="@dimen/activity\_horizontal\_margin"  _  
_        android:paddingRight="@dimen/activity\_horizontal\_margin"  _  
_        android:paddingTop="@dimen/activity\_vertical\_margin"  _  
_        android:hapticFeedbackEnabled="false"\>  _  
  
_\</android.support.constraint.ConstraintLayout \>_  
  
Now, start your design. Set the image button in the left top corner of the page. The below Constraint attribute used to set the image button based on parent view.

  

_app:layout\_constraintLeft\_toLeftOf = “parent”_

_app:layout\_constraintTop\_toTopOf = “parent”_

  

Logo Image – This view set by the right of the image button and centre of the parent. The top, end, start attribute of image set based on parent view and another one attribute set based on another view id.

  

_app:layout\_constraintTop\_toTopOf="parent"_

_app:layout\_constraintStart\_toEndOf="@+id/imageView2"_

_app:layout\_constraintStart\_toStartOf="parent"_

_app:layout\_constraintEnd\_toEndOf="parent"_

  

Text View – This view set below the image and left the side of the parent. This below Constraint attributes used to set the text in the correct position. The first left attribute set based on the parent but other attributes set by different id. 

  

_app:layout\_constraintLeft\_toLeftOf="parent"_

_app:layout\_constraintTop\_toBottomOf="@+id/imageView"_

_app:layout\_constraintBottom\_toTopOf="@+id/txtFirstName"   _

  

As well as, you can set other child views using different attributes.

  

**Step 4: Login Page Design**

  

Now, Design LoginPage. For that, Go to Solution Explorer \>\> Resource \>\> layout \>\> double-click to open login\_activity.xml. This page code given below.

  

<script src="https://gist.github.com/logeshpalani98/965fc5d69d4d2534bcf444fe96ed704b.js"></script>  

  

**Step 5: Signup Page Design**

  

         Now, create a background colour of Signup page named as grandientcolor\_rose.xml. Add new XML page by going to right-click drawable folder \>\> Add new File \>\> Left bar select Android \>\> next followed by select XML file \>\> give the name as grandientcolor\_rose.xml. Copy and paste the below code.

  

<script src="https://gist.github.com/logeshpalani98/badc723fce1b39520af3d697078a651b.js"></script>  

**Step 6: MainActivity.cs Code**

  

         Next open MainActivity.cs(Login Activity)  and add the following code to validate the user input. When you click login, it will validate and check the user is available or not. If credentials are correct it goes to the main page. If you want to sign up click a floating button.

  

<script src="https://gist.github.com/logeshpalani98/d6da0359a8474802bbd2f1083ac36668.js"></script>  

  
 **Step 7: SignupActivity.cs Code**

  

         Add another activity for the sign-up page. For that, Go to Solution Explorer \>\> Resources \>\> activities \>\> Add a new activity named as signup\_activity.cs. Full source code is given below. Entered data will be validated and stored in Intent. 

  

<script src="https://gist.github.com/logeshpalani98/43003bcf8abbe3a62273222ce8d36cd4.js"></script>  

  

**Step 8: Output**

  
  

         Now, Rebuild and run your application. You will have the result like below.

  

Android Output:

  

  

[![](https://1.bp.blogspot.com/-8wJNgxcp-jo/XTy-UamY4XI/AAAAAAAAOxY/9wHIjIjECXMrTDqkoOrfZEHF_H7lfRC7QCEwYBhgL/s400/60339134-8c1f9d80-99c5-11e9-8dd9-6f34af7469cd.png)](https://1.bp.blogspot.com/-8wJNgxcp-jo/XTy-UamY4XI/AAAAAAAAOxY/9wHIjIjECXMrTDqkoOrfZEHF_H7lfRC7QCEwYBhgL/s1600/60339134-8c1f9d80-99c5-11e9-8dd9-6f34af7469cd.png)
 [![](https://1.bp.blogspot.com/-VXxcN0KQmVw/XTy-UYI3YKI/AAAAAAAAOxU/Mkbjl8VT7F8ZTzm6vATfiTcQTXXxyTFQwCEwYBhgL/s400/60339137-8f1a8e00-99c5-11e9-9c61-dde5caa245fc.png)](https://1.bp.blogspot.com/-VXxcN0KQmVw/XTy-UYI3YKI/AAAAAAAAOxU/Mkbjl8VT7F8ZTzm6vATfiTcQTXXxyTFQwCEwYBhgL/s1600/60339137-8f1a8e00-99c5-11e9-9c61-dde5caa245fc.png)  
  

  

Note: Import images, string, colour, theme.

Full Source Code Source: [https://github.com/logeshpalani98/XamarinAndroid-LoginSignupConstraintDemo](https://github.com/logeshpalani98/XamarinAndroid-LoginSignupConstraintDemo)  

  

  

Thank for reading and if you have any suggestions command below.

Reference Android Guide: [https://developer.android.com/reference/android/support/constraint/ConstraintLayout](https://developer.android.com/reference/android/support/constraint/ConstraintLayout)