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

  • SEARCH
  • Home
  • 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 7532889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:35:01+00:00 2026-05-30T05:35:01+00:00

In main.xml I made a row containing a TextView, an EditText and a +

  • 0

In main.xml I made a row containing a TextView, an EditText and a “+” and “-” button.

Underneath that I made an “Add” button that will help you create a new row When you click the add button, you get an EditText and a Submit and Cancel button.

On “Submit” it outputs the EditText value to the TextView and creates the same row as the first one.

The numeric value “NewValueBox” should +1 when the “+” button is pressed.
But because I call it in another function it is not recognized by createNewAddButton() function in which the button is set up.

So in short:
“How do I change the value of NewValueBox when I click NewAddButton?”

Here’s the code:

package com.lars.MyApp;

import com.google.ads.*;
import com.lars.MyApp.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;

public class DrinkRecOrderActivity extends Activity {

    int currentValue1 = 0;
    int currentValueNew = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText firstValue = (EditText) findViewById(R.id.firstValue);
        Button valuePlus = (Button) findViewById(R.id.valuePlus);
        Button valueMinus = (Button) findViewById(R.id.valueMinus);

        final Button addValue = (Button) findViewById(R.id.add);

        final TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
        final LinearLayout addValueRow = (LinearLayout) findViewById(R.id.addValueRow);

        final EditText addNewValue = (EditText) findViewById(R.id.addNewValue);
        final Button submitNewValue = (Button) findViewById(R.id.submitNewValue);
        final Button cancelNewValue = (Button) findViewById(R.id.cancelNewValue);

        // BEGIN ONCLICKLISTENERS
        valuePlus.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                plusValue();
                firstValue.setText("" + currentValue1);
            }
        });

        valueMinus.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                minValue();
                firstValue.setText("" + currentValue1);
            }
        });     

        addValue.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                addValueRow.setVisibility(View.VISIBLE);
                addValue.setVisibility(View.GONE);
            }
        });

        cancelNewValue.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                addValueRow.setVisibility(View.GONE);
                addValue.setVisibility(View.VISIBLE);
            }
        });

        submitNewValue.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                tableLayout1.addView(createnewRow());
                addValueRow.setVisibility(View.GONE);
                addValue.setVisibility(View.VISIBLE);
                addNewValue.setText("");
            }
        });

        // END ONCLICKLISTENERS

        // Look up the AdView as a resource and load a request.
        AdView adView = (AdView) this.findViewById(R.id.adView);
        adView.loadAd(new AdRequest());
    }

    public TableRow createNewRow() {
        final TableRow newRow = new TableRow(this);
        final EditText addNewValue = (EditText) findViewById(R.id.addNewValue);
        newRow.addView(createNewTextView(addNewValue.getText().toString()));
        newRow.addView(createNewValueBox());
        newRow.addView(createNewAddButton());
        newRow.addView(createNewMinusButton());
        return newRow;
    }

    public TextView createNewTextView(String text) {
        final TextView textView = new TextView(this);
        textView.setText(text);
        return textView;
    }

    public EditText createNewValueBox() {
        EditText NewValueBox = new EditText(this);
        NewValueBox.setHint("0");
        NewValueBox.setInputType(InputType.TYPE_CLASS_NUMBER);
        return NewValueBox;
    }

    public Button createNewAddButton() {
        final Button NewAddButton = new Button(this);

        NewAddButton.setText("+");

        NewAddButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                plusNew();

                //NewValueBox.setText("" + currentValueNew);
            }

        });

        return NewAddButton;
    }

    public Button createNewMinusButton() {
        final Button NewMinusButton = new Button(this);
        NewMinusButton.setText("-");
        return NewMinusButton;
    }

    // BEGIN PLUS AND MIN FUNCTIONS

    public void plusNew() {
        if (currentValueNew <= 999) {
            currentValueNew = currentValueNew + 1;
        }
    }

    public void plusValue() {
        if (currentValue1 <= 999) {
            currentValue1 = currentValue1 + 1;
        }
    }

    public void minValue() {
        if (currentValue1 >= 1) {
            currentValue1 = currentValue1 - 1;
        }
    }

    // END PLUS AND MIN FUNCTIONS

}
  • 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-05-30T05:35:03+00:00Added an answer on May 30, 2026 at 5:35 am

    Add IDs for your Views so you can later reference them. Make 3 private static int field in your activity(the ID for NewValueBox, NewAddButton and NewMinusButton):

    private static int edt = 1;
    private static int add = 1001;
    private static int minus = 2001;
    

    Then in your createNewValueBox() method set the ID:

    NewValueBox.setId(edt);
    edt++;
    

    Do the same for the NewAddButton and the NewMinusButton:

    NewAddButton.setId(add);
    add++;
    

       NewMinusButton.setId(minus);
       minus++;
    

    Then in your listener for the buttons find out exactly which add button has been clicked and set the text in the corresponding EditText:

    NewAddButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    plusNew();
                    int tmp = v.getId();
                    EditText temp = (EditText) findViewById(1 + (tmp - 1001));
                    temp.setText("" + currentValueNew);
                }
    

    Kind of hackish method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I added some UI elements to the main.xml file in the res\layout folder and
I called my main activity Main, and Eclipse created Main.java and res/layout/main.xml for the
How can I change the font size in a ListView element? In my main.xml
I'm planning an XML web service that takes an XML request and returns an
I have an application structured as follows: dao domain main services utils I've made
I made a custom ant script to automatically create a jar file each time
I made a swf that interacts with other site on the internet (which has
I have a listView, a main xml file and a customListview xml file. for
I made a splash screen and used a runnable thread to parse a xml
I'm trying to follow this tutorial to made a Word doc template that i

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.