Add view to a fragment programmatically in android studio

Viewed 19

Hello I am bulding a project which use fragments. There is a fragment which should has list of items using scroll view and linear layout and there is a button to add new item which navigate to another fragment to get the details of a the new item the problem is that after adding the details there is a button called save to add a view (card). I used the function add view but when I click on the button nothing happens. I used view model and live data but I don't know what is the problem.

This is the card xml code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/Linear_card"
    >

    <androidx.cardview.widget.CardView
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/round_border"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="26dp"
            android:fontFamily="@font/glamor"
            android:text="Name"
            android:textColor="@color/colorPrimary"
            android:textFontWeight="1000"
            android:textSize="40dp" />

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1"></Space>

        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:fontFamily="@font/glamor"
            android:lineSpacingExtra="40dp"
            android:text="Price"
            android:textFontWeight="1000"
            android:textSize="20dp" />

    </LinearLayout>

    <TextView
        android:id="@+id/company"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:fontFamily="@font/glamor"
        android:lineSpacingExtra="30dp"
        android:text="Company"
        android:textColor="@color/colorPrimary"
        android:textFontWeight="1000"
        android:textSize="20dp" />
</androidx.cardview.widget.CardView>

and this is the detail fragment xml

    <?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <variable
            name="newItem"
            type="com.example.skincareapp.NewItem" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/FragmentDetail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".DetailFragment"
        >

        <View
            android:id="@+id/view"
            android:layout_width="0dp"
            android:layout_height="188dp"
            android:background="@drawable/instruction_wave1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/view2"
            android:layout_width="0dp"
            android:layout_height="145dp"
            android:background="@drawable/instruction_wave2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:layout_width="0dp"
            android:layout_height="99dp"
            android:background="@drawable/instruction_view3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:background="@drawable/ic_wave4"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <EditText
            android:id="@+id/name_edit"
            android:layout_width="@dimen/input_width"
            android:layout_height="@dimen/input_height"
            android:layout_marginTop="92dp"
            android:background="@drawable/round_border"
            android:drawablePadding="@dimen/padding"
            android:ems="10"
            android:hint="Enter the product name"
            android:inputType="textPersonName"
            android:padding="@dimen/padding"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.489"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view"
            android:text="@{newItem.item_name}"
            />

        <EditText
            android:id="@+id/company_edit"
            android:layout_width="@dimen/input_width"
            android:layout_height="@dimen/input_height"
            android:layout_marginTop="16dp"
            android:background="@drawable/round_border"
            android:drawablePadding="@dimen/padding"
            android:ems="10"
            android:hint="Enter the product company"
            android:inputType="textPersonName"
            android:padding="@dimen/padding"
            app:layout_constraintBottom_toTopOf="@+id/cancel"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.489"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/price_edit"
            app:layout_constraintVertical_bias="0.0"
            android:text="@{newItem.item_company}"
            />

        <EditText
            android:id="@+id/price_edit"
            android:layout_width="@dimen/input_width"
            android:layout_height="@dimen/input_height"
            android:layout_marginTop="16dp"
            android:background="@drawable/round_border"
            android:drawablePadding="@dimen/padding"
            android:ems="10"
            android:hint="Enter the price"
            android:inputType="textPersonName"
            android:padding="@dimen/padding"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.489"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/name_edit"
            android:text="@{newItem.item_price}"
            />

        <Button
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="64dp"
            android:layout_marginBottom="144dp"
            android:background="@drawable/round_bg"
            android:text="cancel"
            android:textColor="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.956"
            app:layout_constraintStart_toEndOf="@+id/save" />

        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="144dp"
            android:background="@drawable/round_bg"
            android:text="save"
            android:textColor="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.247"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/add_product_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:fontFamily="@font/my_font"
            android:text="Add Product"
            android:textColor="@color/colorPrimary"
            android:textSize="30dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view2" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

and this is the fragment xml which I should add the item to it

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ShopFragmnet"
    >

    <ScrollView
        android:id="@+id/scroll_view_layout"
        android:layout_width="match_parent"
        android:layout_height="658dp"
        app:layout_constraintBottom_toTopOf="@+id/add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="SpeakableTextPresentCheck">

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

        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:background="@drawable/round_bg"
        android:text="Add"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

and finally this is the code of the detail fragment

    class DetailFragment : Fragment() {                    
    private val newItem:NewItem by activityViewModels()                        
    //private lateinit var newItem:NewItem                   
    private lateinit var cont_layout:LinearLayout                  
    override fun onCreateView(                         
        inflater: LayoutInflater, container: ViewGroup?,            
        savedInstanceState: Bundle?           
    ): View? {               
       val binding:FragmentDetailBinding =           DataBindingUtil.inflate(inflater,R.layout.fragment_detail,container,false)                
        val binding2:FragmentShopBinding =                     DataBindingUtil.inflate(inflater,R.layout.fragment_shop,container,false)                  
         cont_layout = binding2.containerLayout                       
        //newItem = ViewModelProvider(this).get(newItem::class.java)                   
        binding.save.setOnClickListener {                   
            newItem.saveName(binding.nameEdit.text.toString())                      
            newItem.saveCompany(binding.companyEdit.text.toString())               
            newItem.savePrice(binding.priceEdit.text.toString())                 
            addCard(newItem)                 
            findNavController().navigate(R.id.action_detailFragment_to_shopFragment)             
        }                   
        binding.cancel.setOnClickListener { view:View ->              
            view.findNavController().navigate(DetailFragmentDirections.actionDetailFragmentToShopFragment())       
        }                  
        return binding.root                
    }
    private fun addCard(newItem: NewItem) {                  
        val view:View = layoutInflater.inflate(R.layout.card,null,false)             
        val name_t:TextView = view.findViewById(R.id.name)                  
        val company_t:TextView = view.findViewById(R.id.company)                
        val price_t:TextView = view.findViewById(R.id.price)                    
        name_t.setText(newItem.itemName.value.toString())                  
        company_t.setText(newItem.itemCompany.value.toString())                 
        price_t.setText(newItem.itemPrice.value.toString())                
        cont_layout.addView(view)                  
    } }



note : this also is the view model class which I used:

    class NewItem: ViewModel() {               
     var item_name = MutableLiveData("")               
     var item_price = MutableLiveData("")            
     var item_company = MutableLiveData("")            
     val itemName:LiveData<String> = item_name           
     val itemPrice:LiveData<String> = item_price           
    val itemCompany:LiveData<String> = item_company              

    fun saveName(newname:String){
        item_name.value = newname
    }
    fun savePrice(newPrice:String){
        item_price.value = newPrice
    }
    fun saveCompany(newCompany:String){
        item_company.value = newCompany
    }   
}
0 Answers
Related