0

I am not able to see MaterialViewCard at my Activity when adding programmatically. If I add directly on XML, it shows. But When adding via Kotling, it doesn't.

XML sample:

<?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"
    tools:context=".SecondFragment">

    <LinearLayout
        android:id="@+id/layout_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">
        <LinearLayout
            android:id="@+id/layout_first_block"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <com.google.android.material.card.MaterialCardView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:cardBackgroundColor="@color/surface"
                    android:layout_margin="2dp">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="00"
                        android:textColor="@color/on_surface"/>
                </com.google.android.material.card.MaterialCardView>
                <com.google.android.material.card.MaterialCardView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:cardBackgroundColor="@color/surface"
                    android:layout_margin="2dp">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="01"
                        android:textColor="@color/on_surface"/>
                </com.google.android.material.card.MaterialCardView>
                <com.google.android.material.card.MaterialCardView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:cardBackgroundColor="@color/surface"
                    android:layout_margin="2dp">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="03"
                        android:textColor="@color/on_surface"/>
                </com.google.android.material.card.MaterialCardView>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_weight="1">
            <Button
                android:id="@+id/button_second"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/previous"/>
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Result: Cards added via XML

But when I try this (below) won't work. 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"
    tools:context=".SecondFragment">

    <LinearLayout
        android:id="@+id/layout_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">
        <LinearLayout
            android:id="@+id/layout_first_block"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1">
<!--            Will add here-->
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_weight="1">
            <Button
                android:id="@+id/button_second"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/previous"/>
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        view.findViewById<Button>(R.id.button_second).setOnClickListener {
            findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
        }

        val layoutFirstBlock: LinearLayout = view.findViewById<LinearLayout>(R.id.layout_first_block)

        for (r in 1..10){
            val row: LinearLayout = LinearLayout(context)
            row.orientation = LinearLayout.HORIZONTAL
            row.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)

            for(c in 1..10){
                val card: MaterialCardView = MaterialCardView(context)
                val marginParams: ViewGroup.MarginLayoutParams = ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                marginParams.setMargins(R.dimen.small_padding, R.dimen.small_padding, R.dimen.small_padding, R.dimen.small_padding)
                card.layoutParams = marginParams
                card.setBackgroundColor(resources.getColor(R.color.surface, context?.theme))

                val num: TextView = TextView(context)
                num.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                val value = ((r-1)*10 + c).toString()
                num.text = value
                num.setTextColor(resources.getColor(R.color.on_surface, context?.theme))

                Log.d("i", "i$r$c = $value")

                card.addView(num)
                row.addView(card)
            }
            layoutFirstBlock.addView(row, 0)
        }
    }

Result: Cards missing when added by Kotlin

Any ideas?

Thanks!

  • Not sure about your issue, but suggestions for debugging. 1) use hierarchy viewer to confirm the views are added correctly 2) Use "Show layout bounds" debug option to confirm if the views are laid out correctly 3) Declare subviews in xml and inflate them instead of doing it programmatically 4) Consider using a RecyclerView with a GridLayoutManager instead. – dominicoder Apr 11 '21 at 16:44
  • By the layout inspector saw that second LinearLayout didn't have any height. Thank you, man! – Caio Guilherme Apr 16 '21 at 23:40

2 Answers2

1

In my case the issue was i have a component above the cardview and i have set its height to match_parent,when i gave the height to wrap_content the issue has been solved.

If it doesn't work also check in build gradle for below line.

implementation 'com.google.android.material:material:1.0.0'
Akhil
  • 419
  • 5
  • 15
0

Firstly,

You need to generate id's of each view as well.

val card: MaterialCardView = MaterialCardView(context)
card.id = View.generateViewId()

and

val num: TextView = TextView(context)
num.id = View.generateViewId()

Secondly,

I think, you should need to update index of

layoutFirstBlock.addView(row, 0)

to

layoutFirstBlock.addView(row, r)
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23