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();
}
});
Related
I am trying all day to understand, why my method setOnChangeListener is never called...it works well until the first Toast...The second toast never appear. What is wrong? Should i pass someway the view to the onCheckedChangeListener? I have activity, where user can click on button, then he get alertdialog, with 3 radio buttons and radiogroup inside. I want to get, what radiobutton he has choosen. No matter what i tried. Nothing worked. I get always only the one radio button as checked, which i checked in xml layout...Help...
Button setWeekType = (Button) findViewById(R.id.setWeekType);
if (setWeekType != null) {
setWeekType.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new AlertDialog.Builder(report_activity.this, R.style.DialogTheme)
.setView(R.layout.dialog_type_day)
.setCancelable(true)
.setPositiveButton("Choose", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final View child = getLayoutInflater().inflate(R.layout.dialog_type_day, null);
final RadioGroup radiogroup = (RadioGroup)child.findViewById(R.id.radiogroup);
final RadioButton bhome = (RadioButton)child.findViewById(R.id.bhome);
final RadioButton bwork = (RadioButton)child.findViewById(R.id.bwork);
final RadioButton bschool = (RadioButton)child.findViewById(R.id.bschool);
bwork.setChecked(true);
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
if (checkedId == bhome.getId()) {
bhome.setChecked(true);
}if (checkedId == bwork.getId()) {
bwork.setChecked(true);
}if (checkedId == bschool.getId()) {
bschool.setChecked(true);
}
}
});
}
})
.setNegativeButton("Cancel", null)
.show();
}
});
}
I run your code and I think the problem is with child View, It's null! So radiogroup is null too and the listener will never be set, Use logging to see yourself.
I recommend you to create a custom DialogFragment to get rid of nested inner classes and manage things much more easier.
In case you need a tutorial:
http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/
You can make that to else if. Example :
if (checkedId == bhome.getId()) {
bhome.setChecked(true);
}else if (checkedId == bwork.getId()) {
bwork.setChecked(true);
}else if (checkedId == bschool.getId()) {
bschool.setChecked(true);
}
And you can make oncheckedchangelistener in a top of on click listener
You are calling setOnCheckedChangeListener from within the (positive) button callback. This means that your second Toast can only be shown after the user has already clicked the "Choose" button (which presumably, is too late).
You need to get the find the radio group from the AlertDialog instance and call setOnCheckedChangeListener before you show the dialog.
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.
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
}
});
}
So I have made a alert dialog box with 3 options. So, if the first option is selected and something specific is entered into a edit text field it changes a text view into something specific. How do I go about giving the radio button a function?
Check this
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
switch (checkedRadioButton) {
case R.id.firstRadioBtn: //id of first radio button
//code goes here if first radio button is clicked
break;
case R.id.secondRadioBtn://id of second radio button
//code goes here if second radio button is clicked
break;
case R.id.thirdRadioBtn://id of third radio button
//code goes here if third radio button is clicked
break;
}
}
});
Hope it helps.
I think this is what you want....
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
// your code here
}
});
here inside the code block you will do something specific or whatever.... ;)
you must use
RadioGroup radioGroup = (RadioGroup) dialog.findViewById(R.id.yourRadioGroup);
if this is a part of your dialog....
Hope this helps
you can get it in a much easier way like this
builder.setSingleChoiceItems(items, checkeditem, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
I have a radio button group in Android which looks something like:
Choose Color:
Red
Blue
Orange
Green
I need to get selected radio button and also its value.
I have 4 radiobuttons in this manner within radiogroup rg
rb1a=(RadioButton)findViewById(R.id.rb1a);
rb1b=(RadioButton)findViewById(R.id.rb1b);
rb1c=(RadioButton)findViewById(R.id.rb1c);
rb1d=(RadioButton)findViewById(R.id.rb1d);
tv1=(TextView)findViewById(R.id.tv1);
next1=(Button)findViewById(R.id.next1);
rg=(RadioGroup)findViewById(R.id.rg);
// I have error after this line.please help
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
}
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
}
});
you can test the radion button with the isChecked() function.
for ex:
if(radio1_red.isChecked())
{
txtView.setText("Red button is checked");
}
Have a look at this Example .
You can also refer this Page - Form Stuff given in android-sdk pages.
Do this for getting selected radio button and also its value:
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
like you can see on the android documentation
http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
the OnCheckedChangeListener has only the method
onCheckedChanged(RadioGroup group, int checkedId) and doesn't contain the method
public void onCheckedChanged(CompoundButton arg0, boolean arg1) -> remove it and try again.
You find an example here:
http://developer.android.com/guide/tutorials/views/hello-formstuff.html
regards