androidkotlintextview

android studio - how to add a new textView by pressing button in scrollView in fragment


there is a certain button, when pressed, a new TextView should appear in ScrollView, but the problem is that all solutions to similar tasks are made for activity, and i need it for Fragment, and you can explain the code, I'm a beginner. Thank you in advance

binding.addButton.setOnClickListener(){
    binding.layout.addView(binding.textView,0,0)
}

but after clicking, the application crashes

class HomeFragment : Fragment() {
    private lateinit var binding: FragmentHomeBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentHomeBinding.inflate(inflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.addButton.setOnClickListener(){
            binding.layout.addView(binding.textView,0,0)
        }
    }
  }

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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="onClick"
    tools:context=".HomeFragment">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Home"/>

    <ImageButton
        android:id="@+id/addButton"
        android:layout_width="59dp"
        android:layout_height="54dp"
        android:onClick="createTextView"
        android:scaleType="fitStart"
        android:src="@drawable/ic_add_button"
        android:tooltipText="text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.843"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.961" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • create new fun "createText" in mainActivity

    public fun createText(text: String, color: Int): TextView{
            val tv_dynamic = TextView(this)
            tv_dynamic.text = text
            tv_dynamic.setBackgroundColor(color)
            tv_dynamic.setLayoutParams(
                LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
            )
            return tv_dynamic
        }
    

    add new fun in fragment

      private fun addTextView(){
            val newText = (activity as MainActivity).createText("Hello!",R.color.black)
            binding.layout.addView(newText)
        }
    

    add it code to "onViewCreated" in fragment

    binding.addButton.setOnClickListener() {
        addTextView()
    }