kotlin android textview

In this article today learn how to declare textview in  kotlin android so follow the example of the kotlin android textview.

follow the source code  kotlin android textview:

1.MainActivity.kt:

import androidx.appcompat.app.AppCompatActivity 
import android.os.Bundle 
import android.widget.TextView 
import android.widget.Toast 
  
class MainActivity : AppCompatActivity() { 
  
        override fun onCreate(savedInstanceState: Bundle?) { 
            super.onCreate(savedInstanceState) 
            setContentView(R.layout.activity_main) 
  
            //accessing our textview from layout 
            val textView = findViewById<TextView>(R.id.text_view_id) as TextView 
            textView?.setOnClickListener{ Toast.makeText(this@MainActivity, 
                R.string.text_on_click, Toast.LENGTH_LONG).show() } 
        } 
  


2.activity_main:


<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"> 
  
    <!--EditText with id editText-->
  
    <TextView
        android:id="@+id/text_view_id"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/text_view"
        android:textColor="#008000"
        android:textSize="40dp"
        android:textStyle="bold"/> 
</LinearLayout> 

Comments

Popular posts from this blog

Kotlin camera intent example

android kotlin current date example

android kotlin listview example