Skip to main content

Responsive Layout In Android Using Constraint Layout



Create Responsive UI with Constraint Layout, this layout enables us to create a large and complex UI hierarchy in flat view, a major advantage of this layout is that we no longer required nested view group. It is similar to relative layout but more flexible than Relative Layout and you can design the layout with Android Studio Layout Editor very easily. So here is the tutorial to create the flat user interface using Constraint Layout.



1. Introduction


ConstraintLayout is available in API library and it is compatible with  API level 9 and upper version. Its design technique is well supported in Android Studio Layout Editor. Using Android Studio Layout Editor you can create the beautiful layout design by just using the drag and drop rather than writing the complete code in XML files. So, in simple word, we can say that we can create flat, flexible layout with this Layout.

2. Add ConstraintLayout dependency to project


For adding the constraintLayout to your project we need to flow the below-mentioned steps: -Install Constraint Layout Package

  1. Add the latest ConstraintLayout Library to your Android Studio

  2. Go to Tool> Android > SDK Manager

  3. Click on SDK Tool

  4. Under Support Repository select the “ConstraintLayout for Android” and “Solver for ConstraintLayout”

  5. Click on Apply and OK

  6. Click on Finish and then OK


[caption id="attachment_126" align="aligncenter" width="407"] Constrain Layout Install[/caption]

7. Now we need to add the below code to our project level build.gradle file and click on sync project

dependencies {
'com.android.support.constraint:constraint-layout:1.0.1'
}


3. Add Constraint Layout to in XML


For adding the constraint Layout to in your XML files add below XML code to your top-level view.

<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.debugandroid.constraintlayoutdemo.MainActivity">

</android.support.constraint.ConstraintLayout>





4. Adding Constraint to Constraint Layout View


Now click on design tab just after the text mode and start adding the other layout view inside Constraint Layout. Here in below screen shots as you can see that be have added two item one is text View and another one is the button.



In this, as you can see that Text View are connected to left side of the main Layout and similarly on top and button are connected with TextView from the left side and right side with the main layout. It is called constraint for adding the constraint to your view you have to click the view and then click on the anchor point and drag the anchor to other views to connect.
In Constraint Layout, every view must have two constraints one is horizontal and one is vertical. You can create the constraint only between two constraint handlers and an anchor point which have the same baseline.
For removing the constraint click on view and click on constraint handler as given in below screen shots as  I have clicked on TextView constraint handler.

5. Converting Liner Layout to Constraint Layout


For converting the Layout from Linear Layout to Constraint Layout we need to just open the layout XML file in design mode and then right click on the Linear Layout and click on convert view to Constraint Layout. It will automatically convert your Linear Layout to required format using which you can start designing your layout using just drag and drop for creating the decent looking layout for your mobile application.


6. How to enable auto Constraint on or off


Auto Constraint on or off enable us to automatically add the constraint to your layout, for enabling this option you have to right click on Constraint Layout or any view then > Click on Constraint Layout > Turn On/Off Auto Connect

You can check out more about this here.

Comments

  1. […] this fragment, we are going to use the constraint layout, a Recycler View and Button to refresh the list of available network. The layout will look like as […]

    ReplyDelete

Post a Comment

Popular posts from this blog

Flutter Theme Creation, Programmatic Way

Flutter and Dart is an excellent combination for creating the mobile app with material design with nice GUI. Its lots of widget which help the developer for creating the app UI very fast with respect to the other language. You may face some problem in starting due to comm, curly brace and semicolon but after some time you will find the way how to work with these all stuff and it becomes very easy for you. But in this article, we are not going to discuss all these. In this article, we will handle a very interesting topic on how to create a custom theme or multiple themes and let's user decide which theme to use.



Create a flutter project



So let's create a flutter project by selecting the appropriate option from the File menu in android studio or Visual Code. If you are new to Flutter and Dart you can check out our recent post to get started Creating Cross-platform app using flutter.



Once your project is created you will get the default counter app and you try running the app in the…

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View.

Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android
Just go to the android folder and open it in separate windows. We will have the following project structure.









Create the Activity Just right click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project sync completed.
Update the project build config …

Flutter State Management With Provider and ChangeNotifier

Flutter state management with provider library and why it's so important? There are lots of topics in flutter which are very important but out of those state management of your flutter application is the most important and little bit complicated as well. So in this article, I will try to explain how to use the provider package for managing the state of flutter and dart application and how to access the data down the hierarchy of the widget tree without recreating the whole UI.



Basic knowledge of the state management and data flow in the Flutter



Before starting the coding I would like to explain some basics about the Flutter application so that you can understand, why it's important to manage the state of your application in the correct way.



In Flutter, everything is a widget and whenever the state of your application changes the UI is recreated the UI with the new data. Let's list out the topic or problem that we are going to explore about state management.



Challenge of St…