Creating RadioButtons programmatically - android

I want to create a series of radio buttons which correspond to an array of strings within an Android app. The radio buttons should toggle content to be displayed from the array. How do I do this?

You must add the radio buttons to a RadioGroup and then the RadioGroup to the layout
I miss some information like what is submit, but your code should look like:
private void createRadioButton() {
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
rb[i].setText("Test");
}
ll.addView(rg);//you add the whole RadioGroup to the layout
ll.addView(submit);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < 5; i++) {
rg.removeView(rb[i]);//now the RadioButtons are in the RadioGroup
}
ll.removeView(submit);
Questions();
}
});
}
Another code for dynamically creating the radiobutton
<TableRow>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radiobuttons">
</RadioGroup>
</TableRow>
Code:
public void makeRadioButtons(Vector tmpVector, int i, LinearLayout.LayoutParams lp)
{
RadioButton rb = new RadioButton(this);
rb.setText((String) tmpVector.elementAt(i));
//rg is private member of class which refers to the radio group which I find
//by id.
rg.addView(rb, 0, lp);
}

Related

How to get values from dynamic edittext and radio group?

I have created the dynamic view. That view contains two edittext and one radio group. when I click the add button the view is added to the layout. Now I got a confusion, how to get values from these type of dynamic views. I tried but it doesn't work. when I add the two or more views, the loop does not find the next views values. I want to add that values to ArrayList. This is code:
private void addDynamicViews() {
EditText name = new EditText(this);
EditText mobile = new EditText(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.setMargins(10, 10, 5, 5);
name.setLayoutParams(p);
name.setBackgroundResource(R.drawable.edittext_box);
name.setHint("Enter Name");
studentslayout.addView(name);
mobile.setLayoutParams(p);
mobile.setBackgroundResource(R.drawable.edittext_box);
mobile.setHint("Enter Mobile No");
studentslayout.addView(mobile);
/* radioGroup - Radio Group
maleButton,femaleButton - Radio Buttons
studentslayout - Linear Layout */
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
maleButton = new RadioButton(this);
maleButton.setText("Male");
radioGroup.addView(maleButton);
femaleButton = new RadioButton(this);
radioGroup.addView(femaleButton);
femaleButton.setText("Female");
studentslayout.addView(radioGroup);
}
How to take all dynamic edittext and radio group values ?
I tried this code But unfortunately it stopped.
#Override
public void onClick(View v) {
String[] array = new String[studentslayout.getChildCount()];
int count = studentslayout.getChildCount();
for (int i=0; i < studentslayout.getChildCount(); i++){
editText = (EditText)studentslayout.getChildAt(i);
array[i] = editText.getText().toString();
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
array[i] = radValues.getText().toString();
}
}
RadioButton radValues = (RadioButton) studentslayout.getChildAt(i);
You have added radioGroup and are expecting radiobutton. Also since you are looping, you should check the type of the view.
You can try something like this:
int childCount = studentslayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = studentslayout.getChildAt(i);
if (childView instanceof EditText) {
EditText editText = (EditText) childView;
String text = editText.getText().toString();
//use text
} else if (childView instanceof RadioGroup) {
RadioGroup radioGroup = (RadioGroup) childView;
int radioCount = radioGroup.getChildCount();
for (int j = 0; j < radioCount; j++) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
//use radioButton.
}
}
}

Set the ArrayList value to the RadioButton in the for loop

I am getting arrayList [5,8,13,18,19] from the server and I would like to create a RadioGroup in the xml file. After selecting the diserable items, I will put the selected items in arrayList and transmit the query to the server after clicking the OK button. How can I create such as programmatically RadioGroup?
I have tried this but I dont know how to loog through to set the RadioButton with the ArrayList value. How can I get that tp work?
private void createRadioButton(final ArrayList<Integer> items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
final ArrayList<RadioButton> rb = new ArrayList<RadioButton>();
final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
for (int i = 0; i < items.size(); i++) {
items.get(i) = new RadioButton(this);
}
}
Try this
final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
for (int i = 0; i < items.size(); i++) {
RadioButton rb = new RadioButton(this);
rb.setText(items.get(i)+"");
rg.addView(rb);
}
With the addView you are adding the dynamically created RadioButton to the RadioGroup.

How to get values from dynamic radiogroup android?

Hi i created dynamic radiogroup with radiobutton.Now i want to get values from dynamic radiogroup. Below is my code
final RadioGroup rg = new RadioGroup(this);
rg.setId(questionNo);
RadioButton[] rb = new RadioButton[answers.length()];
for (int i = 0; i < answers.length(); i++) {
JSONObject answerObject = answers.getJSONObject(i);
rb[i] = new RadioButton(this);
rb[i].setText(answerObject.getString("AnswerValue"));
rb[i].setId(answerObject.getInt("AnswerId"));
rg.addView(rb[i]);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = rg.getCheckedRadioButtonId();
Log.i("ID", String.valueOf(selectedId));
}
});
Button click event
submit_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rg.getCheckedRadioButtonId()!=-1){
int id= rg.getCheckedRadioButtonId();
View radioButton = rg.findViewById(id);
int radioId = rg.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(radioId);
String selection = (String) btn.getText();
Log.i("selection", selection);
}
}
});
I am getting only last index of the radio group.
I know it's too late, But this may help someone else
While creating your Dynamic Radiobutton Group, Create a parent layout(Linear layout or Relative Layout)
All radio groups will be the child of that Layout and all radio button will be a child of the specific radio group.
Creating No of Dynamic Radio Group
Create all Dynamic radio group in a parent layout
Add all radio button to a group
Add all radio group to the parent layout
public RadioButton[] rb;
public RadioGroup grp;
public LinearLayout layoutmain; //Parent layout
for(int i=0;i<qus.size();i++) //No of Questions
{
rb = new RadioButton[size];
grp = new RadioGroup(this);
grp.setId(a+qus.get(i).qusetionId);
for(int j=0;j<qus.get(i).choices.size();j++) //No of Radio button in radio group
{
rb[j] = new RadioButton(this);
rb[j].setText(qus.get(i).choices.get(j).getChoiceText());
grp.addView(rb[j]); // adding button to group
}
layoutmain.addView(grp); // adding group to layout
}
Retrieve All selected button task
From parent, layout get all child
Find all radio group using the child Instance
Create an object for that radio group
using that object get the selected radio button
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int i=0;i<layoutmain.getChildCount();i++) //From the parent layout (Linear Layout) get the child
{
View child = layoutmain.getChildAt(i);
if(child instanceof RadioGroup) //Check weather its RadioGroup using its INSTANCE
{
RadioGroup rg = (RadioGroup) child; //create a RadioButton for each group
int selectedId = rg.getCheckedRadioButtonId(); // get the selected button
RadioButton radiochecked = (RadioButton) findViewById(selectedId);
Log.e("Radio",radiochecked.getText().toString()); //from the button get the selected text
}
}
} });

Android radiogroup horizontal alignment contents not visible

My layout xml looks like
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollview1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearlayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
I am creating radiogroup and adding radiobuttons through code
LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);
RadioGroup rg = new RadioGroup(this);
rg.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
rg.setOrientation(0);
while(itr.hasNext()){
final String drinkname = itr.next();
System.out.println("drinkname: "+drinkname);
RadioButton rb = new RadioButton(this);
rb.setText(drinkname);
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//group.clearCheck();
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
System.out.println(selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
});
ll.addView(rg);
It is supposed to show 6 values; But it is showing only 3 properly and remaining looks like hidden. Can someone help me out?
Thanks in advance!!
With the current RadioGroup class implementation, you cannot have two rows of RadioButtons.
So, either you set an HorizontalScrollView to your layout and allow the user to scroll to see all the RadioButtons or you use two RadioGroups, each of them containing 3 RadioButtons.
So, using your example (with little variations), this last solution can be implemented like this:
LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);
RadioGroup rg = new RadioGroup(this);
RadioGroup rg2 = new RadioGroup(this);
rg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rg.setOrientation(LinearLayout.HORIZONTAL);
rg2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rg2.setOrientation(LinearLayout.HORIZONTAL);
String [] drinks = new String [] {"Mountain Dew", "7Up", "Root Beer", "Pepsi", "Cola", "Ice Tea"};
int i = 0;
while(i<drinks.length){
final String drinkname = drinks[i];
Log.i(TAG, "drinkname: "+drinkname);
RadioButton rb = new RadioButton(this);
rb.setText(drinkname);
if(i %2==0)
rg.addView(rb);
else
rg2.addView(rb);
i++;
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//unselect all radios in the other RadioGroup
if(rg2.getCheckedRadioButtonId()!=-1){
rg2.setOnCheckedChangeListener(null);
rg2.clearCheck();
rg2.setOnCheckedChangeListener(this);
}
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
if(selectrb != null){
Log.i(TAG,selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//unselect all radios in the other RadioGroup
if(rg.getCheckedRadioButtonId()!=-1){
rg.setOnCheckedChangeListener(null);
rg.clearCheck();
rg.setOnCheckedChangeListener(this);
}
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
if(selectrb != null){
Log.i(TAG,selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
}
});
ll.addView(rg);
ll.addView(rg2);
You just have to be careful with the fact that your two RadioGroups keep track of only 3 RadioButtons each. They do not communicate with each other and do not affect each other. So if you select one RadioButton from the first row (first RadioGroup), you still have your selection on the second RadioGroup and vice-versa. You have to manage this situation yourself.
Edit: One way to manage this is unselecting all the radioButtons in RadioGroup1, when we select one RadioButton in RadioGroup2 and vice-versa. I have added code to do that.

Can I create dynamically RadioGroup with RadioButtons inside it (w/o xml)?

I want to create RadioGroup, with RadioButton list inside it, in the onCreate function. I want to do it as exercise w/o using xml-layout. Is it possible? Thanks.
Something like this:
....
RadioGroup group = new RadioGroup(this);
group.setOrientation(RadioGroup.HORIZONTAL);
RadioButton btn1 = new RadioButton(this);
btn1.setText("BTN1");
group.addView(btn1);
RadioButton btn2 = new RadioButton(this);
group.addView(btn2);
btn2.setText("BTN2");
....
RadioButton btnN = new RadioButton(this);
group.addView(btnN);
btnN.setText("BTNN");
yourLayout.addView(group);
....
This will do the job:
int buttons = 5;
RadioGroup rgp = new RadioGroup(getApplicationContext());
for (int i = 1; i <= buttons; i++) {
RadioButton rbn = new RadioButton(this);
rbn.setId(1 + 1000);
rbn.setText("RadioButton" + i);
//Attach button to RadioGroup.
rgp.addView(rbn);
}
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
viewGroup.addView(rgp);
This is a complete example:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Defining buttons quantity!
int buttons = 5;
//Create a new instance of RadioGroup.
RadioGroup rgp = new RadioGroup(getApplicationContext());
//Create buttons!
for (int i = 1; i <= buttons; i++) {
RadioButton rbn = new RadioButton(this);
rbn.setId(1 + 1000);
rbn.setText("RadioButton" + i);
//Attach button to RadioGroup.
rgp.addView(rbn);
}
//Get the root view.
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
viewGroup.addView(rgp);
}
}
And this is the result:
If you need to use a RadioGroup defined into the xml layout and add dinamically buttons see this answer.

Categories

Resources