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
}
})
Related
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
}
});
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{
.....
}
in my android app i am tried to create a dynamic radio button, according to an arraylist size, inside a radio group using the following code
for (int aa = 0; aa < itemname.length(); aa++)
{
String iname = itemname.getJSONObject(aa).getString("item");
final RadioButton[] radioButton = new RadioButton[itemname.length()];
radioButton[aa] = new RadioButton(this);
radioGroup.addView(radioButton[aa]);
radioButton[aa].setTextColor(R.color.black);
radioButton[aa].setText(iname);
radioButton[aa].setButtonDrawable(R.drawable.about_us_tab);
radioButton[aa].setId(h_count);
radioGroup.setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
int selectedBtn = radioGroup.getCheckedRadioButtonId();
}
});
h_count++;
}
}
I am getting all the data in right order, and the onclick action of radio group is also working correctly.
But i want the the first radio button to be in selected mode. For this i tried using an if condition as follows
if(aa == 0)
{
radioButton[aa].setChecked(true);
radioButton[aa].setId(h_count);
}
But here the problem is, the first one is getting selected and in onclick action of radio group the first one is not getting deseleced, all others are working fine.
how to make the first one also deselected in onclick action of radio group
try in this way and its working
for (int aa = 0; aa < itemname.length(); aa++)
{
String iname = itemname.getJSONObject(aa).getString("item");
final RadioButton radioButton = new RadioButton(this);
radioGroup.addView(radioButton;
radioButton.setTextColor(R.color.black);
radioButton.setText(iname);
radioButton.setButtonDrawable(R.drawable.about_us_tab);
if(aa == 0)
{
radioButton.setChecked(true);
}
radioButton.setId(h_count);
radioGroup.setOnCheckedChangeListener(new android.widget.RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
int selectedBtn = radioGroup.getCheckedRadioButtonId();
}
});
h_count++;
}
I have a radio button group in Android.
I receive events when the items are selected. Normal so far.
But I don't get the event if the user clicks on an already selected item.
Is there a way to know (to receive an event) when the users hits a radiobutton either if it is selected or not?
Thanks a lot.
i don't understand why you would get an event when clicked on an already checked radio button,
but if you want to unselect a radio button by a click on it if it is already selected!!
check this code it can help you:
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
boolean hack = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.rg);
radioButton1 = (RadioButton) findViewById(R.id.r1);
radioButton2 = (RadioButton) findViewById(R.id.r2);
radioButton3 = (RadioButton) findViewById(R.id.r3);
OnClickListener radioClickListener = new OnClickListener()
{
public void onClick(View v)
{ //The first codition check if we have clicked on an already selected radioButton
if (v.getId() == radioGroup.getCheckedRadioButtonId() && hack)
{
radioGroup.clearCheck();
}
else
{
hack = true;
}
}
};
OnCheckedChangeListener radioCheckChangeListener = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
hack = false;
}
};
radioButton1.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton2.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton3.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton1.setOnClickListener(radioClickListener);
radioButton2.setOnClickListener(radioClickListener);
radioButton3.setOnClickListener(radioClickListener);
}
Hope this wil help
By setting an OnClickListener() on your buttons...
I am having a problem with a radio button that is in a radio group.
I have an app that is a quiz application and I ask 5 questions.
When run the application in my Android emulator all the questions have no problem but only on the 3rd question. When I click with my mouse on the radio button it seems toggle to a checked state but then it unchecks right away. Has anyone seen this kind of behavior?!
I setup this Radio group and dynamically add 4 radio buttons in a radio group and then use an OnCheckedChangeListener() event to capture the change.
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0; i<=3;i++)
{
RadioButton btn = (RadioButton) radioGroup.getChildAt(i);
if (btn.isPressed() && questNo < 6)
{
if (corrAns[questNo-1].equals(btn.getText()) && flag==true)
{
Log.e(LOG_TAG,"onCheckedChanged: correct answer = btn Text");
score++;
flag = false;
checked = true;
}
else if(checked==true)
{
Log.e(LOG_TAG,"onCheckedChanged: correct answer != btn Text");
score--;
flag = true;
checked=false;
}
}
}
Log.e(LOG_TAG, "Score:"+ Integer.toString(score));
}
});
I have noticed that it happens randomly on different questions and only on first radiobutton that is selected but if you select another one after then the functionality returns to normal. Any ideas?
I had the same bug. I'm also using dynamic radioGroups and buttons. This is working for me:
private OnCheckedChangeListener rblLikert_Listener = new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
int selectedIndex = group.getCheckedRadioButtonId();
if(selectedIndex != -1)
{
m_likertValue = radioButtonValue;
int buttonId = group.getCheckedRadioButtonId();
Logger.i("button id: " + String.valueOf(buttonId));
RadioButton selectedButton = (RadioButton)findViewById(buttonId);
selectedButton.toggle();
Logger.i(" is checked: " + String.valueOf(selectedButton.isChecked()));
}
}
};