javaandroidxmladdtextchangedlistener

Use addTextChangedListener without knowing EditText Id


I have set up my app to dynamically add table rows from a separate XML layout, each row containing an ImageButton to delete each row dynamically. Each row contains a TextView, two EditText's, and the ImageButton. I used the line android:onClick="onClick"in the XML layout for the ImageButton, which eliminated the need to try to keep track of the Id for each dynamic row. I am now facing a new problem, without knowing any Id's, how can I implement a TextChangedListener for both of the TextEdit's in each row? To my knowledge, XML does not contain a TextChangedListener, so this will likely have to be done completely in Java. Setting up some kind of array to hold each row might work, but I wanted to see what else could be done. My code is below:

Separate XML layout (calculator_layout_table)

 <?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/newRow">

        <EditText
            android:id="@+id/category_name3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Category Name" />

        <EditText
            android:id="@+id/editTextNumber3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:ems="10"
            android:hint="@string/enter_a_percent"
            android:inputType="number"
            android:textAlignment="center" />

        <TextView
            android:id="@+id/result3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="3"
            android:text="$0"
            android:textAlignment="center" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="4"
            android:backgroundTint="#00FFFFFF"
            android:scaleX=".7"
            android:scaleY=".7"
            android:src="@android:drawable/btn_dialog"
            android:onClick="onClick"/>
    </TableRow>

MainActivity (Java)

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onClick(View v) {

        View row = (View) v.getParent();
        ViewGroup container = ((ViewGroup)row.getParent());
        container.removeView(row);
        container.invalidate();
    }
}

SecondFragment (Java)

public class SecondFragment extends Fragment {

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        return inflater.inflate(R.layout.fragment_second, container, false);
    }
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.add_row_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createRow();
            }

        });
    }

    private void createRow(){

        TableLayout bottomTable = getActivity().findViewById (R.id.bottomTable);
        TableRow newRow = (TableRow) getLayoutInflater().inflate (R.layout.calculator_table_row, null, false);
        bottomTable.addView(newRow);
    }

}

Solution

  • You could add a text watcher for each EditText when you inflate a new TableRow. You just need to find these EditTexts in your newly created TableRow and add a text watcher for them. If you want to add a text watcher in the XML layout file, you could use data binding library. Here is an analogous question. As far as I know, there is no way you can add a text watcher in the XML layout in the same way you added the onClick listener except for data binding.