How to give radio button functions in a alert dialog? - android

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
}
});

Related

how to validate fragment RadioGroup inside from MainActivity Button click?

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();
}
});

How to add the selected Radiobutton in an array in android

There is a question having 4 options(Radio buttons). I want to add the option of selected radio button into an array. ONLY the option of the selected radio button.
The Radio Group is having 4 radio buttons.
You should wrap all the radiobuttons in a RadioGroup and then set its OnCheckedChangeListener:
final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View radioButton = group.findViewById(checkedId);
// add radioButton to Your array
}
});
Note that this code will add to the array the selected radio button every time it gets selected. If you want to add it on a button click, you need to do it this way:
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int checkedId = radioGroup.getCheckedRadioButtonId();
View radioButton = radioGroup.findViewById(checkedId);
// add radioButton to Your array
}
})

setOnCheckedChangeListener is never called

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.

Android - Radio button selections are not being registered

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"

Android Radio Button

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

Categories

Resources