android - dynamic first radio button to be in true state - android

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

Related

Dynamic Radio button store json response id and name in that

Hello guys in bottom I code something which work perfectly but I am using array list in that, but I want Dynamic radio button if user select any button so for user it should display name and for me i get selected id my json response is like divid,divisionname I have to show divisionname to user in Radio button and if user select any radio based on that i want divid any thing if we pass both id and name in one radio button, on submit button i want Id
ArrayList<String> divId = null;
ArrayList<String> divName = null;
RadioGroup divRadioGrouop;
String divsionId=null;
if (divisionCheck) {
RadioButton rdbDiv;
divId = new ArrayList<>();
divName = new ArrayList<>();
divisionLogin.setVisibility(View.VISIBLE);
divRadioGrouop.removeAllViews();
for (int j = 0; j < jsonArrayDiv.length(); j++) {
JSONObject jsonDiv = jsonArrayDiv.getJSONObject(j);
divId.add(jsonDiv.getString("divid"));
divName.add(jsonDiv.getString("divisionname"));
rdbDiv = new RadioButton(this);
rdbDiv.setId(View.generateViewId());
rdbDiv.setText(jsonDiv.getString("divisionname"));
divRadioGrouop.addView(rdbDiv);
}
divOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int divid = divRadioGrouop.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(divid);
String divSionNameFind = (String) radioButton.getText();
int idPostionIndex = divName.indexOf(divSionNameFind);
divsionId = divId.get(idPostionIndex);
divName.clear();
divId.clear();
logINDataForDiv();
}
});
divCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something
}
});
}
You need to use tag in that case, first add divid in all your radio button like:
radioButton.setTag(divid);
on Submit, you can get back divid by this:
String divid=radioButton.getTag().toString();
Make sure you do setTag before getTag, otherwise getTag will return null and your app will crash.

Code-generated radiobuttons check selection

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.

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

Table with Radiobuttons in Android

I am looking for something similar in android where I can add radiobuttons to each cells in Table and get their position. Does anyone has worked on it before, if you can share the sample code would be helpful. Thanks
The following pseudocode should get you started:
//create a radio group
RadioGroup rg = new RadioGroup(context);
RadioGroup.LayoutParams rgParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
rg.setLayoutParams(rgParams);
//add the radio group to the row.
rowView.addView(rg);
//add radio buttons
for (i = 0; i < length; i++) {
RadioButton btn = new RadioButton(context);
btn.setText(description[i]);
//or save an object which will hold all information you need
btn.setTag(rowNumber);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, v.getTag());
}
});
if (<some condition>) {
btn.setChecked(true);
}
rg.addView(btn);
}

Radio Button in Radio Group does not show checked after clicking on it

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

Categories

Resources