I have a certain part of an application in which there is a random questionnaire. Generated by the user. The question is: I do not have access to the amount of questions or what the questions are promptly asked. So I have to get it all dynamically. Therefore, the questionnaire has to be dynamic. And therein lies my doubt. I already got everything. But when it comes to assembling, I'm not getting it.
I use ViewFlipper
for the various issues. Within this ViewFlipper
are the layouts with the questions. I use ScrollView
and within ScrollView
a RelativeLayout
to assemble the questions. I do not know if this is the right way but it works when I use a fixed questionnaire.
I'm having a layout break when trying to insert the elements dynamically.
RelativeLayout.LayoutParams paramsRelativeLayout = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
paramsRelativeLayout.setMargins(16, 16, 16, 16);
mainLayout = (ViewFlipper) findViewById(R.id.viewFlipper1);
for(int i = 0; i < perguntasLabel.length; i++) {
viewNotList = new ScrollView(getApplicationContext());
viewNotList.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
mainLayout.addView(viewNotList);
viewNotListChild = new RelativeLayout(getApplicationContext());
viewNotListChild.setLayoutParams(paramsRelativeLayout);
viewNotListChild.setBackgroundColor(getResources().getColor(R.color.padrao));
viewNotList.addView(viewNotListChild);
titulo = new TextView(getApplicationContext());
titulo.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
titulo.setText(perguntasLabel[i]);
titulo.setTextSize(20);
viewNotListChild.addView(titulo);
if (perguntasTipo[i].equals("datepicker")) {
EditText input = new EditText(getApplicationContext());
input.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
input.setBackground(getResources().getDrawable(R.drawable.input));
viewNotListChild.addView(input);
}
}