Saturday, February 17, 2018

android kotlin - Create CardView programmatically

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.graphics.Color
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams


class MainActivity : AppCompatActivity() {
    private lateinit var context:MainActivity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Get the context
        context = this

        // Get the widgets reference from XML layout
        val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout)
        val button = findViewById<Button>(R.id.button)


        // Set a click listener for button widget
        button.setOnClickListener{
            // Disable the button itself
            it.isEnabled = false

            // Initialize a new CardView instance
            val cardView = CardView(context)

            // Initialize a new ConstraintLayout LayoutParams instance
            // CardView width and height
            val layoutParams = LayoutParams(
                LayoutParams.MATCH_PARENT, // CardView width
                LayoutParams.WRAP_CONTENT // CardView height
            )

            // Set top margin for card view
            layoutParams.topMargin = 64

            // Put the CardView bottom of the button
            layoutParams.topToBottom = button.id

            // Set the card view layout params
            cardView.layoutParams = layoutParams


            // Set the CardView other configurations
            cardView.apply {
                // Set the card view corner radius
                radius = 12F

                // Set the card view content padding
                setContentPadding(32,32,32,32)

                // Set the card view background color
                setCardBackgroundColor(Color.LTGRAY)

                // Set card view elevation
                cardElevation = 8F

                // Set card view maximum elevation
                maxCardElevation = 12F

                // Set a click listener for card view
                setOnClickListener{
                    Toast.makeText(
                        applicationContext,
                        "Card clicked.",
                        Toast.LENGTH_SHORT).show()
                }

                // Add an ImageView to the CardView
                cardView.addView(generateImageView())
            }


            // Finally, add the CardView in root layout
            rootLayout.addView(cardView)
        }
    }


    // Custom method to generate an image view
    private fun generateImageView(): ImageView{
        val imageView = ImageView(context)
        val params = LayoutParams(
            LayoutParams.WRAP_CONTENT,
            650
        )
        imageView.layoutParams = params
        imageView.setImageResource(R.drawable.flower2)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        return imageView
    }
}
activity_main.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create CardView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples