0

In my application, I want to add fields dynamically based on the selection. While there are 4 fixed spinner fields, the values of spinner are dependent on the selection of the first spinner field etc. The second spinner field has to be populated based on the first field and the third spinner field based on the second field and so on.

Based on the options selected in these 4 spinner fields, the layout will contain additional fields that are text fields or radio buttons etc.

Can someone suggest what is the best way to achieve this or refer to other examples for this?

Thanks

3
  • can you post what have you done till?? Commented Jan 28, 2013 at 11:13
  • So far, I have only created the 4 spinner fields and able to populate the spinner field values based on selection using OnItemClickListener. I am not able to progress on how to add the new fields based on these selections. Commented Jan 28, 2013 at 11:32
  • do you want to inflate a custom view inside a layout or to created fields dynamically? and please post the code so that we can understand were you got stuck? Commented Jan 28, 2013 at 11:35

1 Answer 1

2

You can add a View to a Layout similar to the below code:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            (LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));

RelativeLayout relativeLayout = new RelativeLayout(mContext);
relativeLayout.setLayoutParams(layoutParams);

TextView view = new TextView(mContext);
view.setLayoutParams(layoutParams);

relativeLayout.addView(view);

And then you need to add it to an existing ViewLayout:

LinearLayout originalLayout = findViewById(R.id.mylayout);
originalLayout.addView(relativeLayout);

This idea can be expanded on to add custom Views etc to your screen.

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

2 Comments

Thank you for the reply. While I understand that you are creating a new layout and the components dynamically and adding it to the already defined layout, I would like to know how to add this on selection of spinner object. I have a layout with four spinner objects and they are populated using OnItemClickListener. How do I add the custom views based on the selection of these spinner objects.
Are you looking to get notified of the selection event? Use spinner.setOnItemSelectedListener(new OnItemSelectedListener());

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.