Kotlin for Android 2 : Android Button

Today we will learn about Android TextView in Kotlin. This article includes 2 content :

  • New Button programmatically
  • Button OnClickListenner
  • Dynamically change button background

1. New Button programmatically
 
In this articles, we will learn about create a Button programmatically and add the Button to a LinearLayout using Kotlin
 We create a new project Kotlin and follow 2 file.
 Android_main.xml : create 1 layout with id : ‘ll_main_layout’

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tvsoft.kotlintutorial.MainActivity">

<LinearLayout
android:id="@+id/ll_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

</LinearLayout>

</android.support.constraint.ConstraintLayout>

MainActivity.kt: create a new Button with text “Dynamic button” and add it to the LinearLayout.

package com.tvsoft.kotlintutorial

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout

class MainActivity : AppCompatActivity() {

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

val llMainLayout = findViewById<LinearLayout>(R.id.ll_main_layout)
// create button
val btnDynamic = Button(this)
// setting button
btnDynamic.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
btnDynamic.text = "Dynamic button"
// add button to layout
llMainLayout.addView(btnDynamic)

}
}

And output

2. Button OnClickListenner
 
We use the method setOnClickListenner() on the object of Button

btnDynamic.setOnClickListener {
Toast.makeText(this, "Clicked Button", Toast.LENGTH_SHORT).show()
}

3.Dynamically change button background
 
Create 2 background .xml in drawable folder
 /app/src/main/res/drawable/btn_background_state_one.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#47A891"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>

/app/src/main/res/drawable/btn_background_state_two.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#2808A8"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>

In MainActivity.kt :

// change background when clicked button
var btnBackgroundState : Int = 1

btnDynamic.setOnClickListener {
if (btnBackgroundState == 1){
btnDynamic.setBackgroundResource(R.drawable.btn_background_state_two)
btnBackgroundState = 2
} else if (btnBackgroundState == 1){
btnDynamic.setBackgroundResource(R.drawable.btn_background_state_one)
btnBackgroundState = 1
}
}

And output

Thank you for reading my post!


Originally published at Code for fun.