Samaresh Sautia
Samaresh Sautia

Reputation: 1

Button is visiable but TextView / ImageView is not visiable in custom compound view

I am trying to create compound CardView using two CardView and extended Framelayout. If I place a button inside custom CardView as a child the button is visible but if i place TextView or ImageView as a child the widget (TextView or ImageView) is not visible.

Screenshot of Custom widget with TextView as child

Screenshot of Custom Widget with Button as child

kotlin code

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.cardview.widget.CardView


class CompoundCardView : FrameLayout{

    private var childView:CardView? = null

    private var heading:String? = null
        set(value) {
            field = value
            invalidate()
            requestLayout()
        }
        get() {
        return field
        }

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context, attrs)
    }


    constructor(context: Context,
                attrs: AttributeSet,
                defStyle: Int) : super(context, attrs, defStyle) {
        init(context, attrs)
    }
    private fun init(context: Context, attrs: AttributeSet?) {
        context.theme.obtainStyledAttributes(attrs,R.styleable.CompoundCardView,0,0).apply {
            try {
                heading = getString(R.styleable.CompoundCardView_heading)

            }finally {
                recycle()
            }
        }

        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        inflater.inflate(R.layout.compaund_card_view,this,true)
        childView = findViewById(R.id.holder)
        val header:TextView = findViewById(R.id.header)
        header.text = heading
    }

    override fun addView(child: View?) {
        invalidate()
        childView?.addView(child,0, child?.layoutParams)
        requestLayout()
    }
}       

xml layout

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

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:background="#00FFFFFF"
        app:cardCornerRadius="8dp"
        app:strokeColor="#2196F3"
        app:strokeWidth="1dp" />

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:backgroundTint="#FFC107"
        app:cardCornerRadius="8dp"
        app:contentPaddingBottom="4dp"
        app:contentPaddingLeft="8dp"
        app:contentPaddingRight="8dp"
        app:contentPaddingTop="4dp"
        app:strokeColor="#2196F3"
        app:strokeWidth="1dp">

        <TextView
            android:id="@+id/header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/heading"
            android:textSize="20sp" />
    </com.google.android.material.card.MaterialCardView>
</merge>

Upvotes: 0

Views: 44

Answers (0)

Related Questions