Tuesday, 21 September 2021

CRUD Operations with MySQL in Android Studio using Kotlin and Retrofit

  •  What is Android?
  • Android is a mobile operating system based on a modified version of the Linux kernel and other open source software, designed primarily for touchscreen mobile devices such as smartphones and tablets. Now a day Android Operating is used in Smart Watch, Smart TV and others.

  • What is Android Studio?
  • Android Studio is the official integrated development environment for Google's Android operating system, built on JetBrains' IntelliJ IDEA software and designed specifically for Android development.
 
  • What is Kotlin?
  • Kotlin is an open-source, cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java, and the JVM version of Kotlin's standard library depends on the Java Class Library, but type inference allows its syntax to be more concise. We can also use Java, Python, C++ to develop Android Apps.

  •  What is Retrofit?
  • Retrofit is a type-safe REST client for Android, Java and Kotlin developed by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp. ... This library makes downloading JSON or XML data from a web API fairly straightforward.
 
  • What is REST Client?
  • REST Client is a method or a tool to invoke a REST service API that is exposed for communication by any system or service provider.
 
  • What is REST?
  • Representational state transfer is a software architectural style that was created to guide the design and development of the architecture for the World Wide Web. REST defines a set of constraints for how the architecture of an Internet-scale distributed hypermedia system, such as the Web, should behave. 

  • What is  API?
  • An API (Application Programming Interface) is a set of programming instructions and standards for accessing a web tool or database. A software company releases its API to the public so other software developers can design products that are powered by its service. The API is usually packaged in an SDK.
     
     
    What is Android SDK?
    The Android SDK (Software Development Kit) is a set of development tools that are used to develop applications for the Android platform. This SDK provides a selection of tools that are required to build Android applications and ensures the process goes as smoothly as possible.


    What is MySQL?
    MySQL is an open-source relational database management system (RDBMS). Its name is a combination of "My", the name of co-founder Michael Widenius's daughter, and "SQL", the abbreviation for Structured Query Language.


    What is RDBMS?
    The software used to store, manage, query, and retrieve data stored in a relational database, that is based on the relational model is called a relational database management system (RDBMS) . The RDBMS provides an interface between users and applications and the database, as well as administrative functions for managing data storage, access, and performance.
     
     
     -------------------------------
     Complete Coding:
    --------------------------------
     
    Create a database on MySQL with name 'visitor' and goto SQL menu and paste following code then press go.
     
    -- phpMyAdmin SQL Dump
    -- version 4.5.1
    -- http://www.phpmyadmin.net
    --
    -- Host: 127.0.0.1
    -- Generation Time: Dec 08, 2020 at 02:37 PM
    -- Server version: 10.1.8-MariaDB
    -- PHP Version: 5.6.14

    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";


    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8mb4 */;

    --
    -- Database: `visitor`
    --

    -- --------------------------------------------------------

    --
    -- Table structure for table `db_visitor`
    --

    CREATE TABLE `visitor` (
      `id` int(11) NOT NULL,
      `name_visitor` varchar(30) NOT NULL,
      `gender` enum('M','F') NOT NULL,
      `address` varchar(30) NOT NULL
    ) ENGINE=InnoDB;

    --
    -- Dumping data for table `visitor`
    --

    INSERT INTO `visitor` (`id`, `name_visitor`, `gender`, `address`) VALUES
    (1, 'Erwin', 'M', 'Tamora'),
    (3, 'Faisal', 'M', 'Pakam'),
    (4, 'Jeremia', 'M', 'Ringroad'),
    (6, 'Riza Ramadhani', 'F', 'Aras Kabu'),
    (7, 'Fitri Lestari', 'F', 'Tamora'),
    (8, 'Anggi Tryana', 'F', 'Tamora'),
    (9, 'Aditya Syahputra', 'M', 'Tamora');

    --
    -- Indexes for dumped tables
    --

    --
    -- Indexes for table `visitor`
    --
    ALTER TABLE `visitor`
      ADD PRIMARY KEY (`id`);

    --
    -- AUTO_INCREMENT for dumped tables
    --

    --
    -- AUTO_INCREMENT for table `visitor`
    --
    ALTER TABLE `visitor`
      MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


     Android Studio Project structure:
     Just add the following codes in the respective files. 
     
     
     
    Gradle Script->
    ------------------------------

    build.gradle(App):
    ------------------------------
    //retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
     
    build.gradle(App) 
     
    app->manifests->
    --------------------- 
     AndroidManifest.xml
    ---------------------
    <uses-permission android:name="android.permission.INTERNET" /> 
    AndroidManifest.xml 
    app->res-values-> 
    --------------------- 
    strings.xml
    ---------------------
    <resources>
    <string name="app_name">Mentoring week 4</string>
    <string name="name_visitor">Visitor Name</string>
    <string name="address">Address</string>
    <string name="gender">Gender</string>
    <string name="male">M</string>
    <string name="female">F</string>
    <string name="submit">Submit</string>
    <string name="todo">TODO</string>
    <string name="update">Update</string>
    <string name="onfailure_input">\"onFailure input: \"</string>
    <string name="crud_api_data_visitor">VISITOR DATA CRUD API APP</string>
    </resources>
     
    app->res->layout-> 
    activity_main.xml
    ---------------------
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    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"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_visitor"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:listitem="@layout/item_visitor"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab_add"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    android:contentDescription="@string/todo"
    android:src="@drawable/ic_add"
    tools:ignore="ImageContrastCheck,DuplicateSpeakableTextCheck" />

    </RelativeLayout>
     Output:
     
    -----------------------
    activity_add.xml
    -----------------------
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context=".AddActivity">

    <EditText
    android:id="@+id/edt_name_visitor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:autofillHints="Jhon"
    android:hint="@string/name_visitor"
    android:inputType=""
    android:minHeight="48dp"
    tools:ignore="TextContrastCheck" />

    <EditText
    android:id="@+id/edt_address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:autofillHints="North"
    android:hint="@string/address"
    android:inputType=""
    android:minHeight="48dp" />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/gender" />

    <RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp">

    <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/male"
    android:textStyle="bold" />

    <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/female"
    android:textStyle="bold" />

    </RadioGroup>


    <Button
    android:id="@+id/btn_submit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="@string/submit"
    android:textAllCaps="false"
    android:textStyle="bold" />

    </LinearLayout>
     Output:
     
    --------------------
    activity_splash.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"
    tools:context=".SplashActivity">

    <ImageView
    android:id="@+id/imageLogo"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_centerVertical="true"
    android:contentDescription="@string/todo"
    android:src="@drawable/crud_logo"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="ImageContrastCheck" />

    <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="100dp"
    android:text="@string/crud_api_data_visitor"
    android:textAlignment="center"
    android:textColor="@color/black"
    android:textSize="28sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
     Output:
     
    -----------------
    item_visitor.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"
    android:orientation="vertical"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="5dp"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
    app:cardCornerRadius="20dp"
    app:cardElevation="0dp"
    app:cardBackgroundColor="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
    android:padding="16dp"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
    android:id="@+id/txt_name_visitor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:text="Erwin Paisal"
    android:textSize="18sp"
    android:textStyle="bold"
    android:textColor="@color/black"/>

    <TextView
    android:id="@+id/txt_gender_visitor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    tools:text="Male" />

    <TextView
    android:id="@+id/txt_address_visitor"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:text="Kolkata" />

    </LinearLayout>

    <ImageView
    android:id="@+id/btn_delete"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:layout_marginEnd="20dp"
    android:contentDescription="@string/todo"
    android:src="@drawable/ic_delete"
    tools:ignore="ImageContrastCheck" />

    </RelativeLayout>

    </androidx.cardview.widget.CardView>

    </LinearLayout>
     Output:
     
    app->java->id.erwinpaisal.mentoringweek4->adapter->
    -----------------
    VisitorAdapter.kt
    -----------------
    package id.erwinpaisal.mentoringweek4.adapter

    import android.annotation.SuppressLint
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import android.widget.ImageView
    import android.widget.TextView
    import androidx.recyclerview.widget.RecyclerView
    import id.erwinpaisal.mentoringweek4.R
    import id.erwinpaisal.mentoringweek4.model.data.DataItem

    class VisitorAdapter(private val data: ArrayList<DataItem>?, private val itemClick: OnItemClickListener): RecyclerView.Adapter<VisitorAdapter.MyViewHolder>() {

    class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
    val txtName: TextView = itemView.findViewById<TextView>(R.id.txt_name_visitor)
    val txtGender = itemView.findViewById<TextView>(R.id.txt_gender_visitor)
    val txtAddress = itemView.findViewById<TextView>(R.id.txt_address_visitor)
    val btnDelete = itemView.findViewById<ImageView>(R.id.btn_delete)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.item_visitor, parent, false)
    return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
    return data?.size ?: 0
    }

    @SuppressLint("SetTextI18n")

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    val item = data?.get(position)
    holder.txtName.text = item?.nameVisitor
    holder.txtAddress.text = item?.address
    if (item?.gender == "M") {
    holder.txtGender.text = "Male"
    } else if (item?.gender == "F") {
    holder.txtGender.text = "Female"
    }

    holder.itemView.setOnClickListener {
    itemClick.detail(item)
    }
    holder.btnDelete.setOnClickListener {
    itemClick.delete(item)
    }
    }

    }

    interface OnItemClickListener {
    fun detail(item: DataItem?)
    fun delete(item: DataItem?)
    }
     
    app->java->id.erwinpaisal.mentoringweek4->model->action 
     ------------------
    ActionResponse.kt
    ------------------- 
    package id.erwinpaisal.mentoringweek4.model.action

    import com.google.gson.annotations.SerializedName

    data class ActionResponse(

    @field:SerializedName("isSucess")
    val isSucess: Boolean? = null,

    @field:SerializedName("message")
    val message: String? = null
    )
     
    app->java->id.erwinpaisal.mentoringweek4->model->data 
    -------------------
    VisitorResponse.kt
    ------------------- 
    package id.erwinpaisal.mentoringweek4.model.data

    import android.os.Parcelable
    import com.google.gson.annotations.SerializedName
    import kotlinx.android.parcel.Parcelize

    data class VisitorResponse(

    @field:SerializedName("data")
    val data: ArrayList<DataItem>? = null,

    @field:SerializedName("message")
    val message: String? = null,

    @field:SerializedName("isSuccess")
    val isSuccess: Boolean? = null
    )

    @Parcelize
    data class DataItem(

    @field:SerializedName("name_visitor")
    val nameVisitor: String? = null,

    @field:SerializedName("id")
    val id: String? = null,

    @field:SerializedName("gender")
    val gender: String? = null,

    @field:SerializedName("address")
    val address: String? = null

    ): Parcelable 
     
    app->java->id.erwinpaisal.mentoringweek4->networking 
    -----------------
    ConfigNetwork.kt
    ----------------- 
    package id.erwinpaisal.mentoringweek4.networking

    import retrofit2.Retrofit
    import retrofit2.converter.gson.GsonConverterFactory

    class ConfigNetwork {

    companion object {
    private fun getRetrofit() : Retrofit {
    return Retrofit.Builder()
    //.baseUrl("http://10.0.2.2/visitor/") //avd
    .baseUrl("http://192.168.0.110/visitor/") //ip device
    .addConverterFactory(GsonConverterFactory.create())
    .build()
    }

    fun service(): VisitorService {
    return getRetrofit().create(VisitorService::class.java)
    }
    }
    }
     
    app->java->id.erwinpaisal.mentoringweek4->networking 
    ------------------
    VisitorService.kt 
    ------------------
    package id.erwinpaisal.mentoringweek4.networking

    import id.erwinpaisal.mentoringweek4.model.action.ActionResponse
    import id.erwinpaisal.mentoringweek4.model.data.VisitorResponse
    import retrofit2.Call
    import retrofit2.http.*


    interface VisitorService{
    //get visitor data
    @GET("getDataVisitor.php")
    fun getDataVisitor(): Call<VisitorResponse>

    //get data by id
    @GET("getDataVisitor.php")
    fun getVisitorById(
    @Query("id") id: Int
    ): Call<VisitorResponse>

    // insert insert data
    @FormUrlEncoded
    @POST("storeVisitor.php")
    fun storeVisitor(
    @Field("name_visitor") nameVisitor: String,
    @Field("gender") gender: String?,
    @Field("address") address: String

    ): Call<ActionResponse>

    // update visitor data
    @FormUrlEncoded
    @POST("updateVisitor.php")
    fun updateVisitor(
    @Field("id") id: String,
    @Field("name_visitor") nameVisitor: String,
    @Field("gender") gender: String?,
    @Field("address") address: String
    ): Call<ActionResponse>

    // delete visitor data
    @FormUrlEncoded
    @POST("deleteVisitor.php")
    fun deleteVisitor(
    @Field("id") id: String?
    ): Call<ActionResponse>
    } 
     
    app->java->id.erwinpaisal.mentoringweek4-> 
    -----------------
    MainActivity.kt
    -----------------
    package id.erwinpaisal.mentoringweek4

    import android.content.Intent
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.util.Log
    import android.widget.Toast
    import androidx.appcompat.app.AlertDialog
    import id.erwinpaisal.mentoringweek4.adapter.OnItemClickListener
    import id.erwinpaisal.mentoringweek4.adapter.VisitorAdapter
    import id.erwinpaisal.mentoringweek4.model.action.ActionResponse
    import id.erwinpaisal.mentoringweek4.model.data.DataItem
    import id.erwinpaisal.mentoringweek4.model.data.VisitorResponse
    import id.erwinpaisal.mentoringweek4.networking.ConfigNetwork
    import kotlinx.android.synthetic.main.activity_main.*
    import retrofit2.Call
    import retrofit2.Callback
    import retrofit2.Response

    class MainActivity : AppCompatActivity() {

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

    showData()

    fab_add.setOnClickListener{
    val intent = Intent(this@MainActivity, AddActivity::class.java)
    startActivity(intent)
    }
    }

    override fun onResume() {
    super.onResume()
    showData()
    }

    private fun showData() {
    val listVisitor = ConfigNetwork.service().getDataVisitor()
    listVisitor.enqueue(object : Callback<VisitorResponse>{
    override fun onFailure(call: Call<VisitorResponse>, t: Throwable) {
    Log.d(TAG, "onFailure list Visitor : " + t.message)
    }

    override fun onResponse(
    call: Call<VisitorResponse>,
    response: Response<VisitorResponse>
    ) {
    if (response.isSuccessful){
    val data = response.body()?.data
    val adapter = VisitorAdapter(data, object : OnItemClickListener{
    override fun detail(item: DataItem?) {
    val intent = Intent(this@MainActivity, AddActivity::class.java)
    intent.putExtra(EXTRA_DATA_VISITOR, item)
    startActivity(intent)
    }

    override fun delete(item: DataItem?) {
    AlertDialog.Builder(this@MainActivity).apply {
    setTitle("Delete data")
    setMessage("Sure want to delete ${item?.nameVisitor}")
    setPositiveButton("Delete") { dialog, which ->
    deleteData(item?.id)
    dialog.dismiss()
    }
    setNegativeButton("Cancel") { dialog, which ->
    dialog.dismiss()
    }
    }.show()
    }
    })
    rv_visitor.adapter = adapter
    }
    }

    })
    }

    private fun deleteData(id: String?) {
    val delete = ConfigNetwork.service().deleteVisitor(id)
    delete.enqueue(object : Callback<ActionResponse>{
    override fun onFailure(call: Call<ActionResponse>, t: Throwable) {
    Log.d(TAG, "onFailure deletion: " +t.message)
    }

    override fun onResponse(
    call: Call<ActionResponse>,
    response: Response<ActionResponse>
    ) {
    if (response.isSuccessful){
    val message = response.body()?.message
    Toast.makeText(this@MainActivity, message, Toast.LENGTH_SHORT).show()
    showData()
    }
    }

    })
    }

    companion object {
    const val TAG = "MainActivity"
    const val EXTRA_DATA_VISITOR = "extra_data_visitor"
    }
    }
     
    -----------------
    AddActivity.kt 
    ------------------ 
    package id.erwinpaisal.mentoringweek4

    import android.annotation.SuppressLint
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.util.Log
    import android.widget.RadioButton
    import android.widget.Toast
    import id.erwinpaisal.mentoringweek4.model.action.ActionResponse
    import id.erwinpaisal.mentoringweek4.model.data.DataItem
    import id.erwinpaisal.mentoringweek4.networking.ConfigNetwork
    import kotlinx.android.synthetic.main.activity_add.*
    import retrofit2.Call
    import retrofit2.Callback
    import retrofit2.Response

    class AddActivity : AppCompatActivity() {
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_add)

    var gender: String? = null
    val dataItem = intent.getParcelableExtra<DataItem>(MainActivity.EXTRA_DATA_VISITOR)

    if (dataItem != null){
    edt_name_visitor.setText(dataItem.nameVisitor)
    edt_address.setText(dataItem.address)

    if (dataItem.gender == "M")
    (radio_group.getChildAt(0) as RadioButton).isChecked = true
    else if (dataItem.gender == "F")
    (radio_group.getChildAt(1) as RadioButton).isChecked = true

    btn_submit.text = getString(R.string.update)
    }

    radio_group.setOnCheckedChangeListener { radioGroup, id ->
    val radio: RadioButton = findViewById(id)
    gender = radio.text.toString()
    }

    if (btn_submit.text == getString(R.string.submit)) {
    btn_submit.setOnClickListener {
    val name = edt_name_visitor.text.toString()
    val address = edt_address.text.toString()
    addVisitor(name, address, gender)
    }
    } else if (btn_submit.text == getString(R.string.update)) {
    btn_submit.setOnClickListener {
    val id = dataItem?.id ?: ""
    val name = edt_name_visitor.text.toString()
    val address = edt_address.text.toString()
    if (gender == null){
    gender = dataItem?.gender
    }
    updateVisitor(id, name, gender,address)
    }
    }
    }

    private fun addVisitor(nameVisitor: String, address: String, gender: String?) {
    val input = ConfigNetwork.service().storeVisitor(nameVisitor, gender, address)
    input.enqueue(object : Callback<ActionResponse> {
    override fun onFailure(call: Call<ActionResponse>, t: Throwable) {
    Log.d(TAG, getString(R.string.onfailure_input) +t.message)
    }

    override fun onResponse(
    call: Call<ActionResponse>,
    response: Response<ActionResponse>
    ) {
    if (response.isSuccessful){
    val message = response.body()?.message
    Toast.makeText(this@AddActivity, message, Toast.LENGTH_SHORT).show()
    finish()
    }
    }
    })
    }

    private fun updateVisitor(id: String, name:String, gender: String?, address:String){
    val input = ConfigNetwork.service().updateVisitor(id, name, gender,address)
    input.enqueue(object : Callback<ActionResponse> {
    override fun onFailure(call: Call<ActionResponse>, t: Throwable) {
    Log.d(TAG, "onFailure update: " +t.message)
    }

    override fun onResponse(

    call: Call<ActionResponse>,
    response: Response<ActionResponse>
    ) {
    if (response.isSuccessful){
    val message = response.body()?.message
    Toast.makeText(this@AddActivity, message, Toast.LENGTH_SHORT).show()
    finish()
    }
    }

    })
    }

    companion object {
    const val TAG = "AddActivity"
    }
    } 
     
    --------------------
    SplashActivity.kt
    ---------------------
    package id.erwinpaisal.mentoringweek4

    import android.content.Intent
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.os.Handler

    class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_splash)

    Handler().postDelayed({
    startActivity(Intent(this, MainActivity::class.java))
    finish()
    }, 4000)
    }
    } 
     Copy and paste the following image in the folder 
    app->res->drawable 
    Splash Page Image 
     Run the project on avd or android device.
     
     Click here to download the Complete Project
     
     
     
     
     
     
     


     
     
     
     
     
     

 

 

No comments:

Post a Comment