user1227670
user1227670

Reputation: 19

Setting the text in EditText dynamically on a button click

I have two EditTexts that have two different values and a button, on click of that button, result of the multiplication should be displayed in the third Edit Text.

Code Written for the function on the button click:

public void Multiply (View view)
{
String str1 = ed1.getText().toString();
        String str2 = ed2.getText().toString();

        int num1 = Integer.parseInt(str1);
        int num2 = Integer.parseInt(str2);

        int prod = num1*num2;

        //Toast.makeText(getBaseContext(), "Product is"+(num1*num2),Toast.LENGTH_LONG ).show();

        ed3.setText(prod);

}

Upvotes: 0

Views: 929

Answers (4)

chaitanya
chaitanya

Reputation: 1756

you can simple add ed3.setText(""+prod); as edittext directly not accept the integer value.

Upvotes: 0

null pointer
null pointer

Reputation: 5914

Use

ed3.setText(String.valueOf(prod));

or

ed3.setText(""+prod);

Upvotes: 1

vinothp
vinothp

Reputation: 10059

EditText won't allow integer to go directly in setText. So convert it into string before setting it

Try this

ed3.setText(String.valueOf(prod));

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

Mistake:

 ed3.setText(prod);

Try:

ed3.setText(String.valueOf(prod));

Upvotes: 2

Related Questions