Lesson 5: How to implement CheckBox Control 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

The objective of this concept is to continue practicing projects with Android Studio and incorporate the visual control CheckBox.

Create a project called Project004.

Problem:

Load two numbers into controls of type EditText (“Number”). Show in the “hint” properties of each component a message requesting the loading of the values. Have two CheckBox controls to select if we want to add and/or subtract those values. Finally, by means of a Button-type control, we perform the respective operation. We show the results in a TextView.

The new thing in this problem is the insertion of two objects of the class CheckBox that is in the tab “Widgets“:

Lesson 5: How to implement CheckBox Control and example - Lesson 5: How to implement CheckBox Control and example - Lesson 5: How to implement CheckBox Control and example -

We must start the “text” properties to display a text and the “id” property to refer to the CheckBox in the program in Kotlin.

The first CheckBox defines its “id” with the value check1 and the second with the value check2.

Then the final graphic interface for this problem and the names of the controls or visual components can be seen in the “Component Tree” window.

Keep in mind that we must always press the “Infer Constraints” icon to link the objects and locate them.

Control that we set the values of the properties “id” of each object: et1, et2, check1, check2, tv1 and button.

Source code:

package com.coding180.project001


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

class MainActivity: AppCompatActivity () {

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
//coding180.com
        val et1 = findViewById (R.id.et1) as EditText
        val et2 = findViewById (R.id.et2) as EditText
        val cb1 = findViewById (R.id.check1) as CheckBox
        val cb2 = findViewById (R.id.check2) as CheckBox
        val tv1 = findViewById (R.id.tv1) as TextView
        val boton1 = findViewById (R.id.button) as Button
        boton1.setOnClickListener {
            var result = ""
            if (cb1.isChecked)
            result = "sum = ${et1.text.toString().toInt()+et2.text.toString().toInt ()}"
            if (cb2.isChecked)
            result += "Subtract = ${et1.text.toString().toInt() - et2.text.toString().toInt ()}"
            tv1.text = result
        }
    }
}

In the onCreate method, we obtain the reference of the six objects:

        val et1 = findViewById (R.id.et1) as EditText
        val et2 = findViewById (R.id.et2) as EditText
        val cb1 = findViewById (R.id.check1) as CheckBox
        val cb2 = findViewById (R.id.check2) as CheckBox
        val tv1 = findViewById (R.id.tv1) as TextView
        val boton1 = findViewById (R.id.button) as Button

When the button is pressed we check each CheckBox and concatenate in a String the sum if the first CheckBox and subtraction is selected, then we show the result in the TextView:

        boton1.setOnClickListener {
            var result = ""
            if (cb1.isChecked)
                result = "sum = $ {et1.text.toString () .toInt () + et2.text.toString () .toInt ()}"
            if (cb2.isChecked)
                result + = "Subtract = $ {et1.text.toString () .toInt () - et2.text.toString () .toInt ()}"
            tv1.text = result
        }

When we run the program in the emulator we have:

Lesson 5: How to implement CheckBox Control and example - Lesson 5: How to implement CheckBox Control and example - Lesson 5: How to implement CheckBox Control and example -

About The Author

Related posts

Leave a Reply