Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8755843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:56:03+00:00 2026-06-13T13:56:03+00:00

I have a Button that adds a row with an EditText to a TableLayout

  • 0

I have a Button that adds a row with an EditText to a TableLayout. I need to find out the input in each EditText of the rows that are added.

For example: Someone adds 3 rows, meaning there are 3 EditText. In each EditText, they put the number 3. I need to store the values of the EditText that were added to the layout and add them all up by the click of a button.

Would I use a list then iterate that list? I’m not sure about how I would do this.

This is my code so far…

int count = 1;

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

    Button buttonAdd = (Button) findViewById(R.id.button1);
    Button buttonDel = (Button) findViewById(R.id.button2);
    Button buttonCalc = (Button) findViewById(R.id.button3);
    buttonAdd.setOnClickListener(this);
    buttonDel.setOnClickListener(this);
}

public void onClick(View v) {
    TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
    List<EditText> allEd = new ArrayList<EditText>();

    switch(v.getId()){
        case R.id.button1:
            if(count != 16){
                count++;

                // Create the row only when the add button is clicked
                TableRow tempRow = new TableRow(MainActivity.this);
                EditText tempText1 = new EditText(MainActivity.this);
                EditText tempText2 = new EditText(MainActivity.this);
                TextView tempTextView = new TextView(MainActivity.this);
                EditText editText1 = (EditText) findViewById(R.id.editText1);
                EditText editText2 = (EditText) findViewById(R.id.editText2);
                TextView textView3 = (TextView) findViewById(R.id.textView3);
                tempTextView.setText(count + ".");

                tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                tempText1.setLayoutParams(editText1.getLayoutParams());
                tempText2.setLayoutParams(editText2.getLayoutParams());
                tempTextView.setLayoutParams(textView3.getLayoutParams());
                tempText1.setInputType(InputType.TYPE_CLASS_TEXT);
                tempText2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                tempText2.setId(count);
                allEd.add(tempText2);

                tempRow.addView(tempTextView);
                tempRow.addView(tempText1);
                tempRow.addView(tempText2);
                tableLayout1.addView(tempRow);
            } 
            else {
                final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
                alertDialog.setTitle("Error");
                alertDialog.setMessage("You can only have 10 rows!");

                alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });

                alertDialog.show();
            }
            break;
        case R.id.button2:
            if(count != 1){
                count--;
                tableLayout1.removeView(tableLayout1.getChildAt(count));
        } 
            else {
                final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
                alertDialog.setTitle("Error");
                alertDialog.setMessage("You must have at least one row!");

                alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });

                alertDialog.show();
            }
            break;
        case R.id.button3:
            String[] strings = new String[allEd.size()];
            for(int i = 0; i < allEd.size(); i++) {
                strings[i] = allEd.get(i).getText().toString();
                int input = Integer.parseInt(strings[i]);

                final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
                alertDialog.setTitle("Your calculated GPA");
                alertDialog.setMessage("Your calculated GPA is: " + input/count);
                alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });

                alertDialog.show();
            }
        break;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:56:05+00:00Added an answer on June 13, 2026 at 1:56 pm

    When you create each of the EditTexts, you need to to add them to an array-like container, such as an ArrayList<EditText>. Whenever you need to access any of these dynamic EditTexts, you can retrieve them from the ArrayList.

    I can see that you are already creating an ArrayList<EditText> in your onClick() method. That’s great, but you need to move the ArrayList outside the onClick() method so that you can reference it from any other methods you create. So move this line…

    List<EditText> allEd = new ArrayList<EditText>();
    

    outside the method, so that the beginning few lines of onClick() now look like this…

    List<EditText> allEd = new ArrayList<EditText>();
    
    public void onClick(View v) {
        TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
    
        switch(v.getId()){
        case R.id.button1:
    

    Also, make sure you’re adding all the EditTexts to the ArrayList – I see there is a line allEd.add(tempText2); but I can’t see any line for allEd.add(tempText1); – if you need to access tempText1, make sure you add it to the list.

    To perform the ‘adding’ caluclation, you need to loop over the entries in the ArrayList, get their values, then add them together. Something like this…

    case R.id.button3:
        // calculate the value first
        int calculation = 0;
        for (int i=0;i<allEd.size();i++){
            // get the entry
            EditText textField = addEd.get(i);
    
            try {
                // get the value, as a number
                int numberValue = Integer.parseInt(textField.getText());
                // add it to the calculation
                calculation += numberValue;
            } 
            catch (Exception e){
                // ignore errors if the input is empty or isn't a number
            }
        }
    
        // now display the result
        final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Your calculated GPA");
        alertDialog.setMessage("Your calculated GPA is: " + input/count);
        alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });
    
        alertDialog.show();
    break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button on my page which adds row by row on each
I have some Javascript that dynamically adds rows/fields when you click the button, Add
I have a button that adds a new row to the view with the
I have a button that, when pressed, will add a new HTML row. Within
I have some JavaScript that creates a row in a table and adds cells
I have a table which has a button to Add Rows. This button adds
I have a button that runs a method. The method gets the selected Rows
I have a button which adds new row in grid view at button click
In my dgv I have a DataGridViewButtonColumn which adds a delete row button to
I have a form and a add button that adds the same form fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.