Skip to content Skip to sidebar Skip to footer

Android - Build Dynamic Form From Code

I have to build a dynamic form in my activity depending on the data retrieved via HTTP that is popullated from XML. This could be one or more RadioGroups each with exactly three Ra

Solution 1:

These widgets can be create like every other widgets:

final Context context; /* get Context from somewhere */finalLinearLayoutlayout= (LinearLayout)findViewById(R.id.your_layout);
finalRadioGroupgroup=newRadioGroup(context);
finalRadioButtonbutton1=newRadioButton(context);
button1.setId(button1_id); // this id can be generated as you like.
group.addView(button1,
    newRadioGroup.LayoutParams(
        RadioGroup.LayoutParams.WRAP_CONTENT,    
        RadioGroup.LayoutParams.WRAP_CONTENT));
finalRadioButtonbutton2=newRadioButton(context);
button1.setId(button2_id); // this id can be generated as you like.
button2.setChecked(true);
group.addView(button2,
    newRadioGroup.LayoutParams(
        RadioGroup.LayoutParams.WRAP_CONTENT,    
        RadioGroup.LayoutParams.WRAP_CONTENT));
layout.addView(group,
    newLinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,    
        LinearLayout.LayoutParams.WRAP_CONTENT));

I haven't tested this code, so it may contain some errors. But I hope you'll get the idea.

Post a Comment for "Android - Build Dynamic Form From Code"