[Solved] How to get value from dynamically created edit text in Android?


Although, I didn’t get what exactly your question is, still I’ll try to help.
What exactly this code does is, on clicking the button on top an new layout(one that you wanted) will add to the activity screen at runtime, i.e an edittext with a button, and when you click on that button, A toast with the value of respective edittext will be shown. Hence solving the problem of getting the value of dynamically added edittext

MainActivity

public class MainActivity extends AppCompatActivity {
    Button generateET;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final LinearLayout myLinearLay = (LinearLayout) findViewById(R.id.dynamic);
        generateET = (Button) findViewById(R.id.generateBtn);
        generateET.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater l = getLayoutInflater();
                final View viewToAdd = l.inflate(R.layout.to_add, null);
                Button buttonWayPoints = (Button) viewToAdd.findViewById(R.id.btn_waypoint);
                final EditText editTextWayPoints = (EditText) viewToAdd.findViewById(R.id.et_waypoint);

                buttonWayPoints.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (editTextWayPoints.getText().toString() != null) {
                            Toast.makeText(MainActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "No text found", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                myLinearLay.addView(viewToAdd);

            }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.techmahindra.stackques.MainActivity">

    <Button
        android:id="@+id/generateBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="add editText To layout" />

    <ScrollView
        android:layout_below="@+id/generateBtn"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

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

</RelativeLayout>

to_add.xml(layout which will be inflated at runtime)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/et_waypoint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:hint="Way Points"
        android:padding="10dp"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn_waypoint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Find" />
</LinearLayout>

2

solved How to get value from dynamically created edit text in Android?