Sunday, June 3, 2018

android kotlin - Enum example

MainActivity.kt

package com.cfsuman.test

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // Basic enum class
        button.setOnClickListener{
            val builder = StringBuilder()
            builder.append("Basic usage of enum class")
            builder.append("\n\nPosition.Top = ${Position.Top}")
            builder.append("\nPosition.Right = ${Position.Right}")
            builder.append("\nPosition.Bottom = ${Position.Bottom}")
            builder.append("\nPosition.Left = ${Position.Left}")
            textView.text = builder.toString()
        }


        // Enum with values
        button2.setOnClickListener{
            val builder = StringBuilder()
            builder.append("Enum with values")
            builder.append("\n\nBangladesh = ${Capital.Bangladesh.capital}")
            builder.append("\nIndia = ${Capital.India.capital}")
            builder.append("\nSweden = ${Capital.Sweden.capital}")
            builder.append("\nAustralia = ${Capital.Australia.capital}")
            builder.append("\nQatar = ${Capital.Qatar.capital}")
            textView.text = builder.toString()
        }


        // Enum with multiple values
        button3.setOnClickListener{
            val builder = StringBuilder()
            builder.append("Enum with multiple values")
            builder.append("\n\nAustria = ${Country.Austria.capital} : ${Country.Austria.currency}")
            builder.append("\nChina = ${Country.China.capital} : ${Country.China.currency}")
            builder.append("\nEgypt = ${Country.Egypt.capital} : ${Country.Egypt.currency}")
            builder.append("\nFrance = ${Country.France.capital} : ${Country.France.currency}")
            textView.text = builder.toString()
        }
    }
}



// Basic enum class
enum class Position{
    Top, Right, Bottom, Left
}


// Enum with values
enum class Capital(val capital:String){
    Bangladesh("Dhaka"),
    India("New Delhi"),
    Sweden("Stockholm"),
    Australia("Canberra"),
    Qatar("Doha")
}


// Enum with multiple values
enum class Country(val capital:String, val currency:String){
    Austria("Vienna","Euro"),
    China("Beijing","Chinese Yuan"),
    Egypt("Cairo","Egyptian Pound"),
    France("Paris","Euro; CFP Franc")
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Basic Enum"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="Enum Values"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Multi Values"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button"
        android:textColor="#ff1557"
        />
</android.support.constraint.ConstraintLayout>