Android - register button click and take action based on radio selection

android, button, radio-button

Solution

import java.text.NumberFormat;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.view.View;

public class TipCalc extends Activity
{
    TextView result;
    RadioGroup radiogroup1;
    RadioButton r1,r2,r3;
    Button calculate;
    EditText bill, resulttotal;
    Locale currentLocale = Locale.getDefault();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        radiogroup1 = (RadioGroup) findViewById(R.id.radiogroup1);
        final Button calculate = (Button) findViewById(R.id.calculate); 
        final RadioButton r1 = (RadioButton) findViewById(R.id.poor);
        final RadioButton r2 = (RadioButton) findViewById(R.id.average);
        final RadioButton r3 = (RadioButton) findViewById(R.id.excellent);
        final EditText bill = (EditText) findViewById(R.id.bill);
        final EditText tiptotal = (EditText) findViewById(R.id.tiptotal);
        final EditText resulttotal = (EditText) findViewById(R.id.resulttotal);
        bill.setText("0.00");
        tiptotal.setText("0.00");
        resulttotal.setText("0.00");
        calculate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) throws  NumberFormatException {
                if (v == calculate)
                {
                NumberFormat currencyFormatter;
                currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
                double atotal = 0;
                    double btotal = 0;
                    String billtotal = bill.getText().toString();
                    Double aDbl = 0.00;
                    try
                    {
                        aDbl = Double.parseDouble(billtotal);
                    }
                    catch(NumberFormatException n)
                    {
                        aDbl = 0.00;
                    }
                    if (r1.isChecked())
                     {
                        atotal = aDbl * 1.1;
                        btotal = aDbl * 0.1;
                     }
                    if (r2.isChecked())
                     {
                        atotal = aDbl * 1.15;
                        btotal = aDbl * 0.15;
                    }
                    if (r3.isChecked())
                    {
                        atotal = aDbl * 1.2;
                        btotal = aDbl * 0.2;
                    }
                    final String bString = currencyFormatter.format(btotal);
                    tiptotal.setText(bString);
                    final String aString = currencyFormatter.format(atotal);
                    resulttotal.setText(aString);
                 }
            }
        });

     }
}

Problem

I'm trying to teach myself how to write android apps and I'm having trouble registering a button click and taking actions based on which radio button is selected at the time. This is a simple tip calculator: ``` import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; import android.widget.RadioGroup; import android.view.View; public class TipCalc extends Activity implements RadioGroup.OnCheckedChangeListener,View.OnClickListener { TextView result; RadioGroup radiogroup1; RadioButton r1,r2,r3; Button calculate; EditText bill, resulttotal; private int radioCheckedId = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); radiogroup1 = (RadioGroup) findViewById(R.id.radiogroup1); Button calculate = (Button) findViewById(R.id.calculate); RadioButton r1 = (RadioButton) findViewById(R.id.poor); RadioButton r2 = (RadioButton) findViewById(R.id.average); RadioButton r3 = (RadioButton) findViewById(R.id.excellent); EditText bill = new EditText(this); EditText resulttotal = new EditText(this); radiogroup1.setOnCheckedChangeListener(this); calculate.setOnClickListener(this); //bill.setText("0"); //resulttotal.setText("0"); } public void onCheckedChanged(RadioGroup group, int checkedId) { radioCheckedId = checkedId; } public void onClick(View v) { if (v == calculate) { String billtotal; double total = 0; billtotal = bill.getText().toString(); final int aInt = Integer.parseInt(billtotal); if (radioCheckedId == 1) { total = aInt * 1.1; final String aString = Double.toString(total); resulttotal.setText(aString); } if (radioCheckedId == 2) { total = aInt * 1.15; final String aString = Double.toString(total); resulttotal.setText(aString); } if (radioCheckedId == 3) { total = aInt * 1.2; final String aString = Double.toString(total); resulttotal.setText(aString); } } } } ``` Everything loads just fine, but nothing happens when I press the calculate button in the virtual phone.

Original source