Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ConstraintLayout Optimizer?

Google published ConstraintLayout 1.1.0 beta 6 on March 22, 2018. It has a new constraint known as Optimizer. The documentation of Optimizer at https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#Optimizer does not mention when to use and why to use it. Can someone shed some light about its usage.

like image 623
Monish Kamble Avatar asked Apr 12 '18 17:04

Monish Kamble


People also ask

What is the use of ConstraintLayout?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.

Should you use ConstraintLayout?

Well, each layout has its own benefits but when it comes to complex, dynamic and responsive views you should always choose Constraint Layout. Constraint Layout was added to Android Studio 2.2 in 2016 and it became the default layout of Android Studio because of its simplicity and ease of creating complex layouts.

What is the difference between RelativeLayout and ConstraintLayout?

Unlike RelativeLayout , ConstraintLayout offers a bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with a red circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes.

Is ConstraintLayout faster than LinearLayout?

More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout. I want to pay attention to 2 more things which are more subjective. Creating Constraint Layout takes more time than other layouts. Also introducing changes in existing Constraint Layout is much more time-consuming.


1 Answers

Constraint Layout 1.1 adds several new optimizations that speed up your layouts. The optimizations run as a separate pass, and attempt to reduce the number of constraints needed to layout your views.

In general they work by finding constants in your layout and simplifying them.

There’s a new tag, called layout_optimizationLevel, which configures the optimization level. It can be set to the following:

  • barriers figures out where barriers are and replaces them with simpler constraints
  • direct optimizes elements directly connected to fixed element, for example the side of the screen or guidelines, and continues to optimize any elements directly connected to them
  • standard is the default optimization level which includes barriers and direct
  • dimensions is currently experimental and can cause issues on some layouts — it optimizes the layout pass by calculating dimensions
  • chains is currently experimental and figures out how to lay out chains of elements with fixed sizes.

If you want to try out the experimental optimizations dimensions and chains you can enable them on a ConstraintLayout with

<android.support.constraint.ConstraintLayout 
    app:layout_optimizationLevel="standard|dimensions|chains"

Note: To clear whole concept, you must have to implement this.

like image 187
Pratik Butani Avatar answered Oct 11 '22 11:10

Pratik Butani