0

So I have a radio group with 2 radio buttons, when user select the first one it's going to display a text under the radio group, if user selects the second one it's going to display different message on the same area. It's really easy to do this with Toast but I don't want to do it with Toast message.

public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


    Button rb1 = (Button) getActivity().findViewById(R.id.radioButton1);
    Button rb2 = (Button) getActivity().findViewById(R.id.radioButton2);

    rb1.setOnClickListener(next_Listener);
    rb2.setOnClickListener(next_Listener);

}

private OnClickListener next_Listener = new OnClickListener() {

    public void onClick(View v) {

        RadioGroup radio_grp=(RadioGroup)getActivity().findViewById(R.id.radio_grp); 
        RadioButton rb1=(RadioButton)getActivity().findViewById(R.id.radioButton1);
        RadioButton rb2=(RadioButton)getActivity().findViewById(R.id.radioButton1);
        if(rb1.isChecked() == true) {

            // display text if they click 1st button

        }

        if (rb2.isChecked()) == true {

            // display text if they click 2nd button

        }
    }
}
Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85

3 Answers3

2

You need to define OnCheckedChangeListener for the radio button. Use it like this

rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                   textview.setText("Radio Button 1 is checked");
            }
        });



rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked)
                       textview.setText("Radio Button 2 is checked");
                }
            });
M.Waqas Pervez
  • 2,492
  • 2
  • 19
  • 33
1

Since RadioButton extends the Button class, you can implement a ClickListener.

You can also implement an onCheckedChangedListener(), but that would require that no value was set by default.

This is explained due to the fact that the onCheckedChangedListener() only triggers when you change between buttons and if one is already selected, you would have no trigger for that button.

Consider you have your TextView defined. You can do it with the following :

public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    TextView yourTextView = (TextView) findViewById(R.id.your_text_view);

     RadioGroup radio_grp= (RadioGroup)  getView().findViewById(R.id.radio_grp);
     RadioButton rb1 = (RadioButton)  getView().findViewById(R.id.radioButton1);
     RadioButton rb2 = (RadioButton)  getView().findViewById(R.id.radioButton1);

    View.OnClickListener button1Listener = new View.OnClickListener() {
        public void onClick(View v) {
            yourTextView.setText("you have touched button1");
        }
    };

    View.OnClickListener button2Listener = new View.OnClickListener() {
        public void onClick(View v) {
            yourTextView.setText("you have touched button2");
        }
    };
    rb1.setOnClickListener(button1Listener);
    rb2.setOnClickListener(button2Listener);
}

@EDIT

If the RadioGroup and respective RadioButtons are inside the Fragment layout, you should find their views using getView().findViewById() instead of getActivity().findViewById()

Change the code to this :

RadioGroup radio_grp= (RadioGroup)  getView().findViewById(R.id.radio_grp);
RadioButton rb1 = (RadioButton)  getView().findViewById(R.id.radioButton1);
RadioButton rb2 = (RadioButton)  getView().findViewById(R.id.radioButton2);
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
Ricardo Vieira
  • 1,738
  • 1
  • 18
  • 26
  • I keep getting java.lang.NullPointerException on `rb1.setOnClickListener(button1Listener); rb2.setOnClickListener(button2Listener);` – Ege Kuzubasioglu Aug 18 '16 at 08:53
  • Can you show me the error/Stacktrace? if you are getting a NullPointerException here rb1.setOnClickListener(button1Listener); , it means that rb1 is most likely null, which means that your RadioButton rb1 = (RadioButton) getActivity().findViewById(R.id.radioButton1); is returning nul.. Is radioButton1 the id of the button inside the layout? – Ricardo Vieira Aug 18 '16 at 09:02
  • There was a small detail I forgot to correct on your code. Check the Edit. Are the RadioGroup and respective RadioButtons inside the Fragment layout? – Ricardo Vieira Aug 18 '16 at 09:15
  • They are inside the layout, but I'd like to inform you, this is a fragment. – Ege Kuzubasioglu Aug 19 '16 at 08:11
  • Yes, I noticed that (You are using the onActivityCreated method which is available on the Fragment and not on the Activity). The thing is that you can update UI items on the Fragment and on the Activity from the Fragment and that is what I needed to know :) If getView() fixed it, means the views are inside the fragment's layout – Ricardo Vieira Aug 19 '16 at 10:01
0

try this

 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId == R.id.radioButton)
            {
               textField.setText("first text");

            }
            else 
             if (checkedId == R.id.radioButton2 ) {

                  textField.setText("second text");
            }
        }
    });
Don
  • 227
  • 1
  • 7