Ryan159
Ryan159

Reputation: 109

Trying to Multiply in Android

I'm trying to make a simple converter that multiplies a value entered by 2.2 but i am having problems getting the result to a text view. The error I am getting says 'cannot resolve method setText(Double). any advice?

public void onClick(View v) {
    double result =0;
    double a = Double.parseDouble(weightEntered.getText().toString().trim());
    double b = 2.2;
    if(v == button_convert)
    {
        if(lbs_kg.isChecked() == true)
            result = (a * b);
            t1.setText(result);
        if(kg_lbs.isChecked() == true)
            t1.setText("Selected is : "+kg_lbs.getText());
    }

Upvotes: 1

Views: 76

Answers (3)

Oleksandr Berdnikov
Oleksandr Berdnikov

Reputation: 679

result = (a * b);
t1.setText(String.format("%.2f", result))

Using String.format() allows you to control the precision for the output value.

Upvotes: 1

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

Convert your double to a String.

This is important, because setText() exists in multiple overloaded signatures.

The signature that you are using in your question is using a String Resource ID! You need to firstly turn that number into a String so that the TextView knows how to use it properly.

String str = String.valueOf(result);
t1.setText(str);

Upvotes: 4

Parsania Hardik
Parsania Hardik

Reputation: 4623

You need to convert your double value to string because setText() can set only text value to it's view. update this line

  t1.setText(result);

to t1.setText(String.valueOf(result));

Upvotes: 0

Related Questions