0

I have radio buttons in a radio group. I am new to java and figured out a way from stack overflow to use switch case with radio buttons.. But the problem is that where i have found the switch case code has a method show() in it for toasts...

public void onRadioButtonClicked(View view) {
    //Is the button checked?
    boolean checked = ((RadioButton) view).isChecked();

    //Check which radio button is clicked
    switch(view.getId()) {
        case R.id.Omaths:
            if (checked)
                Toast.makeText(ChatFragment.this," O grade",Toast.LENGTH_LONG.show());
                break;
    }
}

Idk how to deal with that... Can anybody give me the show method... If you need reference... The XML code is below...

<RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="7"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/Omaths"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="O"
                android:onClick="onRadioButtonClicked"/>

            <RadioButton
                android:id="@+id/Aplusmaths"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="A+"
                android:onClick="onRadioButtonClicked"/>
</RadioGroup>  

Any help will be really appreciated...

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Azzler
  • 7
  • 5
  • 1
    you mean `Toast.makeText(ChatFragment.this," O grade",Toast.LENGTH_LONG).show();` – Pavneet_Singh Jul 22 '17 at 15:35
  • yea.. The show method in this... – Azzler Jul 22 '17 at 15:38
  • What do radio buttons have to do with your typo? – OneCricketeer Jul 22 '17 at 15:38
  • By the way, RadioGroup has its own listener method for the buttons, so I don't think android:onClick is the best use case here – OneCricketeer Jul 22 '17 at 15:39
  • Actually its an SGPA app.. that calculates the credit score of the grades in all subjects... Radio buttons takes the grade input and then the grade will be multiplied by the subject credit... i know i could have used text input but.. I want to use radio buttons. by the way the app has no future. its just for my classmates. so for now i only need the show method!! Thanks... – Azzler Jul 22 '17 at 15:43
  • I'm not disapproving the usage of the radio buttons themselves, but your code doesn’t compile because of a typo on the Toast, not the radio buttons – OneCricketeer Jul 22 '17 at 15:47
  • oh!! By the way i got answered and the code is now working fine... Thanks for helping... :D – Azzler Jul 22 '17 at 15:49
  • Regarding my previous comment: you should detect when you are clicking within the group, not an individual button https://stackoverflow.com/questions/6780981/android-radiogroup-how-to-configure-the-event-listener – OneCricketeer Jul 22 '17 at 15:50
  • sorry to say but still i am not getting your point... Do you mean that i should isChecked() for radio group instead of buttons?? – Azzler Jul 22 '17 at 15:59

1 Answers1

0

The show() method is called outside to show the message,

syntax:

 Toast.makeText(ProjectActivity.this,"Your message here",Toast.LENGTH_LONG
 ).show();

In your case,

 Toast.makeText(ChatFragment.this," O grade",Toast.LENGTH_LONG).show();
Popeye
  • 253
  • 4
  • 13