0

I would like to create a method which returns a RelativeLayout created dynamically. To be clear, let's use this simplified example:

private RelativeLayout createLayout() {
        RelativeLayout layout = new RelativeLayout(activity);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        layout.setLayoutParams(params);

        TextView tv1 = new TextView(activity);
        tv1.setText("Text 1");

        TextView tv2 = new TextView(activity);
        tv2.setText("Text 2");

        TextView tv3 = new TextView(activity);
        tv3.setText("Text 3");

        layout.addView(tv1);
        layout.addView(tv2);
        layout.addView(tv3);

        return layout;
}

Now I want to position these TextViews relatively to each other. For that I have the idea to use a LayoutParams with the addRule method.

But this method requires an ID, e.g. addRule(RelativeLayout.BELOW, tv2Id). It means that I have to set an ID for each TextViews.

My problem is that the createLayout method will be called several times, so the question is:

Do I have to set different IDs for the TextViews each time the method is called in order to avoid conflicts ? If so, how can I do that ?

Most generally, Is there a better solution for doing it ?


EDIT

The idea behind this is to have a kind of ListView, where each item contains a Map (that can be shown or hidden).

Problem: the Map can't be scroll if it is inside a ListView (at least I did not manage to do that).

For that, I have decided to use a ScrollView and a LinearLayout to copy the behaviour of a ListView. This way the Map can be scrolled correctly and now, all I have to do is to create the items dynamically

G.T.
  • 1,557
  • 1
  • 12
  • 24

1 Answers1

1

ID's don't have to be unique. As you can see from this extract

setId (int id)

Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.

But like you said, if you want to avoid conflict then you have to find a way to generate unique identifiers for each view.

Frankly, IMO I don't think it matters much the value of the ID. You can use 10, 20, 30. Just make sure you can have access to them anytime you need it, possible using a static final variable.

You asked if there is a better solution, yes there is. The most preferred way is to inflate an xml layout.

Olayinka
  • 2,813
  • 2
  • 25
  • 43
  • Okay, thank you for your explanation. I will try to use a XML layout and to inflate it. – G.T. Jun 13 '14 at 09:01
  • @G.T. A scrollable item inside a listview is a bad idea and bad practice. I would advise you make the item static but onclick, you pop up a dialog and the scrolling is done on this dialog. – Olayinka Jun 13 '14 at 09:09
  • Is this a bad practice for the user experience or for the performance of the application? – G.T. Jun 13 '14 at 09:15
  • @G.T. Nested Scrollable views is bad for app performance. Read this http://stackoverflow.com/questions/4490821/scrollview-inside-scrollview – Olayinka Jun 13 '14 at 09:17