0

I get the list of elements from the HTTP response, then I want to dynamically insert that list into the textview inside the "box" that you can see, currently it just inserts a string and overlaps them one over the other. I tried changing the layout (all three constraint, relative and linear) and it didn't help. Does anyone know how to position them dynamicly inside the boxes and not overlap but have margins like in the second picture? Otherwise, inside the project, I use a constrain layout.

Here is my code:

RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.layout);

                    int size = response.toArray().length;

                    final TextView[] tv = new TextView[size];
                    TextView temp;

                    for (int i = 0; i < size; i++)
                    {
                        temp = new TextView(Activity.this);

                        temp.setText(response.get(i).getName());

                        parentLayout.addView(temp);

                        tv[i] = temp;

                    }

Here is the picture how it looks right now:

enter image description here

And here is the picture how I want it to looks like:

enter image description here

Niklaus
  • 159
  • 1
  • 2
  • 11

3 Answers3

3

This sounds like a typical ListView use case.

Firstly, I'd suggest you go through the documentation -

https://developer.android.com/reference/android/widget/ListView

You can see an implementation example of a list view with an array of strings here - https://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65

In general, you choose the UI of your item and the listView populates the view to each item in your list (each string in your case).

In the adapter, you give each item the data it needs for the UI.

Itay Feldman
  • 846
  • 10
  • 23
  • I don't understand this: ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, values); – Niklaus Aug 20 '19 at 09:01
  • @Niklaus This is the declaration of the adapter. You mention that it receives strings , you pass 'this' as context, the item layout xml you created for each item (here you create the box for each string with the margins and all), the id of the textview to insert the strings into and the array of the strings. – Itay Feldman Aug 20 '19 at 09:01
  • Yes, but what are these parameters inside? – Niklaus Aug 20 '19 at 09:02
  • In this example, R.layout.simple_list_item_1 is an android internal layout view already defined text fields to show data so you can just pass it the string but if you want a custom view you can just create a layout for the item and pass it there instead – Itay Feldman Aug 20 '19 at 09:09
  • Do I need to put layout and textview into ListView, cause I have problem with these – Niklaus Aug 20 '19 at 09:11
  • In this implementation without a custom adapter, yes you need to pass the textview id too so it'll know where to present the strings. what's the problem you're facing there? – Itay Feldman Aug 20 '19 at 09:13
  • When I put constraint layout and textview into listview I can't put margin to them – Niklaus Aug 20 '19 at 09:15
  • If you choose to put your own item layout and not use the internal one given there then just give top margin to the item's xml. – Itay Feldman Aug 20 '19 at 09:16
1

I would suggest you to use RecyclerView for this type of task. You may use ListView as well.

But RecyclerView is more flexible and advanced than ListView.

Create a simple layout or xml file for your row item to be shown in RecyclerView. Add that row xml file in onCreateViewHolder method. And inside method onBindViewHolder do necessary task like for example, showing name in the list for each position.

Go to this link for your reference : https://developer.android.com/guide/topics/ui/layout/recyclerview

Instead of Array<String> you can use Array<CustomModel> as well depending on your requirement.

Simple example of RecyclerView with model objects as list : https://www.javatpoint.com/android-recyclerview-list-example

Sneha Sarkar
  • 731
  • 6
  • 19
1

Well, the proper way to do what you need is use ListView or RecyclerView.

Anyway, if you want to use your current solution, you need to specify the position of each TextView.

For example, assign an ID to each textview you create and then set the position of it under the previous one. Here you can find how to do that.

MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35