3

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 RadioButtons. After RadioGroups I need to place two EditText and one CheckBox control with submit button afterwards.

I have prepared a LinearLayout with vertical orientation and unique ID to be addressed from code and I expect that I can create a form control within a Java code without defining in android XML layout and adding to this LinearLayout.

I was googling for a few hours but could not find any example how to do this.

Could anyone please provide some example how to create e.g. one RadioGruop with 1-2 RadioButtons and add it to the LinearLayout (that is prepared in XML layout)?

Many thanks for any advice!!!

shadyyx
  • 15,825
  • 6
  • 60
  • 95

1 Answers1

5

These widgets can be create like every other widgets:

final Context context; /* get Context from somewhere */
final LinearLayout layout = (LinearLayout)findViewById(R.id.your_layout);
final RadioGroup group = new RadioGroup(context);
final RadioButton button1 = new RadioButton(context);
button1.setId(button1_id); // this id can be generated as you like.
group.addView(button1,
    new RadioGroup.LayoutParams(
        RadioGroup.LayoutParams.WRAP_CONTENT,    
        RadioGroup.LayoutParams.WRAP_CONTENT));
final RadioButton button2 = new RadioButton(context);
button1.setId(button2_id); // this id can be generated as you like.
button2.setChecked(true);
group.addView(button2,
    new RadioGroup.LayoutParams(
        RadioGroup.LayoutParams.WRAP_CONTENT,    
        RadioGroup.LayoutParams.WRAP_CONTENT));
layout.addView(group,
    new LinearLayout.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.

Michael
  • 53,859
  • 22
  • 133
  • 139
  • Hmm, thank You! I was nearly there with my code, just missing `new LinearLayout.LayoutParams(...)` part... But still I have one problem: I'm creating controls in a loop like `for(int i = 0; i < questions.getLength(); i++) { TextView q = new TextView(this); q.setText("TEST-" + i); form.addView(q, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); }` where should three TextViews be created but ONLY ONE is... Am I missing something??? – shadyyx May 01 '11 at 12:03
  • This code seems to be correct. So, I think the error is somewhere else. – Michael May 01 '11 at 12:26
  • OK, so they appear to be processed but the second and third TextView is just not "drawed" to the screen even there is new space for these controls... I tried calling `.postInvalidate()` on LinearLayout but with no success... Any suggestion? – shadyyx May 01 '11 at 13:26
  • Are you sure you're adding this widgets to `LinearLayout`, not `FrameLayout`? Anyway, I suggest you trying Hierarchy Viewer tool. It must help. – Michael May 01 '11 at 15:00
  • OK, I figured it out... It took me two hours, I feel like a dumb... and it was just a tiny think - the orientation of LinearLayout... While I was trying to add controls one under another there were postioned next to the right... So `layout.setOrientation(LinearLayout.VERTICAL);` did the job... – shadyyx May 01 '11 at 15:42
  • Oh, I faced similar problem several times. That's disgusting. – Michael May 01 '11 at 16:08
  • And please, can You also help me hot to generate the ID for these RadioButtons so they could be addressed when the form is submitted? You wrote "`button1.setId(button1_id); // this id can be generated as you like.`" but how can I achieve this IDs to be added into my local R.Java??? When I set the ID like `button1.setId(100+i);` and then call `RadioButton b1 = (RadioButton)findViewById(100+i); if(b1.isChecked()) {...}` I get the nullPointerException... – shadyyx May 04 '11 at 23:47
  • You don't need these IDs to be in the R.java file. I don't know really why you can't find the button later. Try to find a reason using Hierarchy Viewer tool. It shows widgets' IDs. Anyway, I think, you'd better use `RadioGroup.getCheckedRadioButtonId()` to find a checked button. You can also use tags instead of IDs and search views using `findViewWithTag()`. – Michael May 05 '11 at 05:39
  • Hmmm, that's nice. Thank You! Anyway, problem was in `for` loop that I corrected later on. Now my app is working well within emulator but when packed into .apk and install on my Htc Desire, it is not working...but this is for another thread... Again, thank You very much for Your help!!! – shadyyx May 05 '11 at 08:59