What code do I need to write to have my listener call out the checked Radio Button? Here's my code:
public class TabFragment1 extends Fragment {
RadioGroup radioGroup;
RadioButton radioButtonJa, radioButtonNee;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
radioGroup = (RadioGroup)v.findViewById(R.id.radioGroup);
radioButtonJa = (RadioButton)v.findViewById(R.id.radio_hoogteverschillen_ja);
radioButtonJa.setChecked(true);
radioButtonNee = (RadioButton)v.findViewById(R.id.radio_hoogteverschillen_nee);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup radioGroup, int checkedId)
{
boolean isChecked1 = radioButtonJa.isChecked();
boolean isChecked2 = radioButtonNee.isChecked();
if (isChecked1){
radioButtonJa.setChecked(true);
Toast.makeText(getActivity().getBaseContext(), "Ja", Toast.LENGTH_SHORT).show();
}
else if (isChecked2){
radioButtonNee.setChecked(true);
Toast.makeText(getActivity().getBaseContext(), "Nee", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getActivity().getBaseContext(), "Geen", Toast.LENGTH_SHORT).show();
}
}
});
}
}
When starting my app, it should create a toast but it doesn't. It only creates one when I first click on the other Radio Button. This was also the case with android:checked(true) in the layout file (instead of radioButtonJa.setChecked(true);). How can I get the toast to appear when I open my application?
int radioButtonID = radioGroup.getCheckedRadioButtonId();
View radioButton = radioGroup.findViewById(radioButtonID);
int idx = radioGroup.indexOfChild(radioButton);
RadioButton r = (RadioButton) radioGroup.getChildAt(idx);
String selectedtext = r.getText().toString();
Toast.makeText(getApplicationContext(), selectedtext, Toast.LENGTH_SHORT).show();
Put this code in your onCreateView() method just after setting up the variables for Radio Buttons.
This will do the trick.
Taken from here.
Related
I have a Fragment with Dynamic Radio group. When User Select any radio button. I am Passing value instead of radio button and load another thing with next button click from MainActivity. But If User not selected any radiobutton, MainActivity "NextButton" need to check Validation like Radiogroup to radiobutton ischeck() or not, If not Then need to show Toast.
Sorry for my Bad English.
Radio Grounp Code in Fragment:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton= group.findViewById(checkedId);
answer = radioButton.getText().toString();
// Toast.makeText(getActivity(), ""+answer, Toast.LENGTH_SHORT).show();
answerModel = new AnswerModel(id, question, answer);
Double referId = (Double) questionDataModel.getOptions().get(checkedId - 1).getReferTo();
nextId = referId.intValue();
// Toast.makeText(getActivity(), ""+referId, Toast.LENGTH_SHORT).show();
// Toast.makeText(getActivity(), ""+answerModel.getId()+"\n"+answerModel.getAnswer()+"\n"+answerModel.getQuestion(), Toast.LENGTH_SHORT).show();
sendDataInterface.sendata(nextId, answerModel);
}
});
Data Receive from MainActivity with NextButton:
I need to check Validation within this method actually
#Override
public void sendata(int data, AnswerModel answerModel) {
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
goToNextQuestion(data);
// Toast.makeText(MainActivity.this, ""+data, Toast.LENGTH_SHORT).show();
}
});
I have some code that generates in the activity some radiobuttons:
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
l1.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<drawables;i++){
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(current.getAnswers().get(i).getAns());
l1.addView(rd);
}
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
}
What I want to do is be able to verify which ones (radiobuttons) are checked when I click a button:
public void onAddAnswer(View v){
position++;
delete();
drawRAnswers(position);
}
For now, what this button does is only load the next set of radiobuttons in the view and delete the current ones, but I don't check which ones are selected. Do you know how I could do this in this onAddAnswer method? I would like to put the text of each selected radiobutton in a list.
Thanks.
I managed to figure this one out myself. Here is what I have done:
public class Activity extends AppCompatActivity {
RadioGroup currentGroup;
...
public void onAddAnswer(View v){
if (currentGroup.getCheckedRadioButtonId()==-1){
Toast.makeText(getApplicationContext(), "Veuillez sélectionner au moins une réponse",
Toast.LENGTH_LONG).show();
} else {
int selectedId = currentGroup.getCheckedRadioButtonId();
RadioButton selectedRadio = (RadioButton)findViewById(selectedId);
Toast.makeText(getApplicationContext(), selectedRadio.getText().toString()+" is selected",
Toast.LENGTH_SHORT).show();
position++;
delete();
updateData(position);
}
}
...
public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
RadioGroup l1 = new RadioGroup(this);
currentGroup = l1;
l1.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<drawables;i++){
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(current.getAnswers().get(i).getAns());
l1.addView(rd);
}
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
}
}
So basically what I am doing is have a RadioGroup for every time I draw radiobuttons, match it with the currentGroup and check if I have (or not) any radiobuttons selected. If yes, then I find out which one.
I have an Activity which implements both Checkbox.OnCheckedChangeListener and RadioGroup.OnCheckedChangeListener The groups are both dynamically generated and passed the same Activity as the listener.
However, when I click on a RadioGroup, the RadioGroup.OnCheckChangeListener is completely ignored but here's where it gets weird. It triggers CheckBox.OnCheckChangeListener! I tried casting the activity to the RadioGroup listener before passing it, but RadioGroup cannot even accept it, only the CheckBox listener gets through.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(subquestion.getQuestionType().equals("MULTIPLE_CHOICE_MULTI_ANSWER")) {
String selected = buttonView.getText().toString();
if (subquestion.getAnswer().getDataList() != null) {
Answer answer = subquestion.getAnswer();
ArrayList<String> checked = answer.getDataList();
if (checked.contains(selected)) {
checked.remove(selected);
Log.d("Checkbox" + buttonView.getId(), "unchecked");
} else {
checked.add(selected);
Log.d("Checkbox" + buttonView.getId(), "checked");
}
} else {
ArrayList<String> checked = new ArrayList<String>();
checked.add(selected);
subquestion.getAnswer().setDataList(checked);
Log.d("Checkbox" + buttonView.getId(), "checked");
}
}
if(subquestion.getQuestionType().equals("MULTIPLE_CHOICE_SINGLE_ANSWER")) {
RadioGroup group = buttonView; //CAN'T BE DONE
String selected = group.getFocusedChild().getId() + "";
subquestion.getAnswer().setData(selected);
Log.d("Radiobutton " + selected + group.getFocusedChild().getId(), "selected");
}
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//This does NOTHING, can't even be passed to the RadioGroup as a listener
}
How do I get the RadioGroup from the buttonView? I generate everything in code and it's different every time so there are no static id's to determine what's clicked. I pass an id to the radiobuttons to let me know which is which but I can't get to it like this.
It works perfectly for the checkboxes, it's just that Android seems to throw the wrong event and uses the wrong event handler for RadioGroup.
Thank you for reading!
Call this method for RadioGroup.OnCheckChangeListener and pass RadioGroup as argument
public void onRadioChangeListener(RadioGroup radioGroup){
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// write your code here
}
});
}
In my application, I need to programmatically set a radiobutton and expect the onCheckedChanged of a Radiogroup to be triggered so that I can do something in it.
Code to set a radiobutton programmatically.
LayoutInflater inflater = LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.footer, null);
RadioButton btn = (RadioButton)view.findViewById(R.id.footer_btn_course);
btn.setChecked(true);
And in another activity, I did something like:
public class MainActivity extends ActivityGroup implements RadioGroup.OnCheckedChangeListener{
protected void onCreate(Bundle savedInstanceState){
mRadioGroup = (RadioGroup)findViewById(R.id.footer_radio);
mRadioGroup.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId){
RadioButton btn = (RadioButton)findViewById(checkedId);
if(btn != null){
do_something();
}
}
}
However, looks like this way doesn't work, the onCheckedChanged was never called. Anyone know what I should do? Thanks.
To fire a radio check box for the default when initializing. Set everything to unchecked with the clearCheck method. Then, set the handler and finally set the default and your handler will fire.
itemtypeGroup.clearCheck();
then, same as usual add a listener:
mRadioGroup = (RadioGroup)findViewById(R.id.footer_radio);
mRadioGroup.setOnCheckedChangeListener(this);
mRadioGroup.clearCheck();
RadioButton btn = (RadioButton)view.findViewById(R.id.footer_btn_course);
btn.setChecked(true);
I am not sure for the issue, but it may be you miss refer your radio group id.
Try this instead to see the effect (inside your onCreate()):
RadioGroup rdGroup = (RadioGroup) findViewById(R.id.footer_radio);
rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton btn = (RadioButton)findViewById(checkedId);
if(btn != null){
do_something();
}
}
});
Or may be (RadioButton)findViewById(checkedId) return null.
Try Spinner if your case gets ambiguous.
Instead of btn.setChecked(true);
Used btn.performClick(); this will work.
I had the same problem..
and here is my approach:
class Myclass extends Activity{
protected void onCreate(Bundle savedInstanceState) {
radiogroup = (RadioGroup) findViewById(R.id.radiogroupDecisionUncertain);
radio1 = (RadioButton) findViewById(R.id.radiobuttonNo_U);
radio2 = (RadioButton) findViewById(R.id.radiobuttonYes_U);
// declare the onCheckChangedListener
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == radio1.getId()) { //do something
} else { // do other thing
}
}
});
radiogroup.check(radio1.getId());
}
}
Notice that i set the checked radio button after registering the listener..
Implement OnCheckedChangeListener instead of RadioGroup.onCheckedChangeListener that will work.
public class MainActivity extends ActivityGroup implements OnCheckedChangeListener{
.....
}
I am trying to use radio buttons but they aren't working. I've got the toast in there at the moment purely for debugging, and it never appears. There seems to be various ways to use them so perhaps I'm just using a poor method. Any advice on what I've done wrong would be amazing.
final RadioButton rbSDR = (RadioButton) findViewById(R.id.rbSDR);
final RadioButton rbMM = (RadioButton) findViewById(R.id.rbMM);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
//int checkedRadioButtonID = radGrp.getCheckedRadioButtonId();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int id) {
Toast.makeText(getBaseContext(), "Checked",
Toast.LENGTH_SHORT).show();
switch (id) {
case -1:
rbMM.setChecked(false);
rbSDR.setChecked(false);
break;
case R.id.rbSDR:
rbMM.setChecked(false);
break;
case R.id.rbMM:
rbSDR.setChecked(false);
break;
default:
break;
}
});
EDIT:
Apparently the issue was that I had a linear layout WITHIN the radio button group. Now to figure out how to put the buttons side by side...
the id will always return -1 only. Try the following edited code,
final RadioButton rbSDR = (RadioButton) findViewById(R.id.rbSDR);
rbSDR.setId(R.id.rbSDR);
final RadioButton rbMM = (RadioButton) findViewById(R.id.rbMM);
rbMM.setId(R.id.rbMM);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
//int checkedRadioButtonID = radGrp.getCheckedRadioButtonId();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int id) {
Toast.makeText(getBaseContext(), "Checked",
Toast.LENGTH_SHORT).show();
switch (id) {
case -1:
rbMM.setChecked(false);
rbSDR.setChecked(false);
break;
case R.id.rbSDR:
rbMM.setChecked(false);
break;
case R.id.rbMM:
rbSDR.setChecked(false);
break;
default:
break;
}
});
set the listener to radio buttons instead of radio group
Instead of using
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
Use
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
To put the radio buttons side-by-side, add this to the XML properties:
android:orientation="horizontal"