Aitazaz Khan
Aitazaz Khan

Reputation: 1605

I am making a calculator in android but i am getting error in adding two values

I have 2 edit text field where i am getting values from user than i have four buttons add multiply subtract and divide when user give the values and press one of the button the program will perform specific task which i have assign to it and it will give the result in text view but i am getting error in adding two values. Here is my effort.

package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
TextView textView1;
Button button1,sub,mul,divi;
EditText editText1,editText2;
double a,b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
button1=(Button)findViewById(R.id.button1);
sub=(Button)findViewById(R.id.sub);
mul=(Button)findViewById(R.id.mul);
divi=(Button)findViewById(R.id.divi);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        a=Double.parseDouble(editText1.getText().toString());
        b=Double.parseDouble(editText1.getText().toString());
        if(arg0.getId()==R.id.button1){
            textView1.setText(""+ a+b);
        }
        else{textView1.setText("not working ");}
        }

}

its my application

Upvotes: 0

Views: 348

Answers (4)

syed shah
syed shah

Reputation: 678

I have edited the code this will work 100%.

    package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
TextView textView1;
Button button1,sub,mul,divi;
EditText editText1,editText2;
double a,b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
button1=(Button)findViewById(R.id.button1);
sub=(Button)findViewById(R.id.sub);
mul=(Button)findViewById(R.id.mul);
divi=(Button)findViewById(R.id.divi);
button1.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        a=Double.parseDouble(editText1.getText().toString());
        b=Double.parseDouble(editText1.getText().toString());
        if(arg0.getId()==R.id.button1){
            textView1.setText(""+(a+b));
        }
        else{textView1.setText("not working ");}
        }

}

Upvotes: 3

Tarsem Singh
Tarsem Singh

Reputation: 14199

First

You are missing Parenthesis,

Here textView1.setText(""+ a+b); + operator is acting as concatenation

So change your code to

textView1.setText(""+ (a+b));

PS. Second :

I think you are also forgetting to Register the onClickListener to button1=(Button)findViewById(R.id.button1);

So use this button1.setOnClickListener(this) in your onCreate()

Upvotes: 4

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

Put android:inputType="number" in your Edittext of xml file.

Upvotes: 0

Szymon
Szymon

Reputation: 43023

Try textView1.setText(""+ (Double)(a+b));

Upvotes: 3

Related Questions