Lesson 10 – Control EditText and example

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

Contents

From the first problem, we have been using the control that allows in Android to enter values by keyboard. The class that manages keyboard input is EditText.

But in the component palette we see that there are many types of EditText.

As we can see in the tab “Text” are all types of EditText that offers us Android to use in our applications: Password, E-mail, Number, etc.

Depending on the type of data input we need, we will use a specific type of EditText.

Create a project called Project009.

Problem:

Make an application for Android that allows you to enter the username and its key in two controls of type EditText.

Check when pressing a button if any text was entered in the key. If no text was entered inform by a notification said situation.

Use the length property of the String class to see how many characters were entered.

The visual interface should be similar to this:

Lesson 10 - Control EditText and example - Lesson 10 - Control EditText and example - Lesson 10 - Control EditText and example -

The EditText for the entry of the name is of type “Plain Text”, we start the property “hint” with the value “enter name”. The second EditText is of type “Password” and we initiate the property “hint” with the value “enter key”.

Define the ID of the two controls of type EditText with the values “et1” and “et2”. We start the id property of the Button with the value “boton1”

Remember to link objects by pressing the “Infer constraints” icon.

Source code:

package com.coding180.project001


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

class MainActivity: AppCompatActivity () {

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
// coding180.com
        val et2 = findViewById (R.id.et2) as EditText
        val boton1 = findViewById (R.id.button1) as Button
        boton1.setOnClickListener {
            if (et2.text.toString().length == 0)
            Toast.makeText (this, "The key can not be empty", Toast.LENGTH_LONG) .show ()
        }
    }
}

As we can see when pressing the “verify” button we proceed to extract the contents of the EditText of the key and the length property we control if it has zero characters, if so we show the notification on screen:

        boton1.setOnClickListener {
            if (et2.text.toString (). length == 0)
                Toast.makeText (this, "The key can not be empty", Toast.LENGTH_LONG) .show ()
        }

On screen we will have a result similar to this if a key is not entered:

Lesson 10 - Control EditText and example - Lesson 10 - Control EditText and example - Lesson 10 - Control EditText and example -

About The Author

Related posts

Leave a Reply