By passing in false
, we say that we do not want to attach our View
to the root ViewGroup
just yet. We are saying that it will happen at some other point in time. In this example, the other point in time is simply the addView()
method used immediately below inflation.
Also we need to pass in the parent ViewGroup
if we want the child xml width and height to be honored. Visit Source for further explanation.
// this inflate the view immediately
inflater.inflate(R.layout.custom_button, mLinearLayout, true);
// this is equivalent to above with one key difference
Button button = (Button) inflater.inflate(R.layout.custom_button, mLinearLayout, false); // view is promised to be added later
mLinearLayout.addView(button);
// for adding multiple item in list, use this method. First method doesn't bind data
val view = activity?.layoutInflater?.inflate(R.layout.item_doctor, fadt_doctor_list_ll, false)
view?.findViewById<TextView>(R.id.id_tv_name)?.text = ecp.getFullNameWithFallback()
view?.findViewById<ImageView>(R.id.id_imv_avatar)?.loadCircleAvatar(ecp.imageURL)
fadt_doctor_list_ll.addView(view)
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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"
style="@style/BaseCardShadow"
android:layout_width="match_parent"
android:layout_height="@dimen/appt_detail_doctor_item_height"
app:cardCornerRadius="24dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/id_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/id_imv_avatar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="@drawable/ic_doctor_default_avatar"/>
<TextView
android:id="@+id/id_tv_name"
style="@style/DemiBoldTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:maxLines="1"
android:textColor="@color/headline"
android:textSize="14sp"
android:ellipsize="end"
app:layout_constraintEnd_toStartOf="@+id/id_chat"
app:layout_constraintStart_toEndOf="@+id/id_imv_avatar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="Dr Nguyen Thi Luu" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/id_chat"
android:layout_marginEnd="10dp"
android:layout_width="@dimen/appt_detail_icon_size"
android:layout_height="@dimen/appt_detail_icon_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="@drawable/ic_chat_blue_2"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>