Friday, March 2, 2018

android kotlin - LayoutInflater example

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // Get the LayoutInflater from Context
        val layoutInflater:LayoutInflater = LayoutInflater.from(applicationContext)

        // Inflate the layout using LayoutInflater
        val view: View = layoutInflater.inflate(
                R.layout.another_view, // Custom view/ layout
                root_layout, // Root layout to attach the view
                false // Attach with root layout or not
        )

        // Find the text view from custom layout
        val label = view.findViewById<TextView>(R.id.label)

        // Get the ImageView widget reference from custom view
        val imageView = view.findViewById<ImageView>(R.id.image_view)

        // Set the text of custom view's text view widget
        label.text = "Cute Flower."

        // Set an image to image view widget
        imageView.setImageDrawable(
                ContextCompat.getDrawable(applicationContext,R.drawable.flower)
        )

        // Finally, add the view/custom layout to the activity root layout
        root_layout.addView(view,0)
    }
}
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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cedeca"
    android:orientation="vertical"
    android:padding="16dp"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello Android!"
        android:textSize="25sp"
        android:layout_margin="25dp"
        />
</LinearLayout>
another_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#87cf3f"
    android:padding="10dp"
    >
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="350dp"
        android:scaleType="center"
        />
    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:padding="10dp"
        />
</LinearLayout>