Why Is 0Dp Considered a Performance Enhancement

Why is 0dp considered a performance enhancement?

First of all you have this,

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>

Never take the ListView's height as wrap_content, that will lead into troubles. Here is the reason for that and this answer.

Further more,

I searched around but haven't found anything that really explains why
Android Lint as well as some Eclipse hints suggests replacing some
layout_height and layout_width values with 0dp.

Its because you are using layout_weight = "1" that means your ListView with take the height as much as is available to it. So, in that case there is no need of using layout_height = "wrap_content" just change it to android:layout_height="0dp" and ListView's height will be managed by layout_weight = "1".

In ConstraintLayout, what is the meaning of android:layout_width=0dp, when there is no `weight` attribute?

for the children of ConstraintLayout if you have set constraints then the 0dp is for match_constraint (take full width, or full height)

Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"
https://developer.android.com/reference/android/support/constraint/ConstraintLayout

example

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="text1" />

<TextView
android:id="@+id/tv_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toStartOf="@id/tv_3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_1"
tools:text="text2" />

<TextView
android:id="@+id/tv_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tv_2"
app:layout_constraintTop_toBottomOf="@id/tv_1"
tools:text="text3" />
</android.support.constraint.ConstraintLayout>

Sample Image

in the above code (and image) you see that text1 TextView width is the width it needs to write the text text1

for text2 and text3 TextView it takes the whole width divided by 2 that is what the constraints say

Why is 0dp the same as wrap_content in this ConstraintLayout?

In order for the button to match the parent layout, you need to set two opposite constraints....as in

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/button1"
<!-- Since you are setting width as 0dp, i presume you want the width to match... -->
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="20dp"
android:text="1"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
<!-- You must add here another constraint to the right -->
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>```

What is the trick with 0dip layout_height or layouth_width?

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.

In android layouts what is the effect/meaning of layout_height=0dip

Yep you are right about the weight, when you want the width or height to be controlled by weight its convention to set that value to 0dip and let the weight control the actual value. Although I am pretty sure 0 is just arbitrary here you could put anything but putting 0 makes your intention more clear.

How to get real height layout with layout_height=0dp?

It is because the views are not drawn yet when setHeartStrengthViewHeight is called. To solve this, try the following:

@JvmStatic
@BindingAdapter("app:heartStrength")
fun setHeartStrengthViewHeight(bar: FrameLayout, level: Int) {
val barParent = bar.parent as FrameLayout

val observer : ViewTreeObserver = barParent.viewTreeObserver
observer.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
println("bar parent height: ${barParent.height}")
barParent.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})

}


Related Topics



Leave a reply



Submit