Uzair Qaiser
Uzair Qaiser

Reputation: 156

if else condition with radio button not working on button click in android

I am stuck in simple code. I am trying to check which Radiobutton is checked and then showing results on Button click. When i click Male Radio Button, it shows results perfectly But when i check female radio button then its showing nothing or you can say its not working and even not showing any error. I don't know whats wrong with my code. Please take a look:

RadioButton radm,radf;
radm=(RadioButton)findViewById(R.id.radiomale);
radf=(RadioButton)findViewById(R.id.radiofemale);
Button find=(Button) findViewById(R.id.btnfind);

//Where s is Spinner and find is Button

   find.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

if(s.getSelectedItem().toString().equals("KG")){

                    if(radm.isChecked()){


                        int inches= myfoot*12+myinch;

                        float sizeincm= inches*2.54f;
                        mybmr= (float) (66.47f+ (13.7*myweight) +(5*sizeincm) -6.8 * myage);
                        myrmr= (float) ((10*myweight)+(6.25*sizeincm)-(5*myage)+5);
                        mygcal=level.getSelectedItem().toString();

                        Intent intent = new Intent(MainActivity.this, Output_activity.class);
                        Bundle extras = new Bundle();

                        extras.putFloat("bmr",mybmr);
                        extras.putFloat("rmr",myrmr);
                        extras.putString("gcal",mygcal);
                        intent.putExtras(extras);
                        startActivity(intent);


                    }
                }



                   else if(s.getSelectedItem().toString().equals("KG")){
                   if(radf.isChecked()) {

                            int inches = myfoot * 12 + myinch;

                            float sizeincm = inches * 2.54f;
                            mybmr = (float) (655.1f + (9.6 * myweight) + (1.8 * sizeincm) - 4.7 * myage);
                            myrmr = (float) ((10 * myweight) + (6.25 * sizeincm) - (5 * myage) + 5);
                            mygcal = level.getSelectedItem().toString();

                            Intent intent = new Intent(MainActivity.this, Output_activity.class);
                            Bundle extras = new Bundle();

                            extras.putFloat("bmr", mybmr);
                            extras.putFloat("rmr", myrmr);
                            extras.putString("gcal", mygcal);
                            intent.putExtras(extras);
                            startActivity(intent);


                        }
                }
     }
    });

Here is my XML:

  <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radmf"
        android:padding="4dp"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:text="Male"
            android:checked="true"
            android:background="@drawable/customedittext"
            android:id="@+id/radiomale"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"/>


        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:text="Female"
            android:background="@drawable/customedittext"
            android:id="@+id/radiofemale"/>


    </RadioGroup>

Upvotes: 1

Views: 1045

Answers (2)

Uzair Qaiser
Uzair Qaiser

Reputation: 156

I Solved my Problem Like this:

  if(s.getSelectedItem().toString().equals("KG") && radm.isChecked()){




                        int inches= myfoot*12+myinch;

                        float sizeincm= inches*2.54f;
                        mybmr= (float) (66.47f+ (13.7*myweight) +(5*sizeincm) -6.8 * myage);
                        myrmr= (float) ((10*myweight)+(6.25*sizeincm)-(5*myage)+5);
                        mygcal=level.getSelectedItem().toString();

                        Intent intent = new Intent(MainActivity.this, Output_activity.class);
                        Bundle extras = new Bundle();

                        extras.putFloat("bmr",mybmr);
                        extras.putFloat("rmr",myrmr);
                        extras.putString("gcal",mygcal);
                        intent.putExtras(extras);
                        startActivity(intent);


                }



                  else if(s.getSelectedItem().toString().equals("KG") && radf.isChecked()){



                            int inches = myfoot * 12 + myinch;

                            float sizeincm = inches * 2.54f;
                            mybmr = (float) (655.1f + (9.6 * myweight) + (1.8 * sizeincm) - 4.7 * myage);
                            myrmr = (float) ((10 * myweight) + (6.25 * sizeincm) - (5 * myage) + 5);
                            mygcal = level.getSelectedItem().toString();

                            Intent intent = new Intent(MainActivity.this, Output_activity.class);
                            Bundle extras = new Bundle();

                            extras.putFloat("bmr", mybmr);
                            extras.putFloat("rmr", myrmr);
                            extras.putString("gcal", mygcal);
                            intent.putExtras(extras);
                            startActivity(intent);



                }

Still thankful to Anders for your time. Thanks

Upvotes: 1

Anders Duus
Anders Duus

Reputation: 426

Like this:

RadioButton radm,radf;
radm=(RadioButton)findViewById(R.id.radiomale);
radf=(RadioButton)findViewById(R.id.radiofemale);
Button find=(Button) findViewById(R.id.btnfind);

//Where s is Spinner and find is Button

find.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(s.getSelectedItem().toString().equals("KG")){

            if(radm.isChecked()){
                int inches= myfoot*12+myinch;

                float sizeincm= inches*2.54f;
                mybmr= (float) (66.47f+ (13.7*myweight) +(5*sizeincm) -6.8 * myage);
                myrmr= (float) ((10*myweight)+(6.25*sizeincm)-(5*myage)+5);
                mygcal=level.getSelectedItem().toString();

                Intent intent = new Intent(MainActivity.this, Output_activity.class);
                Bundle extras = new Bundle();

                extras.putFloat("bmr",mybmr);
                extras.putFloat("rmr",myrmr);
                extras.putString("gcal",mygcal);
                intent.putExtras(extras);
                startActivity(intent);


            }else if (radf.isChecked()){
                    int inches = myfoot * 12 + myinch;

                    float sizeincm = inches * 2.54f;
                    mybmr = (float) (655.1f + (9.6 * myweight) + (1.8 * sizeincm) - 4.7 * myage);
                    myrmr = (float) ((10 * myweight) + (6.25 * sizeincm) - (5 * myage) + 5);
                    mygcal = level.getSelectedItem().toString();

                    Intent intent = new Intent(MainActivity.this, Output_activity.class);
                    Bundle extras = new Bundle();

                    extras.putFloat("bmr", mybmr);
                    extras.putFloat("rmr", myrmr);
                    extras.putString("gcal", mygcal);
                    intent.putExtras(extras);
                    startActivity(intent);
            }
        }
    }
});

Upvotes: 0

Related Questions