3

Here's my EditText

<?xml version="1.0" encoding="utf-8"?>
<EditText
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="49dp"
    android:layout_marginTop="3dp"
    android:text=""
    android:ems="10"
    android:gravity="center_vertical"
    android:imeOptions="actionNext"
    android:maxLines="1"
    android:background="@drawable/profile_edt_round_corners"
    android:drawablePadding="5dp"
    android:textSize="14sp"
    android:hint=""
    tools:ignore="RtlSymmetry"
    android:textColor="#4a4a4a"/>

Inflating the view

private fun getRoundedEditText() =
    layoutInflater.inflate(R.layout.v_rounded_corners, null) as EditText

Setting the values

private fun addDataToParents() {
    val editTextView = getRoundedEditText()
    editTextView.setText("Parent 1")
    ll_parents_guardian_container.addView(editTextView)
    val editTextView2 = getRoundedEditText()
    editTextView2.setText("Parent 2")
    ll_parents_guardian_container.addView(editTextView2)
}

Where ll_parents_guardian_container is

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/ll_parents_guardian_container"
            android:orientation="vertical">
    </LinearLayout>

However the layout_marginTop and layout_height which i had already defined in XML is not reflected there.

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77

1 Answers1

5

I found out the issue.

layoutInflater.inflate(R.layout.v_rounded_corners, null) as EditText

While inflating a view which has margin/ height properties, you shouldn't pass the root as null. This is the cause of the issue. Because the inflated view needs the root to draw the layout params.

So change it with the view on which you're going to add.

layoutInflater.inflate(R.layout.v_rounded_corners, ll_parents_guardian_container ) as EditText
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
  • I encountered the same issue with 0 margins, but was inflating views using view binding: `ActivityMyExampleBinding.inflate(layoutInflater)`. It was necessary to use a different overload, `ActivityMyExampleBinding.inflate(layoutInflater, container, true)` (the last argument automatically calls `addView()` for you, if `false` you must do it yourself). It appears that the generated binding `.java` class is implicitly passing `null` as the parent `ViewGroup` when you use the first overload. – Will Westrop Jul 25 '21 at 19:15
  • @WillWestrop can you do a debug by adding a breakpoint, that can easily find the root cause. if you still find it difficult please add a question and mention me. – Manoj Perumarath Jul 26 '21 at 09:16
  • 2
    I've got it working, thanks. Was just dropping a note in case people in future stumble upon this, that using view binding you can experience the same issue, but there, the passing of `null` for the container is hidden behind the binding class generated by the compiler (unless you know to use the correct method overload) – Will Westrop Jul 26 '21 at 11:35