0

i am creating a simple application that contain 3 buttons where each button have its listener that take the user input and do some math calculation than it display the result in toast .

but the third button display the result in second activity using intent .

the first buton work as it should but the second and third do not work . mean that the second do not show a toast and third do not display the result in the second activity .

can anyone help me ???

MainActivity.java

package com.tipcalc.assignment1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    EditText txt;
    Button btn1, btn2, btn3;

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

        txt = (EditText) findViewById(R.id.edittxt);
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        btn3 = (Button) findViewById(R.id.button3);

        btn1.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.main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        String input;
        String result = null;
        Double dbl;

        if (arg0 == btn1) {
            input = txt.getText().toString();
            dbl = Double.parseDouble(input) * 0.1;

            result = String.valueOf(dbl);


        } else if (arg0 == btn2) {

            input = txt.getText().toString();
            dbl = Double.parseDouble(input) * 0.15;

            result = String.valueOf(dbl);

        }
            Toast.makeText(this, result, Toast.LENGTH_LONG).show();
        if(arg0 == btn3){

            Intent intent = new Intent(MainActivity.this, SecondPage.class);
            intent.putExtra("com.tipcalc.assignment1.showResult", result);
            startActivity(intent);

        }

    }

}

SecondActivity.java

package com.tipcalc.assignment1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class SecondPage extends Activity {

    TextView txt_result;

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

        txt_result = (TextView) findViewById(R.id.txtResult);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String value = extras
                    .getString("com.tipcalc.assignment1.showResult");
            txt_result.setText(value);

        }

    }

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

}
user3006788
  • 165
  • 1
  • 14
  • 30
  • Since you are calling the same `onClick` for all `Button`s, you can set the `onClick` for each in xml which is easier and more readable, IMHO. You can see how [here in the Button Docs](http://developer.android.com/reference/android/widget/Button.html) – codeMagic Nov 19 '13 at 00:01
  • It might just be me, but if the process is different for the buttons, instead of `if`-ing `arg0` why not do `btnN.setOnClickListener(new OnClickListener() {...});` – ChiefTwoPencils Nov 19 '13 at 00:07

1 Answers1

1

You're currently only setting the OnClickListener on the first button. Change this:

    btn1.setOnClickListener(this);

to this:

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);

UPDATE (based on comment)

You are not sending the result to the next activity when you press btn3 because you are not processing the value in that block. If you follow the logic of your onClick handler in the case that arg0 == btn3, you'll see that result never gets set. I don't know what the purpose or expected result is, but you're calculating one value when clicking btn1, calculating another value when you click btn2, but you're not doing any calculation when clicking btn3

Rich
  • 36,270
  • 31
  • 115
  • 154
  • okk thank you it solve me the problem of the toast but still the third button do not display the result in the second activity – user3006788 Nov 19 '13 at 00:05