Wednesday 7 August 2019

Kotlin : TextViews , EditText & Buttons(Post 8)

Hi ,

In previous post , I have started android development , with Kotlin programming language.
Now let's take a look how Textviews and Edittext will work .

I have created one layout , having 1 EditText , 1 TextView and 1 Button in it.

  • EditText will use , to get the input from the user. 
  • Input validation will check by clicking on Button . 
  • TextView is used to check the validation status of input and then change its text accordingly.


labels_layout.xml :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText"
android:layout_margin="20dp"
/>

<Button
android:text="Validate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_margin="20dp"/>

<TextView
android:text="Check Validation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:padding="10dp"
android:textColor="#FFFFFF"/>
</LinearLayout>



LabelActivity.kt

package com.kotlin_application 
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 
import kotlinx.android.synthetic.main.labels_layout.* 

class LabelActivity : AppCompatActivity() 

var isValid : Boolean = false 

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

private fun inItView(){ 

val textView = findViewById<TextView>(R.id.textView2) 
val button = findViewById<Button>(R.id.button) 
val ediText = findViewById<EditText>(R.id.editText) editText.setText(R.string.app_name) 

button.setOnClickListener{ 
var inputValue : String = ediText.text.toString()

if (inputValue.isEmpty() || inputValue== null){

Toast.makeText(this, "Please enter something!", Toast.LENGTH_LONG).show() 
isValid = false


else{

Toast.makeText(this, "You have entered: "+inputValue, Toast.LENGTH_LONG).show()
 isValid = true 

}

}

textView.setOnClickListener 
var inputValue : String = ediText.text.toString()

if ((inputValue.isEmpty() || inputValue== null) && !isValid){

 Toast.makeText(this, "Not Validate", Toast.LENGTH_LONG).show()
textView.text = "Can not login , as input is not valid" 


else{ 

Toast.makeText(this, "Validate String : "+inputValue, Toast.LENGTH_LONG).show() 

textView.text = "Login successful" 

}
  • Here you can see , the click listeners are different , than java . 
  • To set Text on TextView , we need to just : textView.text = "Login successful"
  • To get the value from the EditText , we have to call it like this:
  •  var inputValue : String = ediText.text.toString()
  • Toast is same as java. 
Buttons:

There are below type , by which we can define the click listener for buttons :


button1.setOnClickListener()
{              
Toast.makeText(this,"button 1 clicked", Toast.LENGTH_SHORT).show()         
}  


Here are some screenshots: 
  
 

 

That's it for this post . Will continue with Intents in next post. 

No comments:

Post a Comment

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...