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

5
  • 1
    how is the layout supposed to know where to position the textview if you don't tell it Commented Aug 20, 2019 at 8:27
  • This looks like a normal ListView (RecyclerView) use case. Commented Aug 20, 2019 at 8:28
  • I tried all these methods: stackoverflow.com/questions/3416087/…, but nothing happened Commented Aug 20, 2019 at 8:28
  • @VladyslavMatviienko is it possible with ListView (RecyclerView) to put them into boxes like on picture two Commented Aug 20, 2019 at 8:30
  • that's up to you how to design list view items. Commented Aug 20, 2019 at 8:31

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I don't understand this: ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
@Niklaus This is the declaration of the adapter. You mention that it receives strings <String>, 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.
Yes, but what are these parameters inside?
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
Do I need to put layout and textview into ListView, cause I have problem with these
|
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.