Saturday, February 17, 2018

android kotlin - Create CardView programmatically

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.support.v7.widget.CardView
import android.widget.ImageView
import android.widget.LinearLayout.LayoutParams
import android.widget.Toast


class MainActivity : AppCompatActivity() {

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

        // Set a click listener for button widget
        button.setOnClickListener{

            // Initialize a new CardView instance
            val card_view = CardView(this)

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

            // Set bottom margin for card view
            layoutParams.bottomMargin = 50

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

            // Set the card view corner radius
            card_view.radius = 12F

            // Set the card view content padding
            card_view.setContentPadding(25,25,25,25)

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

            // Set card view elevation
            card_view.cardElevation = 8F

            // Set card view maximum elevation
            card_view.maxCardElevation = 12F

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

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

            // Finally, add the CardView in root layout
            root_layout.addView(card_view)
        }
   }


    // Custom method to generate an image view
    private fun generateImageView(): ImageView{
        val imageView = ImageView(this)
        val params = LayoutParams(LayoutParams.WRAP_CONTENT,350)
        imageView.layoutParams = params
        imageView.setImageResource(R.drawable.flower)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        return imageView
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    android:background="#ffffff"
    >
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create CardView"
        />
</LinearLayout>