Wednesday, April 24, 2013

Dynamic radio button demo in Android | Radio Group example in android in Java class

This is a simple tutorial for dynamic Radio button in Android. Copy paste below code and enjoy..



1)MainActivity.java
package com.manish.dynamicspinner;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

 String countryName[] = { "India", "Pakistan", "China", "Nepal",
   "Bangladesh" };

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
  for (int k = 1; k <= 20; k++) {
   //create text button
   TextView title = new TextView(this);
   title.setText("Question Number:" + k);
   title.setTextColor(Color.BLUE);
   mLinearLayout.addView(title);
   // create radio button
   final RadioButton[] rb = new RadioButton[5];
   RadioGroup rg = new RadioGroup(this);
   rg.setOrientation(RadioGroup.VERTICAL);
   for (int i = 0; i < 5; i++) {
    rb[i] = new RadioButton(this);
    rg.addView(rb[i]);
    rb[i].setText(countryName[i]);

   }
   mLinearLayout.addView(rg);
  }
 }
}
2)activity_main.xml


    
    

27 comments:

  1. Nice... is it possible to download it in zip ? Thx

    ReplyDelete
    Replies
    1. Hi GG thanks for your comment. Please don't mind it is a very simple demo an activity and a layout so just copy paste it and enjoy.
      Thanks

      Delete
  2. sir, what aplication to write this syntac??
    java or eclipse??

    ReplyDelete
    Replies
    1. This syntax for Android(Java). you can use any IDE or simple notepad no issue...

      Delete
  3. how to get the selected item.

    ReplyDelete
  4. Sure your previous experiance help you to learn java. Nothing is impossible. Best of luck..

    ReplyDelete
  5. very useful and how to save the result when using the database ....?

    ReplyDelete
    Replies
    1. You can put a Submit button for save data into database. And simply get all data from selected radio button pass to database.

      Delete
  6. sir, excellent tutorial .... i want to know how to do click event for radio buttons. ie.... here i want to display a message as the radio button is clicked...e.g if radiobutton 1 is clicked then i need it as "Radiobutton 1 is clicked.."can u pls help me its very urgent

    ReplyDelete
  7. Hi Manish,

    I need small help. My requirement is getting the listview with Questions and options(which has radio buttons)

    I used the same logic what you made but when I scroll the radio buttons are getting duplicates (Like scrolling twice gives two sets of radio buttons,thrice gives three sets of radio buttons ) I used view.setTag(viewHolder) method to eliminate this issue.Still it is not solved.

    Please give me an idea how to resolve it.

    ReplyDelete
  8. Is it? Okay so where are your question-answers? on server or sqlite? no issue so just use listview and in every row generate dynamic radio button. Or may be on scroll you can add item.clear() if it is work so every-time your item will be new.

    ReplyDelete
    Replies
    1. Question-Answers are coming from SQlite

      Delete
    2. so just put them inside list-view. It will be right approach to do this task.

      Delete
  9. Hi Manish can you please give a reference code how to get all the selected values when a button clicked thankyou...

    ReplyDelete
    Replies
    1. On click of button just get the all selected item. Any issue here?

      Delete
    2. Hi manish thanku for ur reply.In the above code there is no id's for radio group as well as radio buttons i tried to set the id's dynamically and tried to get the all the selected items on a button click. But am unable to get the values of check radio buttons i used below code for this.....

      int selectedId = radioGroup.getCheckedRadioButtonId();

      Delete
  10. I have a question
    if i upload folder at my website and i make user and password for the folder
    i mean if my website like it www.name.com and i upload folder name : india
    local host like this : www.name.com/india
    what the code can i use of eclipse pro ?

    ReplyDelete
  11. I copy pasted the code ....only changed the name of the package as "com.example.questions" and i got an error- "didn't find class android.view.scrollview" in eclipse. Can you point out what i might be doing wrong?? .
    The name of package is set as per the name of the project i created, so it should be fine i think

    ReplyDelete
    Replies
    1. Got the error ...scrollview should be ScrollView and linearlayout should be LinearLayout in the XML file.

      Delete
  12. how do i add on click listener for each of the radio group?

    ReplyDelete
    Replies
    1. I think you can use add id for radiogroup and then get them when you needed-
      rg.addId(i);

      Delete
    2. i have tried that with setOnCheckedChangeListener, but no response for whichever radio button i tried

      Delete
  13. How to get selected value form particular group?

    ReplyDelete