19

I mean why anybody want they view to be 0dip height ? I have seen this many times, there must be some kind of trick, but I do not get it.

        <TextView android:gravity="top" android:textColor="#FFFF0000"
            android:textSize="20dip" android:text="TextView"
            android:layout_height="0dip" android:layout_width="fill_parent"
            android:id="@+id/contactName"></TextView>

Why they don't use for example wrap_content ? what do they want to achieve ?

Lukap
  • 31,523
  • 64
  • 157
  • 244

3 Answers3

26

This is heavily used for views withing LinearLayout. There are three "layout" attributes that LinearLayout is aware of:

  1. android:layout_height
  2. android:layout_width
  3. android:layout_weight

You can find example with android:layout_weight in tutorial project.

So when android:layout_weight is used on View X and LinearLayout is horizontal, then X's android:layout_width is simply ignored.

Similar, when android:layout_weight is used on View X and LinearLayout is vertical, then X's android:layout_height is ignored.

This actually means, that you can put anything in those ignored fields: 0dp or fill_parent or wrap_content. It doesn't matter. But it's recommended to use 0dp so View's do not do extra calculation of their height or width (which is then ignored). This small trick simply saves CPU cycles.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • 5
    this is incorrect . try using different weights (for example 1,2,3) and see that there is a difference between using match_parent and 0px (or 0dp) – android developer Aug 18 '12 at 08:45
  • @androiddeveloper, can you elaborate on this a bit? Why does this happen? Can you link to documentation/other references on the subject if possible? – batbrat Mar 30 '14 at 08:23
  • @batbrat I don't remember where I read about this. However, you can just try it out and see for yourself. you can also read what Lint suggests about this, as it's bad for the performance of the LinearLayout. – android developer Mar 30 '14 at 08:31
  • @androiddeveloper, I was hoping for an official word rather than experimental verification of a change in layout based on 0dp versus match_parent etc. I'm aware that using a non-zero value will cause cause extra calculations that are best avoided for performance reasons. Thanks for the quick response. – batbrat Mar 30 '14 at 08:54
  • @batbrat sorry. I just don't remember. maybe i didn't even read about it but just noticed it. – android developer Mar 30 '14 at 09:07
  • @androiddeveloper, thanks! I'll try it out and look around. I really appreciate your helping out. – batbrat Mar 30 '14 at 09:15
19

This is usually used when having many views inside a linearlayout and have set android:layout_weight="1" in order both views to take equal space. for example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="TextView" />

</LinearLayout>

In that case, the view will take as much height as all other views.

Community
  • 1
  • 1
Dimitris Makris
  • 5,183
  • 2
  • 34
  • 54
  • Yes, I usually set 0dip width or height in this case. Or you could use it to hide widgets, however the visibility attribute should be used in that case. – Zsombor Erdődy-Nagy Aug 28 '11 at 10:46
2

The android:layout_height="0dp" is used in various codes because:

  1. It means the height of the view can be changed later due to other layout constraints.
  2. It is a common practice and often seen in relative and linear layouts.

e.g:

android:layout_height = "0dp"
android:layout_weight = "1.0"

Height or width when set to "0dp", are mostly used in combination with "weight". e.g. you want to fill all the available space for height then use the above code and like wise the same case for width.

Kjuly
  • 34,476
  • 22
  • 104
  • 118