0

I'm a beginner in Android development and would appreciate your help on this one.

I have an android activity that creates a Checkbox and an EditText when the user clicks on a button. The user is free to add as many Checkboxes/EditTexts he likes.

This is the xml file of the view that is being added to the activity:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

            <requestFocus />
        </EditText>

    </LinearLayout>

</LinearLayout>

This is the java code :

public void onclickplus (View view){

        LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view1 = inflate.inflate(R.layout.row_checkitem, null);
        mainLayout.addView(view1);
        count = count +1;

}

My question is , how do we access these checkboxes and EditTexts added, in the actual java code so that i can save their data to my database.

If these objects where predetermined i would do for example :

EditText text1;
text1 = (EditText) findViewById(R.id.editText1);
text1.getText().toString();

and so on ,to access an editText's value.

In this case however , these objects are not there when the activity is launched.

Can you help me out? Thanks.

2 Answers2

0

You could look at this solution:

Add EditText(s) dynamically and retrieve values – Android

What they do is dynamically adding multiple EditText fields and save them in a list to access them later on. You just have to rewrite it a bit too also add checkboxes.

D. Wonnink
  • 306
  • 4
  • 19
  • Hello and thank you for your response. I have explored the solution that you linked, i can create an amount of EditTexts and access their values using the following code : EditText[] ed; ed = new EditText[count]; for (int i=0; i < count; i++){ ed[i] = new EditText(this); ed[i].setId(i); mainLayout.addView(ed[i]); } However, as i understand it , i can add only a selected amount of them as opposed to the user inflating the xml view and adding or removing as many he sees fit. Still looking for a way to access these checkboxes and editTexts added by inflating the xml view. – Panagiotis Moraitis Apr 12 '13 at 12:10
  • Why do you want that? If you look at the example they just add them to the layout with: `tableLayout.addView(createOneFullRow(rowId));`. Accessing the EditText fields (for example to get the text) can be done with `ed[i].getText().toString();` That was the question right? Dynamically adding and accessing fields. – D. Wonnink Apr 12 '13 at 12:42
  • I'll get back to you on that. Gonna work on it tonight and see how this works. Thanks. – Panagiotis Moraitis Apr 12 '13 at 13:33
  • Done , sorry for the delay – Panagiotis Moraitis Apr 18 '13 at 19:18
0

I've managed to achieve what i wanted to do thanks to D.Wonnik's help.

I abandoned the LayoutInflater method and instead used a TableLayout and TableRows.

This way the user can add TableRows containing a checkbox and an EditText by clicking on a "+" Button and he can also remove them as he sees fit by clicking on a "-" button. I'm also able to retrieve their values this way.

Here's the code :

LinearLayout mainLayout;
    TableLayout tablelayout;
    TableRow[] tableRow;
    int count;
    EditText[] ed;
    CheckBox[] cb;

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


        mainLayout = (LinearLayout)findViewById(R.id.LinearLayout1);
        tablelayout = new TableLayout(this);
        mainLayout.addView(tablelayout);
        ed = new EditText[10];
        cb = new CheckBox[10];
        tableRow = new TableRow[10];

    }

public void onclickplus (View view){


        ed[count] = new EditText(this);
        ed[count].setId(count);
        cb[count] = new CheckBox(this);
        cb[count].setId(count);
        tableRow[count] = new TableRow(this);

        tablelayout.addView(tableRow[count]);

        tableRow[count].addView(cb[count]);
        tableRow[count].addView(ed[count]);

        count = count +1;

}

public void onclickminus (View view){


        if (count>0){

            count = count -1;
            tablelayout.removeView(tableRow[count]);
    }

    }

Thank you D.Wonnik for pointing me in the right direction!