How to Implement TextWatcher in EditText - Android?

Viewed 58

I am inflating a Linear Layout dynamically 2 times which has some editTexts and some TextViews

The layouts are inflated successfully, but when I am setting a TextWatcher , then the effect is seen only on the 2nd inflated layout and not on first one

This is how I am inflating layout:

 LinearLayout myLayout = findViewById(R.id.rootLayout);
 
            final View rootView = getLayoutInflater().inflate(R.layout.registration_screen, null, false);
            length1 = rootView.findViewById(R.id.length1);
            breadth1 = rootView.findViewById(R.id.breadth1);
            area = rootView.findViewById(R.id.area);
            carpetarea = rootView.findViewById(R.id.carpetarea);
            myLayout.addView(rootView);

        }

This is the TextWatcher I am adding:

 breadth1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() != 0) {

                    float a = Float.parseFloat(length1.getText().toString());
                    float b = Float.parseFloat(breadth1.getText().toString());
                    area.setText(String.valueOf(a * b));
                    float c = a * b;


                }
                if (charSequence.length() == 0) {
                    area.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        length1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() != 0) {
                    if (!breadth1.getText().toString().equalsIgnoreCase("")) {
                        float a = Float.parseFloat(length1.getText().toString());
                        float b = Float.parseFloat(breadth1.getText().toString());

                        area.setText(String.valueOf(a * b));
                        float c = a * b;                  
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

This is my XML code:

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="12dp"
            android:fontFamily="@font/mo_re"
            android:padding="4dp"
            android:text="Dimensions in ft."
            android:textColor="@color/black"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="6dp"
            android:orientation="horizontal"
            android:weightSum="3">

            <EditText
                android:id="@+id/length1"
                android:layout_width="wrap_content"
                android:layout_height="38dp"
                android:layout_marginLeft="4dp"
                android:layout_weight="1"
                android:background="@drawable/corner_bg_new"
                android:fontFamily="@font/mo_re"
                android:hint="Length"
                android:inputType="number"
                android:padding="6dp"
                android:textColor="@color/black" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="4dp"
                android:layout_weight="1"
                android:fontFamily="@font/mo_re"

                android:gravity="center"
                android:padding="4dp"
                android:text="X"
                android:textColor="@color/black"
                android:textSize="16sp" />


            <EditText
                android:id="@+id/breadth1"
                android:layout_width="wrap_content"
                android:layout_height="38dp"
                android:layout_marginLeft="4dp"
                android:layout_weight="1"
                android:background="@drawable/corner_bg_new"
                android:fontFamily="@font/mo_re"
                android:hint="Breadth"
                android:inputType="numberDecimal|numberSigned"
                android:padding="6dp"
                android:textColor="@color/black" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="12dp"

            android:fontFamily="@font/mo_re"
            android:padding="4dp"
            android:text="Area in Sq. ft"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/area"
                android:layout_width="match_parent"
                android:layout_height="38dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/bg_border_white"
                android:fontFamily="@font/mo_li"
                android:hint="Area"
                android:inputType="number"
                android:maxLength="10"
                android:padding="10dp" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="12dp"

            android:fontFamily="@font/mo_re"
            android:padding="4dp"
            android:text="Carpet in Sq. ft"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/carpetarea"
            android:layout_width="match_parent"
            android:layout_height="38dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/bg_border_white"
            android:fontFamily="@font/mo_li"
            android:hint="Carpet Area"
            android:inputType="number"
            android:maxLength="10"
            android:padding="10dp" />

The issue as I said is that the TextWatcher is only working on the 2nd layout and not on first one.

Screenshot:

enter image description here

0 Answers
Related