android
android

Reputation: 33

How to create radio button without knowing exactly count?

How to create radio button in radioGroup in file layout without knowing exactly count

Dose anyone has any ideas?

Upvotes: 0

Views: 501

Answers (2)

Mudassir
Mudassir

Reputation: 13174

May be this is of any help;

public class RadDemoActivity extends Activity {

    private RadioGroup radGroup;
    private Button addButton;
    private RadioButton radButton;

    private static int no;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        no=0;

        doInflateItems();

        setAddButton();
    }


    private void doInflateItems() {

        radGroup = (RadioGroup) findViewById(R.id.radGroup);
        addButton = (Button) findViewById(R.id.addRad);
    }


    private void setAddButton() {

        addButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                radButton = new RadioButton(getApplicationContext());
                radButton.setText("Option " + no++);

                radGroup.addView(radButton);
            }
        });
    }
}

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button android:id="@+id/addRad" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Add" />


    <ScrollView android:id="@+id/vScroll" android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <RadioGroup android:id="@+id/radGroup"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />

    </ScrollView>
</LinearLayout>

This code is adding radio buttons from a button click, you can do the same from a loop if you want to add multiple buttons at once. Hope this helps.

Upvotes: 1

Tofeeq Ahmad
Tofeeq Ahmad

Reputation: 11975

I request you to say thing clearly,but as i got ,you want many number of radio Button,

Take a loop and create dynamic RadioButton add these button to Radio Group  

Upvotes: 3

Related Questions