Android Kotlin Tutorial Day 2

Android Kotlin Tutorial Day 2

Learning Objective: Let’s start playing with Kotlin Language and do some basic operations like on button click display toast message to users, take input from edit text and display in textview and more.

Button Click Event – Display Toast Message

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.btnclickme.setOnClickListener {

            Toast.makeText(context, "Hello I am Toast :)", Toast.LENGTH_SHORT).show()
        }
    }

Read EditText Value > Convert to Integer and Do Sum

 binding.btnsum.setOnClickListener {

            var no1 = binding.et1.text.toString();
            var no2 = binding.et2.text.toString();
            var sum = no1.toInt() + no2.toInt();

            binding.tvresult.text = sum.toString();
        }

Layout Code for above ones

<?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"
    tools:context=".FirstFragment">


    <Button
        android:id="@+id/btnclickme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me" />

    <EditText
        android:id="@+id/et1"
        android:hint="Enter Number 1 "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <EditText
        android:id="@+id/et2"
        android:hint="Enter Number 2 "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btnsum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sum" />

    <TextView
        android:id="@+id/tvresult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

Move from FirstFragment to SecondFragment after 5 Seconds

   Handler().postDelayed({
            findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)

        }, 5000)