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!!!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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???
This code seems to be correct. So, I think the error is somewhere else.
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?
Are you sure you're adding this widgets to LinearLayout, not FrameLayout? Anyway, I suggest you trying Hierarchy Viewer tool. It must help.
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...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.