Tuesday, 6 March 2018

Android TextView , EditText ,Button and Toast Example Using Kotlin

 

 

1. Creating New Project in Kotlin

Step
Description
1.
Open Android Studio.
2.
Go to File => New => New Project.Give Name To your application. Then, check Include Kotlin Support and click next button.
3.
Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4.
Then, select Empty Activity => click next => click finish.
5.
If you have followed above process correctly, you will get a newly created project successfully.
2.Now Go to app/res/layout and select activity_main.xml file.And Replace your code with following code.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  
 
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:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical">

    <EditText 
        android:id="@+id/editTextName" 
        android:layout_width="match_parent"         
        android:layout_height="wrap_content" 
        android:ems="10"         
        android:inputType="textPersonName"         
        android:hint="Enter Your Name" />

    <TextView 
       android:id="@+id/textViewName"         
       android:textStyle="bold" 
       android:textAlignment="center" 
       android:layout_marginTop="20dp" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/button" 
        android:layout_marginTop="20dp" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Click Me" />
</LinearLayout>



3.Now Open Your MainActivity.kt and past Following Code
 
package com.dharmendra.widgetexample

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {


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

        //FindViewByID for All your Widget


        val textView = findViewById<TextView>(R.id.textViewName) as TextView

        val button = findViewById<Button>(R.id.button) as Button

        val editText = findViewById<EditText>(R.id.editTextName) as EditText


        //Now we Create OnClickListner for Button Click which get Value from EditText and Set in Textview

        button.setOnClickListener {

            //Get Data from Edittext and Store in String

            var name: String = editText.text.toString()

            //set Value in TextView if name is not Empty other wise Display toast Message

            if (name.isEmpty()) {
                Toast.makeText(this@MainActivity, "Please Enter Something in EditText", Toast.LENGTH_LONG).show()
                textView.setText("")
            } else {
                textView.setText("Hello " + name)
            }

        }

    }
}  


Output:


No comments:

Post a Comment