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. 

Tuesday 6 August 2019

Kotlin : Hello World Android App (Post 7)

Hi ,

After the basic of Kotlin App. Let's start android development with Kotlin language. In this post I am going to share the demo for simple hello world program with Kotlin language.

For setup of Kotlin Support , android studio will be required of 3.0 version and more.At the time of language selection , we need to select "kotlin" for programming language , when we create a new application.

When creating the android application with kotlin , we do not need to change the layouts , just src files will be changed.

Here I will just create one layout having textview in it ,and attach the activity with this textview . Let's have a look :

activity_main.xml :

<?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"
 xmlns:app="http://schemas.android.com/apk/res-auto"  
      android:layout_width="match_parent"  
      android:layout_height="match_parent" 
       tools:context=".MainActivity">

    <TextView    
      android:id="@+id/textView" 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Hello World!"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>



MainActivity.kt:

package com.kotlindemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

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

Run the code:


That's it for this post , in next post I will discuss how TextViews and EditText is used in Kotlin.




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...