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

36,258

Solution 1

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);
                 }
            }
        });

     }
}

Solution 2

The problem is where you're comparing the RadioGroup's selected id... you'll want to change your onClick() to:

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 == R.id.poor) {
            total = aInt * 1.1;
            final String aString = Double.toString(total);
            resulttotal.setText(aString);
        }
        if (radioCheckedId == R.id.average) {
            total = aInt * 1.15;
            final String aString = Double.toString(total);
            resulttotal.setText(aString);
        }
        if (radioCheckedId == R.id.excellent) {
            total = aInt * 1.2;
            final String aString = Double.toString(total);
            resulttotal.setText(aString);
        }
    }
}    

onCheckedChanged() gives you will be the R.id for the view and not just a number which tells you which it is in sequence.

A few quick (unrelated) suggestions:

  • Use a switch statement instead of a bunch of if-statements.
  • Put something in there to check for -1 (nothing checked) too... just to be sure.
  • In the onClick() I usually check for which View was clicked by checking the incoming view's id. This just makes it where you don't have to keep everything stored and (IMHO) is a little more clear what you're talking about.

The above suggestions would look something like:

public void onClick(View v) {
    if (v.getId() == R.id.calculate) {
        String billtotal;
        double total = 0;
        billtotal = bill.getText().toString();
        final int aInt = Integer.parseInt(billtotal);
        switch(radioCheckedId) {
            case R.id.poor:
                total = aInt * 1.1;
                final String aString = Double.toString(total);
                resulttotal.setText(aString);
                break;
            case R.id.average:
                total = aInt * 1.15;
                final String aString = Double.toString(total);
                resulttotal.setText(aString);
                break;
            case R.id.excellent:
                total = aInt * 1.2;
                final String aString = Double.toString(total);
                resulttotal.setText(aString);
                break;
            default:
                // do something for when nothing is selected... maybe throw an error?
                break;
        }
    }
}

Lastly, if all you're doing in onCheckedChanged() is storing the value you could get rid of it all together and just check for it in the onClick(). Something like:

public void onClick(View v) {
    int radioCheckedId = radiogroup1.getCheckedRadioButtonId();
    if (v == calculate) {
        // ...

Unrelated, but another problem I noticed (and someone else mentioned)... if your EditTexts are listed in the XML layout then you'd need to get hooks to them like this (and not create new ones):

EditText bill        = (EditText) findViewById(R.id.bill       );
EditText resulttotal = (EditText) findViewById(R.id.resulttotal);

Also, you could probably just use a TextView instead of an EditView for the result if yo udon't need it to be editable.

Solution 3

I have some similar problem. I have a countdown in a radio group activity. When user clicks the next Button the radio group is checked to see if an option is selected. I implemented the button pressed at the end of the countdown, now i need to pass a checked radio Id to bypass the default user message of an option not selected.

case R.id.next:

        Log.d(" ID BOTAO",((java.lang.String) String).valueOf(rGroup3.getCheckedRadioButtonId()));

            if(rGroup3.getCheckedRadioButtonId()==-1){
                Context context = getApplicationContext();
                CharSequence text = "Please, select an option!";                    
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                break;

            }
Share:
36,258
MaQleod
Author by

MaQleod

I'm a Software Quality Principal Engineer that specializes in Networking and hardware (with a strong emphasis on Unix/Linux systems). My background is in Telecom and Network troubleshooting. I am proficient in both Ethernet and Infiniband standards. I code primarily with Python, Autoit, C and and SQL in my spare time, but I like to occasionally tinker in other languages. My degree is in Classical Numismatics and Archaeology. I also keep a blog on investing. profile for MaQleod on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/343e8ac1ebe84dacb26151af03317dcd.png

Updated on July 10, 2022

Comments

  • MaQleod
    MaQleod almost 2 years

    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.