Search code examples
androidandroid-constraintlayout

What exaclty is the meaning of MATCH_CONSTRAINT?


I am new to Android and recently I came up with a term 'match constraint' while using ConstraintLayout.

As per doc it says 'Dimension will be controlled by constraints', I don't understand what exactly mean by this ?

As far as I could understand , it's somehow can be used as replacement of match-parent but not sure how ?


Solution

  • First of all, lets look at what the word Constrain means. According to Google, Constrain means to

    • Compel or force to follow a particular course of action
    • Severely restrict the scope, activity or extent of
    • Bring about by compulsion

    When using ConstraintLayout we align/position our items by applying Constraints to that particular item. What these Constraints do is that they limit (or allow) the position of that item in the screen. Lets say I have a button which I constrain to be between the left and right edge of the screen. By doing so, the button can move anywhere in the screen as long as it is within the left and right edges of the screen. Similar is the case if I constrain the button to be between the top and bottom of the screen.

    Now what does MATCH_CONSTRAINT mean? It means, that the view will take up as much space as the Constraints allow it to take. So, if I constrain a view to be between the left and right edge of the screen, then the view will expand its width to be equal to the width of the screen (if no margins are set).