0

I'm using an if statement to check which of two radio buttons in a radio group is selected but the app always thinks the second one is selected no matter what. I can't figure out why.

@TargetApi(24)
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.drawable.ic_launcher_carwash);
        getSupportActionBar().setDisplayUseLogoEnabled(true);

        final EditText washNumInput = (EditText)findViewById(R.id.txtWashNum);
        final RadioButton extOnly = (RadioButton)findViewById(R.id.radBtnExtOnly);
        final RadioButton extAndInt = (RadioButton)findViewById(R.id.radBtnExtAndInt);
        final Button btnCalc = (Button)findViewById(R.id.btnCalc);
        final TextView output = (TextView)findViewById(R.id.txtOutput);

        btnCalc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int washes = Integer.parseInt(washNumInput.getText().toString());
                double cost = 0.0;
                DecimalFormat currency = new DecimalFormat("$###,###.##");
                if (washes < 12){
                    Toast.makeText(MainActivity.this,"No discount for less than 12 washes",
                            Toast.LENGTH_LONG).show();
                    if (extOnly.isSelected()){
                        cost += washes * 10.99;
                        output.setText(currency.format(cost) + " for " + washes + " washes");
                    }
                    else{
                        cost += washes * 15.99;
                        output.setText(currency.format(cost) + " for " + washes + " washes");
                    }
                }
                else{
                    if (extOnly.isSelected()){
                        cost += washes * 8.99;
                        output.setText(currency.format(cost) + " for " + washes + " washes");
                    }
                    else{
                        cost += washes * 12.99;
                        output.setText(currency.format(cost) + " for " + washes + " washes");
                    }
                }
            }
        });
    }
}

Here is the XML file in case that helps:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.travis.carwashapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/car_wash_packages"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.032"
        android:id="@+id/textView2"
        app:layout_constraintHorizontal_bias="0.515" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/discount_params"
        android:textColor="@android:color/darker_gray"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.104" />

    <EditText
        android:id="@+id/txtWashNum"
        android:layout_width="358dp"
        android:layout_height="43dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="@string/washnumber"
        android:inputType="textPersonName"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.196" />

    <RadioGroup
        android:id="@+id/radGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:checkedButton="@+id/radBtnExtOnly"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.33">

        <RadioButton
            android:id="@+id/radBtnExtOnly"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:text="@string/exterior_only"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.257"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.306" />

        <RadioButton
            android:id="@+id/radBtnExtAndInt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:text="@string/exterior_with_interior_vacuum"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />
    </RadioGroup>

    <Button
        android:id="@+id/btnCalc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/calculate_package"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="320dp"
        android:layout_height="167dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.516"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/carwash"
        tools:layout_editor_absoluteY="328dp" />

    <TextView
        android:id="@+id/txtOutput"
        android:layout_width="377dp"
        android:layout_height="35dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_light"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.571"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.604" />

</android.support.constraint.ConstraintLayout>
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
MWTravesty
  • 13
  • 4

1 Answers1

0

Try using isChecked() and not isSelected(). The reason your code always assumes the second RadioButton is checked is because isSelected() returns false, constantly falling into the else case of your conditional statement; where you handle the second button!

This behaviour would be a little more obvious if you check the state of each button individually, using multiple if conditions rather than if/else; since the RadioGroup always guarantees only a single option can possibly be selected.

Mapsy
  • 4,192
  • 1
  • 37
  • 43
  • This worked, thank you very much. The subtle differences between the Java used with android studio and other IDEs like Eclipse can be very frustrating sometimes. – MWTravesty May 28 '17 at 02:27
  • @MWTravesty It's just one of those things. Don't let it get you down, it's a mistake you'll never make again. – Mapsy May 28 '17 at 07:03