How to get checked radio button's id created dynamically in android
Asked Answered
B

3

6

In my activity, I am creating a dynamic radiogroup. I have some questions and answers in Sqlite database, retrieving them in activity and then set in RadioGroup. This is working fine. But after that, I want to get all ID of selected radio button by user to store them in database. In general, we do something like this when we have option's ID :

            id1=rdgroup1.getCheckedRadioButtonId();
            que1=(RadioButton)findViewById(id1);
            ans1=que1.getId()+""; // so here I will get radio button's ID.

So my questions is, how will I get selected radio button's ID. Here's my code for dynamically creating radiogroup.

     LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
        c1=db.rawQuery("SELECT * FROM QueTable WHERE AgeGroup='10-20'", null);
        c1.moveToFirst();
        int i = c1.getCount();
        if(i > 0)
        {
            Log.w("START", "start");
            while (i > 0)
            {
                TextView title = new TextView(this);
                questions = c1.getString(1);
                title.setText(questions);
                title.setTextColor(Color.BLACK);
                mLinearLayout.addView(title);
                // create radio button

                answers=c1.getString(2);
                String[] answer = answers.split(",");
                rb = new RadioButton[5];
                rg = new RadioGroup(this);
                rg.setOrientation(RadioGroup.VERTICAL);

                int k = answer.length;

                for (int j = 0; j < k; j++)
                {
                    rb[j] = new RadioButton(this);
                    rg.addView(rb[j]);
                    rb[j].setText(answer[j]);
                }
                mLinearLayout.addView(rg);
                c1.moveToNext();
                i--;
            }
        }
Bolognese answered 12/3, 2015 at 8:3 Comment(5)
how will I get selected radio button's ID but you are getting id using que1.getId() then what is issue?Hanhhank
@ρяσѕρєяK the issue is I'm creating radiogroup programmatically. So I don't know ID of any radio button. See second part of code of dynamic radiogroup. que1.getId() is used when we created them using drag and drop in xml layout, then give them id in xml like android:id...Bolognese
then what is issue use id1=rg.getCheckedRadioButtonId();que1=(RadioButton)findViewById(id1);ans1=que1.getId()+""; ??Hanhhank
@ρяσѕρєяK whatever u are telling is static radiobutton not dynamic. So I know ID of that static radio button but not dynamiclly added button.Bolognese
@ Ruchir Tarawat : you can set radiobutton id using setid method in codeHanhhank
B
2

Ok..So finally I got the solution. I am going to share complete code here that will create dynamically radiogroup and radiobuttons from database. Here is code below :

private static final int RB_ID = 100;
int k=0,len=0,r=0,p = 0,q=0, len = 0;

 LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
 c1 = db.rawQuery("SELECT * FROM QueMotion WHERE AgeGroup ='"+Age+"' LIMIT 5", null);
            c1.moveToFirst();
            int i = c1.getCount();
            rg = new RadioGroup[i];
            rb = new RadioButton[i*5];
            if (i > 0) {
                Log.w("START", "start");
                while (i > 0)
                {
                    TextView title = new TextView(this);
                    questions = c1.getString(1);// retrive from database
                    title.setText(questions);
                    title.setTextColor(Color.BLACK);
                    title.setTypeface(null, Typeface.BOLD);
                    title.setPadding(0, 0, 0, 10);
                    mLinearLayout.addView(title);
                    answers = c1.getString(2);// retrive from database
                    String[] answer = answers.split(",");// will create options for radio button.
                    rg[p] = new RadioGroup(this);
                    rg[p].setOrientation(RadioGroup.VERTICAL);
                    len=len+answer.length;
                    for (int j = 0; j < answer.length; j++)
                    {
                            rb[q] = new RadioButton(this);
                            rg[p].addView(rb[q]);
                            rb[q].setId(k + RB_ID);
                            rb[q].setText(answer[j]);
                            k++;
                            q++;
                    }
                    mLinearLayout.addView(rg[p]);
                    c1.moveToNext();
                    i--;
                    p++;
                }

    //Submit button click
     submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (rg[0].getCheckedRadioButtonId() == -1)
                {
                    Log.e("Nothing selected", "Nothing selected");
                }
                else
                {
                    for (r = 0; r < len; r++) {
                        if (rb[r].isChecked()) {
                            RadioButton id = (RadioButton) findViewById(rb[r].getId());
                            String radioText = id.getText().toString();
                            c3.moveToFirst();
                            Log.e("RadioString", radioText);
                        } else {
                            Log.e("RadioString", "nothing");
                        }
                    }
                }
            }
        });
Bolognese answered 16/3, 2015 at 10:51 Comment(0)
R
4

The place where you create RadioButton set and Id to it

  for (int j = 0; j < k; j++)
            {
               RadioButton rb = new RadioButton(this);
                rb.setId(Somenumber + j)
                 rb[j] = rb;
                rg.addView(rb[j]);
                rb[j].setText(answer[j]);
            }

Like this you can setId or also if you want you can add tag to process.

                rb.setTag(SomeObject)
Rutherford answered 12/3, 2015 at 8:23 Comment(0)
C
2

You need to set an ID for your programmatically created RadioButton in order to find it using findViewById as you show in your first code.

To set the ID for your RadioButton, you can use View.setId(). The documentation says:

Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.

The identifier does not have to be unique, but if it's not unique, findViewById will return the first occurence, so you can use the static method View.generateViewId added on API level 17.

If you are using lower APIs, you may want to write your own function to find a suitable (unused) ID. You can take a look at Android: View.setID(int id) programmatically - how to avoid ID conflicts?

Coot answered 12/3, 2015 at 8:23 Comment(2)
Your answer is good..set the id of radio button but what about radioGroup. I mean, how it will detect that which radioGroup's radiobutton is selected. I have 20 questions and each have 4 options. So user will click on all options. And to get id of radiobutton, I also need radioGroup's id.Bolognese
@Ruchir Tarawat You can set the id for your RadioGroup too. Also, you can set a tag for your Radiogroup and RadioButton using setTag() and check for the tag (according to the documentation "a tag can be used to mark a view in its hierarchy"). You can also create some kind of structure (a Map can do the trick) to relate each RadioButton with the key you want to store on your DB.Coot
B
2

Ok..So finally I got the solution. I am going to share complete code here that will create dynamically radiogroup and radiobuttons from database. Here is code below :

private static final int RB_ID = 100;
int k=0,len=0,r=0,p = 0,q=0, len = 0;

 LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
 c1 = db.rawQuery("SELECT * FROM QueMotion WHERE AgeGroup ='"+Age+"' LIMIT 5", null);
            c1.moveToFirst();
            int i = c1.getCount();
            rg = new RadioGroup[i];
            rb = new RadioButton[i*5];
            if (i > 0) {
                Log.w("START", "start");
                while (i > 0)
                {
                    TextView title = new TextView(this);
                    questions = c1.getString(1);// retrive from database
                    title.setText(questions);
                    title.setTextColor(Color.BLACK);
                    title.setTypeface(null, Typeface.BOLD);
                    title.setPadding(0, 0, 0, 10);
                    mLinearLayout.addView(title);
                    answers = c1.getString(2);// retrive from database
                    String[] answer = answers.split(",");// will create options for radio button.
                    rg[p] = new RadioGroup(this);
                    rg[p].setOrientation(RadioGroup.VERTICAL);
                    len=len+answer.length;
                    for (int j = 0; j < answer.length; j++)
                    {
                            rb[q] = new RadioButton(this);
                            rg[p].addView(rb[q]);
                            rb[q].setId(k + RB_ID);
                            rb[q].setText(answer[j]);
                            k++;
                            q++;
                    }
                    mLinearLayout.addView(rg[p]);
                    c1.moveToNext();
                    i--;
                    p++;
                }

    //Submit button click
     submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (rg[0].getCheckedRadioButtonId() == -1)
                {
                    Log.e("Nothing selected", "Nothing selected");
                }
                else
                {
                    for (r = 0; r < len; r++) {
                        if (rb[r].isChecked()) {
                            RadioButton id = (RadioButton) findViewById(rb[r].getId());
                            String radioText = id.getText().toString();
                            c3.moveToFirst();
                            Log.e("RadioString", radioText);
                        } else {
                            Log.e("RadioString", "nothing");
                        }
                    }
                }
            }
        });
Bolognese answered 16/3, 2015 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.