1

I'm creating a simple app for personal use and I'm stuck because I need to add/remove a text from my scrollview. In the screenshot below the blue square is my scrollview and the mail button (placeholder) needs to open a popup where I can put 2 values in 2 different boxes, a string and a double.

Main_Activity

I know how to do almost everything but I don't know how to add a text every time I click on the mail button and it should look like this when I click on it

this is how it should be when is done The left button of a car block is the "delete from scrollview" button that removes it from the scrollview and preferences and the right button that does like the 2nd image but where I can edit it.

My questions are:

How can I add a "block" of items in a scroll view? Per block I mean: Edit and delete button and the text like in the example.

Suggestions on what to use for the popup that adds 2 input fields?

tivir13126
  • 21
  • 1
  • 3

1 Answers1

1

In my opinion, I suggest to use recyclerView instead because of performance issues. link

But, if you need to do the way using ScrollView just follow this.

In your xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_dark"
        android:fillViewport="true"
        android:padding="16dp">

        <LinearLayout
            android:background="@android:color/holo_red_light"
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

And add item into linear layout when handle click event.

  val linear = view.findViewById<LinearLayout>(R.id.linearLayout)
        val item =
            LayoutInflater.from(requireContext()).inflate(R.layout.item_list_row, linear, false)
        linear.addView(item)
Dharman
  • 30,962
  • 25
  • 85
  • 135
siwarak
  • 177
  • 13
  • Sorry I may be too much hurry.
    For multiple input you can inflate sub view into Alert dialog like this https://stackoverflow.com/questions/16169787/how-to-add-two-edit-text-fields-or-views-in-an-alertdialog-box
    – siwarak Nov 05 '20 at 04:23
  • I will probably save the strings as array and same for the double. I will probably have something like: cars[a,b,c,d,e], price[1,2,3,4,5] if I use the button in the recycleview how can i remove the car corresponding to the button pressed? Like I delete the car c I want to remove the price 3. All this is in my sharedPreferences – tivir13126 Nov 05 '20 at 04:38
  • You can follow this quick sample https://github.com/siwarakDee/AddCar – siwarak Nov 05 '20 at 07:01