1

I set the margin of the view (card view) in my xml of my item file, this xml item file will be used in for my recyclerView adapter.

As you can see in my xml below, that I have given margin to top, bottom, start and end. and I want to change the margin from my fragment

Here is my xml file, item_category_list.xml:

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView
        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="wrap_content"
        app:cardCornerRadius="8dp"
        app:cardElevation="4dp"
        android:id="@+id/cardView_item_category_list" android:layout_marginStart="8dp" android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp" android:layout_marginTop="8dp">

    <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="wrap_content"
            android:background="@android:color/background_light">

        <ImageView
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:srcCompat="@drawable/logo_apps"
                android:id="@+id/categoryImageView_Item"
                android:layout_marginTop="16dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="24dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="24dp"
                app:layout_constraintDimensionRatio="w,1:1" android:scaleType="centerCrop"/>

        <TextView
                android:text="@string/Category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/categoryName_textView_item"
                app:layout_constraintTop_toBottomOf="@+id/categoryImageView_Item"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginStart="4dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="4dp"
                android:textAlignment="center"
                android:minLines="1"
                android:maxLines="2"
                app:autoSizeTextType="uniform"
                app:autoSizeMinTextSize="10sp"
                app:autoSizeMaxTextSize="15sp"
                app:autoSizeStepGranularity="1sp"
                android:layout_marginBottom="24dp"
                android:layout_marginTop="24dp"/>


    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>

Here is the adapter:

class CategoryAdapter(val context: Context, val categories: List<Category>) : RecyclerView.Adapter<CategoryAdapter.ViewHolderCategory>() {

    private lateinit var mListener : CategoryAdapterListener

    interface CategoryAdapterListener {
        fun onItemClick(position: Int)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderCategory {

        val layoutInflater = LayoutInflater.from(parent.context)
        val itemView = layoutInflater.inflate(R.layout.item_category_list,parent, false)
        return ViewHolderCategory(itemView,mListener)
    }

    override fun getItemCount(): Int {
        return categories.size
    }

    override fun onBindViewHolder(holder: ViewHolderCategory, position: Int) {

        val category = categories[position]

        holder.categoryNameTextView.text = category.name
        Glide
            .with(context)
            .load(category.getFormattedImageURL())
            .into(holder.categoryImageView)

    }

    inner class ViewHolderCategory(itemView: View, listener: CategoryAdapterListener) : RecyclerView.ViewHolder(itemView) {

        val categoryImageView = itemView.findViewById<ImageView>(R.id.categoryImageView_Item)
        val categoryNameTextView = itemView.findViewById<TextView>(R.id.categoryName_textView_item)
        val cardView = itemView.findViewById<CardView>(R.id.cardView_item_category_list)

        init {

            itemView.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(position)
                }
            }

        }

    }

    fun setCategoryAdapterListener(listener: CategoryAdapterListener) {
        mListener = listener

    }
}

and in the fragment, I set the adapter to the recycler view:

val categoryAdapter = CategoryAdapter(mContext,parentCategory)
val layoutManager = GridLayoutManager(mContext,4,RecyclerView.VERTICAL,false)

recyclerViewParentCategory.adapter = categoryAdapter
recyclerViewParentCategory.layoutManager = layoutManager
recyclerViewParentCategory.setHasFixedSize(true)

I want to change that margin in card view in my item_category_list.xml programatically in my java/kotlin file (in my fragment file), so I can change the margin from my fragment.

So how can I achieve it ? Java/Kotlin any language is preferred.

Alexa289
  • 8,089
  • 10
  • 74
  • 178

3 Answers3

0

You can do something like this. Make id of cardview , create instance of it in your adapter and do this code

ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) myCardView.getLayoutParams(); layoutParams.setMargins(10, 10, 10, 10);

myCardView.requestLayout();
Vivek Shah
  • 380
  • 2
  • 8
0

To set margins to the cardView, you will have to create layoutParams, set margins to it and then set it as cardView LayoutParams like:

inner class ViewHolderCategory(itemView: View, listener: CategoryAdapterListener) : RecyclerView.ViewHolder(itemView) {

        val categoryImageView = itemView.findViewById<ImageView>(R.id.categoryImageView_Item)
        val categoryNameTextView = itemView.findViewById<TextView>(R.id.categoryName_textView_item)
        val cardView = itemView.findViewById<CardView>(R.id.cardView_item_category_list)
        //Main code here
        val lparams=LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)
        lparams.setMargins(leftMargin,topmargin,rightMargin,bottomMargin)
        cardView?.layoutParams=lparams
}
ankuranurag2
  • 2,300
  • 15
  • 30
0

First Of all its a long way . So i'm just suggesting a way .

  1. First of all . In your Fragment when some action happen you need to change cardview size in adapter list item xml.

    So . You need a interface for that (Let's say interface ChangeMargin). create interface in Fragment and implement that interface in your adapter like this

class CategoryAdapter(val context: Context, val categories: List<Category>):RecyclerView.Adapter<CategoryAdapter.ViewHolderCategory>(),ChangeMargin()

For how to create interface so can go through this

  1. Now in that interface you need to get cardview and assign new margin .

 @Override
         public void ChangeMargin() {
         val linear_params=LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)
                 linear_params.setMargins(leftMargin,topmargin,rightMargin,bottomMargin)
                 cardView?.layoutParams=linear_params
         }

and don't forget to notify adapter

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51