Add Elements Programmatically

Asked

Viewed 276 times

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);

    }
}
  • 1

    Set "layout break".

  • 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.

  • 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.

  • 4

    Please put the solution as a response and preferably accept it. You help other people and can still gain reputation.

  • @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 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.

Show 1 more comment

1 answer

1

As I said, the problem was solved as follows.

By creating a View has been informed your ID.

view.setId(INTEIRO);

And in the LayoutParams next View a rule has been added.

layoutParams.addRule(RelativeLayout.BELOW, viewAnterior.getId());

That’s not the right way, most likely. I found it half-hearted but followed the pattern that had already been established in the other application forms. Maybe, as @Anselmoms suggested, the best option would have been to use a LinearLayout.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.