2
I have a certain part of an application in which there is a random questionnaire. Generated by the user himself. The point is, I don’t have access to the amount of questions or what the questions are promptly. So I have to get all of this dynamically. So, the questionnaire has to be dynamic. And there’s my doubt. I’ve got it all. But when it comes time to ride I’m not getting.
I use a ViewFlipper
for the various questions. Within this ViewFlipper
are the layouts with the questions. I use a ScrollView
and within the ScrollView
one RelativeLayout
to assemble the questions. I don’t know if this is the right way but it works when using a fixed questionnaire.
I’m having layout break while 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);
}
}
Set "layout break".
– Piovezan
Are you having crash? Paste the error stack if so. Or, post a screenshot of what the screen looks like when you use this code.
– Neto Marin
Thanks for the interest. That’s it! The problem was that I was creating a form and the elements were climbing on top of each other. I was basically forgetting to add a rule to Below and set the id.
– user5023
Please put the solution as a response and preferably accept it. You help other people and can still gain reputation.
– Maniero
@user5023 "The problem was that I was creating a form and the elements were climbing on top of each other. I was basically forgetting to add a rule to Below and set the id." Since the elements are like a list of questions, wouldn’t it be more interesting to exchange this relativelayout for a linearlayout? each new view created would be added below the previous one.
– AnselmoMS
@Anselmoms would be a good idea too. In fact the option for relative was given because there was already a structured form used in the application. So I took advantage. But I will take into account what was said in future forms.
– user5023