Home > Back-end >  onTextChange, create new EditText in LinearLayout
onTextChange, create new EditText in LinearLayout

Time:08-16

I am creating a database that has a user inputable quantity for the number of rooms. For that quantity I want to create EditText fields where the user can then enter a personalized name for the space rather than the generic one I generate.

I thought I had figured out how parse the data from the initial Child EditText field and add the new EditText to the appropriate layout with the quantity provided but it doesn't appear to be working.

I have pasted examples of the Layout XML as well as the relevant code supporting the work I am currently doing to accomplish this task.

The layout I want to add rows of user settable names to pending the input from the input_brQty field. This linear layout is inside a ScrollView if it matters

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@ id/input_brQty"
                        android:layout_width="48dp"
                        android:layout_height="match_parent"
                        android:ems="10"
                        android:gravity="center"
                        android:inputType="number" />

                    <TextView
                        android:id="@ id/textAddBed"
                        android:layout_width="292dp"
                        android:layout_height="48dp"
                        android:ems="10"
                        android:gravity=""
                        android:paddingStart="8dp"
                        android:scrollHorizontally="false"
                        android:text="Additional Bedrooms" />
                </LinearLayout>
            </LinearLayout>

code for recognizing a change in the input of input_brQty.

 // secondary Bedroom QTY
    binding.inputBrQty.doOnTextChanged { text, start, count, after ->
        if (bed.isNotEmpty())
            if (bed.toInt() > 0)
                CreateLayout(bed.toInt(), binding.secBr, "Secondary Bedroom ", roomList)
    }

Function doing the actual creation and storing of the created ID so I can get the data later to push to a database.

fun CreateLayout(
    qty: Int,
    parent: LinearLayout,
    textDisplay: String,
    storage: MutableList<String>
) {
    var tmp = 1

    while (tmp < qty) {

        val editText = EditText(this)
        editText.layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        editText.setText(textDisplay   tmp)
        editText.isVisible = true
        editText.id = View.generateViewId()

        // Add EditText to LinearLayout
        parent.addView(editText)

        // add to list for retrieval later
        storage.add(editText.id.toString())

        // update iterator
        tmp  
    }

CodePudding user response:

One mistake you might be doing is here .

if (bed.isNotEmpty())

Here you might be initializing bed before onTextChanged.So new value will not be updated to bed. So value will be always empty at initial . Move that inside doOnTextChanged.Something like this

binding.inputBrQty.doOnTextChanged { text, start, count, after ->
            val bed = text.toString()
            binding.secBr.removeAllViews() //You need this if you want to remove already added view.
            if (bed.isNotEmpty())
                if (bed.toInt() > 0)
                    CreateLayout(bed.toInt(), binding.secBr, "Secondary Bedroom ", roomList)
        }

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <EditText
            android:id="@ id/input_brQty"
            android:layout_width="48dp"
            android:layout_height="match_parent"
            android:ems="10"
            android:gravity="center"
            android:inputType="number" />

        <TextView
            android:id="@ id/textAddBed"
            android:layout_width="292dp"
            android:layout_height="48dp"
            android:ems="10"
            android:gravity=""
            android:paddingStart="8dp"
            android:scrollHorizontally="false"
            android:text="Additional Bedrooms" />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:id="@ id/secBr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/> // Separate Linear layout for adding dynamic edittext . As we remove existing views.
</LinearLayout>
    
  • Related