Calculate with data from 2 textedit and show result in textview

594 views Asked by At

I want to calculate a value from the data in 2 text edits and show the result in a textview. How can I do it?

BMI calculator:

private TextView deinbmi;
    private EditText weight;
    private EditText height;

    public void calculator ()
    {
        double ergebnis;
        weight=(EditText)findViewById(R.id.text_calculator_weight);
        height=(EditText)findViewById(R.id.text_calculator_height);
        double a=Double.parseDouble(weight.getText().toString());
        double b=Double.parseDouble(height.getText().toString());
        ergebnis=a*b;
        deinbmi.setText(""+ergebnis);
    }

I don't have an idea; I just started with Java. I can program with C++ but just console.

If I search on the internet for this problem, everyone has another version of solving this, but none of these answers worked for me?

2

There are 2 answers

0
Sevenhero On
public class calculator extends ActionBarActivity {

private TextView deinbmi;
private EditText weight;
private EditText height;

public void calculator ()
{
    double ergebnis;
    weight=(EditText)findViewById(R.id.text_calculator_weight);
    height=(EditText)findViewById(R.id.text_calculator_height);
    double a=Double.parseDouble(weight.getText().toString());
    double b=Double.parseDouble(height.getText().toString());
    ergebnis=a*b;
    deinbmi.setText(""+ergebnis);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculator);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_calculator, menu);
    //rechnen ?!
    Button credits_calculate = (Button)    
   findViewById(R.id.button_calculate);
    credits_calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        calculator();
        }
    });
    //back
    Button credits_back = (Button)  

  findViewById(R.id.button_calculator_back);
    credits_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            finish();
        }
    });
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

       //noinspection SimplifiableIfStatement
       if (id == R.id.action_settings) {
              return true;
       }

        return super.onOptionsItemSelected(item);
    }
}

is my activity

i tryed to use your code but

    Answer=(Button)findViewById(R.id.answerbuttton);
    Answer.SetOnclicklistener(this);

he didnt understand this

0
Android Dev On

Try This Working code.

you Missing the Textview Inside oncreate, Then Add a Button for Getting the Answer

public class Mainactivity extends Activity implements OnClickListener
{
    private TextView deinbmi;
        private EditText weight;
        private EditText height;
    Button Answer;

    @Override
        protected void onCreate(Bundle savedInstanceState) 
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.page1_landing);

          weight=(EditText)findViewById(R.id.text_calculator_weight);
            height=(EditText)findViewById(R.id.text_calculator_height);
           deinbmi=(Textview)findViewById(R.id.text);

    Answer=(Button)findViewById(R.id.answerbuttton);
    Answer.SetOnclicklistener(this);
    }

    @Override
        public void onClick(View v) {

            switch (v.getId()) {

            case R.id.answerbuttton:

            calculator ();
                break;
}}
        public void calculator ()
        {
            double ergebnis;

            double a=Double.parseDouble(weight.getText().toString());
            double b=Double.parseDouble(height.getText().toString());
            ergebnis=a*b;
            deinbmi.setText(""+ergebnis);
        }
}