Animation in Android/Kotlin

View animation in Android is a system that allows you to add smooth, tweened animations to your app’s Views. This means you can control how elements like buttons, images, and layouts change their appearance over time, creating a more engaging and dynamic user experience.

Here are some key things to know about view animation in Android:

Types of animations:

  • Tween animation: This is the most common type, where you define the starting and ending values for various properties like alpha (transparency), rotation, scale, and translation (position). The system automatically calculates the intermediate values to create a smooth transition.
  • Frame animation: This involves displaying a sequence of images rapidly to create the illusion of movement.

Implementing animations:

There are two main ways to implement view animations:

  • Using pre-defined animations: Android provides several built-in animation resources like fade in, fade out, rotate, and scale. You can load these animations from XML files and apply them to your Views.
  • Creating custom animations: For more complex animations, you can use the Animation class or the newer Animator class to define your own animation properties and timing.

View animation, in the context of Android development, refers to the process of adding smooth transitions and movements to the elements on your app’s screen. These elements, called Views, can be anything from buttons and images to layouts and text boxes. By animating their properties like size, position, transparency, and rotation, you can create a more visually appealing and engaging experience for your users.

Important Links for Java and Android

Java and Android Programming Tutorials

Java Hibernate Tutorials

Android Kotlin Checkbox example

MainActivity.kt

package com.example.checkboxdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
// author : www.raviroza.com
// date : 2-Jan-2023, 9.00 am
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity()
{
    var ch = mutableSetOf("")
    fun setChoices()
    {
        var t = ""
        for(tt in ch)
        {
            t = t + tt + " "
        }
        lblChoiceList.text = t.toString()
    }

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

        /*chkCricket.setOnClickListener()
        {
            if(chkCricket.isChecked == true)
            {
                ch.add(chkCricket.text.toString())
            }
            else
            {
                ch.remove(chkCricket.text.toString())
            }
            lblChoiceList.text = ch.toString()
        }
        chkFootBall.setOnClickListener()
        {
            if(chkFootBall.isChecked == true)
            {
                ch.add(chkFootBall.text.toString())
            }
            else
            {
                ch.remove(chkFootBall.text.toString())
            }
            lblChoiceList.text = ch.toString()
        }
        chkKabbadi.setOnClickListener()
        {
            if(chkKabbadi.isChecked == true)
            {
                ch.add(chkKabbadi.text.toString())
            }
            else
            {
                ch.remove(chkKabbadi.text.toString())
            }
            lblChoiceList.text = ch.toString()
        }

        chkFootBall.setOnClickListener()
        {
            lblChoiceList.text = lblChoiceList.text.toString() + chkFootBall.text.toString()
        }
        chkKabbadi.setOnClickListener()
        {
            lblChoiceList.text = lblChoiceList.text.toString() + chkKabbadi.text.toString()
        }*/


       chkCricket.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked)
            {
                ch.add(buttonView.text.toString())
                setChoices()
            }
            else
            {
                ch.remove(buttonView.text.toString())
                setChoices()
            }
        }
        chkFootBall.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked)
            {
                ch.add(buttonView.text.toString())
                setChoices()
            }
            else
            {
                ch.remove(buttonView.text.toString())
                setChoices()
            }
        }
        chkKabbadi.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked)
            {
                ch.add(buttonView.text.toString())
                setChoices()
            }
            else
            {
                ch.remove(buttonView.text.toString())
                setChoices()
            }
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_margin="4dp"
android:background="@color/MainColor"
    tools:context=".MainActivity" >

    <LinearLayout
        android:background="@color/SubColor"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/lblHead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="#2196F3"
            android:gravity="center"
            android:text="Check Box Demo"
            android:textColor="#232031"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/lblSubHead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:gravity="center"
            android:text="Select Your Game"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/chkCricket"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:text="@string/cricket"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/chkKabbadi"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:text="@string/Kabbadi"
            android:textSize="18sp" />

        <CheckBox
            android:id="@+id/chkFootBall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:text="@string/Football"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/lblHereAreYourChoices"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:gravity="center"
            android:background="#4876a8"
            android:text="Your Choices are "
            android:textSize="18sp" />

        <TextView
            android:id="@+id/lblChoiceList"
            android:layout_width="match_parent"
            android:layout_height="301dp"
            android:layout_margin="4dp"
            android:gravity="center_horizontal"
            android:text=".... "
            android:textSize="18sp" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Android Kotlin SQLite Demo

Here is the example of Android with Kotlin SQLite database in Android Studio.

MainActivity.kt

package com.example.sqlitedemo

import android.content.Context

// Author : www.raviroza.com
// Date : 26-Dec-2022, 10.00 am

import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    lateinit var DB : SQLiteDatabase
    lateinit var QRY : String
    lateinit var CURSOR : Cursor

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

        DB = openOrCreateDatabase("MyDB", Context.MODE_PRIVATE,null)

        Toast.makeText(this,DB.path.toString(),Toast.LENGTH_LONG).show()

        QRY = "CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);"

        DB.execSQL(QRY)

        Toast.makeText(this,"Table Created !",Toast.LENGTH_LONG).show()
        loadData()

        btnAdd.setOnClickListener()
        {
            QRY = "insert into student values ( ${txtRollNo.text} , '${txtName.text}' , ${txtMarks.text} )"
            DB.execSQL(QRY)
            Toast.makeText(this,"Record Inserted !",Toast.LENGTH_LONG).show()
            txtName.text = null
            txtMarks.text = null
            txtRollNo.text = null
            loadData()

        }

    }
    fun loadData()
    {
        QRY = "Select * from Student "
        CURSOR = DB.rawQuery(QRY,null)
        var data : String = ""
        while (CURSOR.moveToNext())
        {
            data += CURSOR.getString(0) + "\n"
            data += CURSOR.getString(1) + "\n"
            data += "-------------------------------\n"
        }
        if(data.length==0) {

            lblResult.text = "No Records Found"
        }
        else
        {
            lblResult.text = data
        }
    }
}

activity_main.xml

<?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"
    android:layout_margin="16dp"
    tools:context=".MainActivity">

<!--    Author : www.raviroza.com-->
<!--    Date : 26-Dec-2022, 10.00 am-->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="DB App Demo - Student"
        android:textSize="25dp"
        android:gravity="center"
        android:background="@color/colorAccent"
        android:padding="8dp"
         />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter Roll No. :"
        android:textSize="20dp"
        />

    <EditText
        android:id="@+id/txtRollNo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="0"
        android:inputType="number"
        android:textSize="20dp"

        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter Name :"
        android:textSize="20dp"
        />

    <EditText
        android:id="@+id/txtName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter Marks:"
        android:textSize="20dp"
        />

    <EditText
        android:id="@+id/txtMarks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:inputType="number"
        />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="Add Record"
        android:textAllCaps="false"
        />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <TextView
        android:id="@+id/lblResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:text="student data"
        android:textSize="18dp"
        />

    </ScrollView>

</LinearLayout>
%d bloggers like this: