Gaby
Gaby

Reputation: 1379

Add value to TextView?

I have a TextView where is set my budget. For example, 100$. When I press a button I introduce a new value, for example 40$, and I want to display in my TextView the addition of 100$ (initial value from TextView) and 40$(what I introduced). That's mean 140$.How can I do that?

Upvotes: 0

Views: 4432

Answers (4)

Houcine
Houcine

Reputation: 24181

double newValue = 40;
String txtOld = txtView.getText();
txtOld.replace("$","");
double oldValue = Double.parseDouble(txtOld);
txtView.setText((oldValue + newValue)+"$");

Upvotes: 2

TofferJ
TofferJ

Reputation: 4784

Create a variable to keep track of the current amount (without the currency symbol)

int totalAmount = 100;

then set it as the text in your TextView

textView.setText(totalAmount + "$");

when a new value is added simply add it to your total amount and put the new value in the TextView

totalAmount += 40;
textView.setText(totalAmount + "$");

that should do the trick :)

Upvotes: 0

David Rodrigues
David Rodrigues

Reputation: 12532

You need set an event for that, that is called on change value. After that, you need make a new int variable that receives only the value. I suggest you don't work with "$", but if it is need, you need cut out it. After, you add 100 to int, convert to String again and apply on TextView.

Upvotes: 0

salman khan
salman khan

Reputation:

you can use the settext property of the textview to set the new value.

Upvotes: 0

Related Questions