Guru
Guru

Reputation: 186

Android - When I entered in EditText then value is not calculate in next EditText?

amntpaid EditText value should be add in total EditText.But when I am running the below code, the addition is not happening. Check my code.

String subtotal=String.valueOf(amntpaid);
String total =String.valueOf(totamnt);
total = total+subtotal;
String ncr=total+"";
totalamt.setText(ncr);

Upvotes: 1

Views: 276

Answers (5)

nobalG
nobalG

Reputation: 4620

Do something like this: do the sum of amntpaid and totamnt and store it in some int variable say var like

var=Integer.parseInt(amntpaid.getText())+Integer.parseInt(totamnt.getText());
String value_to_be_set=String.valueOf(var);
totalamnt.setText(value_to_be_set);

where totalamnt is the TextView in which you want to set the value,and amntpaid and totamntare your EditText from where you will get the entered input value.

Upvotes: 1

VVB
VVB

Reputation: 7641

You need to use either

Integer.parseInt(stringValue);

or

Integer.valueOf(stringValue);

for those edittexts.

Try below code :

int i = Integer.parseInt(et1.getText().toString().trim());

int j = Integer.parseInt(et2.getText().toString().trim());

int total = i + j;

String sum = total + "";

Upvotes: 0

Aniruddha
Aniruddha

Reputation: 4487

This is just a sample and you have to modify it accordingly.

TextView resultTV;
EditText ed1, ed2;
Button add;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_xml);

resultTV = (TextView) findViewById (R.id.your_tv_id)  // do the same for both edittexts and button


 add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

  String s1 = ed1.getText().toString();
  String s2 = ed2.getText().toString();
  if (!s1.trim().equals("") && !s2.trim().equals("")
  {
     int first = Integer.parseInt(s1);
     int second = Integer.parseInt(s2);
     int sum = first + second;
      resutTV.setText(sum + "");
  }
   else
   resultTV.setText("Please enter the values");
 }

    });

Upvotes: 1

SilentKiller
SilentKiller

Reputation: 6942

You are adding one string to another you need to TypeCast String data to Integer then only it will add

String subtotal = String.valueOf(amntpaid);
String total = String.valueOf(totamnt);
int sum = Integer.parseInt(total) + Integer.parseInt(subtotal);
totalamt.setText(sum + "");

Upvotes: 0

Tr4X
Tr4X

Reputation: 299

You're trying to add 2 strings, convert it to int first

String amount = amntpaid.getText().toString();
String total = totamnt.getText().toString();
int totalInt = Integer.parseInt(amount) + Integer.parseInt(total);
totalamt.setText("" + totalInt);

Upvotes: 0

Related Questions