
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
How does Constraint Layout works in android?
In simple words, constraint layout is an advanced version of a Relative layout. It is used to reduce the child view hierarchies and improve the performance.
Properties of constraint layout as shown below -
Wrap Content –It wrap the view size according to data.
Any Size – This is very similar to match parent.
Fixed Size – This allows standard height and width(fixed sizes).
In the above example we have shown the button with all properties, now look into code level as shown below -
<Button android:layout_width="wrap_content" android:layout_height="53dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.391" app:layout_constraintStart_toStartOf="@+id/data1" app:layout_constraintTop_toTopOf="parent" />
In the above, we have declare layout margin-top, bottom, start and end. those are the standard distance (Similar to match parent)
There is two bias available in constraint layout as horizontal and vertical. Gravity space in a horizontal way and Vertical way.
By clicking view, it shows all hierarchies which are connected to the view.
By clicking view, you will get "X" color icon button to remove hierarchy with other view or parent view.
This example demonstrates how to use constraint layout in android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:id="@+id/layout" android:gravity="center" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/data" android:layout_width="wrap_content" android:gravity="center" android:textSize="30sp" android:text="www.tutorialspoint.com" android:layout_height="wrap_content" /> <TextView android:id="@+id/data1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="80dp" android:layout_marginEnd="8dp" android:gravity="center" android:text="www.tutorialspoint.com" android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:layout_width="wrap_content" android:layout_height="53dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.43" app:layout_constraintStart_toStartOf="@+id/data1" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click Run Icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Click here to download the project code
- Related Articles
- How to use Constraint Layout with recyclerview?
- How to use appbar layout in android?
- How does digital thermometer works?
- How to use Android sequence layout?
- How does == operator works in Python 3?
- How does DES works in Information Security?
- How does Steganography works in Information Security?
- Android Motion Layout in Kotlin
- How does jQuery event namespace works?
- How does JavaScript focus() method works?
- How does Secure Hash Algorithm works?
- How does the MD5 Algorithm works?
- How to detect orientation change in layout in android?
- How to add table rows Dynamically in Android Layout?
- How to Create a Tab Layout in Android App?
