Search code examples
androiddynamicandroid-edittextlayout-inflater

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


How to get value from dynamically created EditText in Android?

This is my dynamic view (XML file)

 <LinearLayout
        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:background="@drawable/background"
            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> 

And I am using LayoutInflater because I want perfect Designed layout which is not accomplish by me via setLayoutParams so this is the reason I am using LayoutInflater.

And here is my code:

  LayoutInflater l = getLayoutInflater();
                    final LinearLayout mainActivity = (LinearLayout) findViewById(R.id.dynamic);
                    final View view = l.inflate(R.layout.dynamic_layout_waypoints, null);
                    buttonWayPoints = (Button) view.findViewById(R.id.btn_waypoint);
                    editTextWayPoints = (EditText) view.findViewById(R.id.et_waypoint);
 mainActivity.addView(editTextWayPoints);

I am new to Android. Most of people suggest this solution but it's not worked for me, the issue is when I implement this code my ADD NEW EDIT TEXT BUTTON not respond me.

This is how I am trying to get inserted value but it returns value of only last created edit text:

public Cursor getPerson(String Query) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(Query, null);
        return c;
    }

sb is StringBuilder sb;, w is String w;

String queryWayPoints = "select * from route_table where trip_id = " + t;
            Cursor s = myDb.getPerson(queryWayPoints);
            int count3 = s.getCount();
            for (int j = 0; j < count3; j++) {
                s.moveToNext();
                w = s.getString(s.getColumnIndex("waypoints"));
                // se.append(w + "." + "00");
            }
            trip_name.setText(n);
            trip_date.setText(d);
            sb.append(w);
        }
        trip_route.setText(sb.toString());

Solution

  • 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>