How to use the new Android Constraint Layout to reduce View hierarchy
Solution 1
You can go to an existing layout resource file, open the visual editor and right-click on a RelativeLayout (for example) and click the option to convert to a constraint layout.
You also have to add the Gradle dependency in build.gradle file:
compile 'com.android.support.constraint:constraint-layout:1.0.0'
Solution 2
From Docs
If you're updating an existing project, proceed as follows:
Ensure you have the latest Android Support Repository (version 32 or higher): // This was the part that was missing for me
Click Tools > Android > SDK Manager. Click the SDK Tools tab. Select Android Support Repository, then click OK.
Add the updated Constraint Layout library as a dependency in your build.gradle file:
dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.0'
}
In the toolbar or sync notification, click Sync Project with Gradle Files.
To add a new constraint layout to your project:
- Right-click on your module's layout directory, then click New > XML > Layout XML. Enter a name for the layout and enter "android.support.constraint.ConstraintLayout" for the Root Tag. Click Finish.
To convert an existing layout to a constraint layout:
- Open your existing layout in Android Studio and select the Design tab at the bottom of the editor window. In the Component Tree window, right-click the layout and click Convert to ConstraintLayout.
Solution 3
Go though this link from Google CodeLabs. You will have basic idea of Constraint Layout and how to use different constraints like Manual Constraint
, Auto Connect
& Inference
.
Also there is UI Builder
& Inspector
which will help us to build faster UI.
Solution 4
I tried many version, but i couldn't solve the issue! Finally i let Android Studio to solve this issue.
In the XML file, Beside error message you can see this option! Click on that to import reccomonded version
or you can press alt+enter placing your cursor on error line
I got constraint-layout:1.0.0-alpha8, when i pressed alt+enter
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
Solution 5
add dependencies
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha9'
And create new layout xml file --> goto design tab --> right click your root layout and select last option convert LinearLayout to ConstraintLayout
See Screenshot
Bogdan M.
Updated on November 25, 2020Comments
-
Bogdan M. over 3 years
Do you have any idea on how to use the new Constraint Layout that was recently announced at this year's Google I/O?