Mon. Nov 25th, 2024

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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="androindian.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello"
        android:textSize="20sp"
        android:textColor="#234098"
        android:textAllCaps="true"
        android:textStyle="bold|italic"
        android:id="@+id/tv"
        android:typeface="monospace"
        android:gravity="center"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:textSize="20sp"
        android:textColor="#234098"
        android:textAllCaps="true"
        android:textStyle="bold|italic"
        android:id="@+id/bt"
        android:typeface="monospace"
        android:gravity="center"/>
</LinearLayout>

MainActivity.kt

package com.androinidan.button

import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.androinidan.button.R

class MainActivity : AppCompatActivity() {
    var textView: TextView? = null
    var button: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView = findViewById<View>(R.id.tv) as TextView
        button = findViewById<View>(R.id.bt) as Button
        button!!.setOnClickListener {
            textView!!.text = "Hi Hello Good morning"
        }
    }
}

output

After clicking the button

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *