Height vs minHeight Toolbar

5.8k views Asked by At

I recently updated to AppCompatActivity and switched from ActionBar to ToolBar.

When I was checking the xml, I found these two attributes -

android:layout_height="wrap_content"

and

android:minHeight="?attr/actionBarSize"

What is the difference between these two attributes? Why is layout_height set to wrap_content in the ToolBar documentation?

Is it necessary to use both the attributes?

3

There are 3 answers

0
Zahid On BEST ANSWER
  • On using both the attributes, android:layout_height="wrap_content" and android:minHeight="?attr/actionBarSize", there is a possibility that your toolbar height may get larger when you use larger icons.
  • android:minHeight ensures that your toolbar will not resize itself smaller than ?attr/actionBarSize value.

If you want your toolbar height to be fixed, just use

android:layout_height="?attr/actionBarSize"

The other attribute is not needed.

0
Moinkhan On

Ok when you specify android:layout_height="wrap_content" then your toolbar may shrink if it's not contain larger child view then the standard actionbar size.

But if you specify android:minHeight="?attr/actionBarSize" then it doesn't matter how small view is there in toolbar it will maintain its standard size of ActionBar.

0
Sergey Shustikov On

What is the difference between these two attributes?

android:minHeight Defines the minimum height of the view.
android:layout_height Specifies the basic height of the view.

Pseudocode for more clear.

if(minHeightDefined) {
    if(currentHeightOfView < minHeight) {
        currentHeightOfView = minHeight;
    } else {
        currentHeightOfView = layout_height;
    }
} else {
    currentHeightOfView = layout_height;
}

minHeightDefined - flag indicate does android:minHeight declared in layout xml file or not

Why is layout_height set to wrap_content in the ToolBar documentation?

Because this is a default implementation.