Chatbot in android studio with kotlin

Rahul Navgire
4 min readMar 1, 2021
chatbot ui

activity_main.xml file-
<?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=”wrap_content”
tools:context=”.ui.MainActivity”>

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:id=”@+id/mainlinearlayout”
android:orientation=”horizontal”
android:background=”#dcdcdc

android:weightSum=”6"
>

<EditText
android:id=”@+id/typemessege”
android:layout_width=”50dp”
android:layout_height=”wrap_content”
android:hint=”type a messege….”
android:inputType=”text”
android:background=”@drawable/round_shape”
android:backgroundTint=”#ffff”
android:layout_margin=”6dp”
android:singleLine=”true”
android:padding=”6dp”
android:textStyle=”normal”
android:layout_weight=”5.2"
android:textSize=”20sp”
android:drawableRight=”@drawable/ic_mic”
android:drawableTint=”#F4380D “
/>
<Button
android:id=”@+id/button”
android:layout_width=”60dp”
android:layout_height=”wrap_content”
android:drawableRight=”@drawable/ic_send”
android:drawableTint=” #26EA4B “

android:backgroundTint=”#dcdcdc”
android:layout_margin=”6dp”
android:padding=”2dp”
android:layout_marginEnd=”60dp”
android:background=”@drawable/shapeee”
/>

</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id=”@+id/recyclerview”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_above=”@id/mainlinearlayout”
android:padding=”5dp”
android:layout_below=”@id/vieww”
tools:listitem=”@layout/messege_item”
tools:itemCount=”20"
/>
<View
android:layout_width=”match_parent”
android:layout_height=”10dp”
android:id=”@+id/vieww”
android:background=”#5BE8BD”
>

</View>

</RelativeLayout>

messageitem.xml- this file contains the layout ,which is inflated in the message adapter file-

<?xml version=”1.0" encoding=”utf-8"?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent” android:layout_height=”wrap_content”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/sendmesege”
android:background=”@drawable/send_shape”
android:backgroundTint=”#86FD94"
android:text=”send messege here”
android:textSize=”20sp”
android:textStyle=”normal”
android:layout_alignParentEnd=”true”
android:layout_alignParentBottom=”true”
android:layout_margin=”4dp”
android:padding=”5dp”
/>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/recivemesege”
android:background=”@drawable/recieve_shape”
android:backgroundTint=”#e3e6e4 “
android:text=”recive messege here”
android:textSize=”20sp”
android:textStyle=”normal”
android:layout_alignParentStart=”true”
android:layout_alignParentBottom=”true”
android:layout_margin=”4dp”
android:padding=”5dp”
/>

</RelativeLayout>

Main activity.kt

package com.rahul.pixelschatbot.ui

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.speech.RecognizerIntent
import android.widget.Button
import android.widget.EditText
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.rahul.pixelschatbot.R
import com.rahul.pixelschatbot.data.messege
import com.rahul.pixelschatbot.utils.botresponces
import com.rahul.pixelschatbot.utils.constants.RECIEVE_ID
import com.rahul.pixelschatbot.utils.constants.SEND_ID
import com.rahul.pixelschatbot.utils.time
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import java.util.*

class MainActivity : AppCompatActivity() {

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

title=”PIXEL”

adptr= messgingadapter()
recyclerview.adapter= adptr
recyclerview.layoutManager=LinearLayoutManager(applicationContext)

typemessege.setOnLongClickListener {

val intent=Intent(this,speak::class.java)
startActivity(intent)
return@setOnLongClickListener true
}

customsg(“hey i am pixo , how can i help you”)

button.setOnClickListener {
sendmessege()

}

typemessege.setOnClickListener {
GlobalScope.launch {
delay(1000)
withContext(Dispatchers.Main){

recyclerview.scrollToPosition(adptr.itemCount- 1)
}
}
}

}

private fun sendmessege(){

val messege= typemessege.text.toString()
val timestamp=time.timestamp()

if (messege.isNotEmpty()){

typemessege.setText(“”)
adptr.insertmessegr(messege(messege,SEND_ID,timestamp))
recyclerview.scrollToPosition(adptr.itemCount-1)
botresponce(messege)

}

}

private fun botresponce(messs: String) {
val timestamp=time.timestamp()
GlobalScope.launch {
delay(1000)
withContext(Dispatchers.Main){
val responce=botresponces.responces(messs)
adptr.insertmessegr(messege(responce, RECIEVE_ID,timestamp))
recyclerview.scrollToPosition(adptr.itemCount- 1)
}
}

}

private fun customsg(messege: String) {

GlobalScope.launch {
delay(1000)
withContext(Dispatchers.Main){
val timestamp= time.timestamp()
adptr.insertmessegr(messege(messege,RECIEVE_ID,timestamp))
recyclerview.scrollToPosition(adptr.itemCount- 1)
}
}

}
}

messageningadapter.kt
package com.rahul.pixelschatbot.ui

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.rahul.pixelschatbot.R
import com.rahul.pixelschatbot.data.messege
import com.rahul.pixelschatbot.utils.constants.RECIEVE_ID
import com.rahul.pixelschatbot.utils.constants.SEND_ID
import kotlinx.android.synthetic.main.messege_item.view.*



class messgingadapter():RecyclerView.Adapter<messgingadapter.messegeviewholder>() {

var messegelist = mutableListOf<messege>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): messegeviewholder {

val infalter =
LayoutInflater.from(parent.context).inflate(R.layout.messege_item, parent, false)
return messegeviewholder(infalter)
}

override fun getItemCount(): Int {
return messegelist.size

}

override fun onBindViewHolder(holder: messegeviewholder, position: Int) {
val curreemtmessege = messegelist[position]
when(curreemtmessege.id){

SEND_ID ->{
holder.sendmessege.apply {
text=curreemtmessege.messegs
visibility=View.VISIBLE
}
holder.recievemesege.visibility=View.GONE
}
RECIEVE_ID -> {

holder.recievemesege.apply {
text=curreemtmessege.messegs
visibility=View.VISIBLE
}
holder.sendmessege.visibility=View.GONE
}
}
}

class messegeviewholder(view: View) : RecyclerView.ViewHolder(view) {

val sendmessege = view.findViewById<TextView>(R.id.sendmesege)
val recievemesege = view.findViewById<TextView>(R.id.recivemesege)


}

fun insertmessegr(messege: messege){

messegelist.add(messege)//so messege list getsdata from here
notifyItemInserted(messegelist.size)
}
}

dataclass.kt

package com.rahul.pixelschatbot.data

data class messege (val messegs:String,val id:String,val time:String){
}

now all the objects/ utils used in the code

  1. botresponces — this object have mathods which will return the responces

package com.rahul.pixelschatbot.utils

import com.rahul.pixelschatbot.utils.constants.CALL
import com.rahul.pixelschatbot.utils.constants.OPEN_SEARCH
import com.rahul.pixelschatbot.utils.constants.PLAY

object botresponces {

fun responces(mess:String):String{

val random=(0..2).random()
val argumess=mess.toLowerCase()
return when{

argumess.contains(“call”)->{
CALL

}

argumess.contains(“search”)->{
OPEN_SEARCH

}

argumess.contains(“play”)->{
PLAY

}
argumess.contains(“hello” ) -> {

when(random){
0-> “hey rahul “
1 ->”hello rahul “
2->”hello rahul”
else ->”error”
}
}

argumess.contains(“good morning” ) -> {

when(random){
0-> “good morning “
1 ->” good morning “
2->”good morning “
else ->”error”
}
}

argumess.contains(“fine”) -> {

when(random){
0-> “goood “
1 ->”goood “
2->”goood”
else ->”error”
}
}

argumess.contains(“how are you”)->{
when(random){

0->”i am good , how about you”
1->”very good , what about you”
2->”very good , what about you”

else -> “error”
}
}

argumess.contains(“who are you”) -> {

when(random){
0-> “my name is pixel i am a chatbot, what can i do for you? “
1-> “my name is pixel i am a chatbot, what can i do for you? “
2-> “my name is pixel i am a chatbot, what can i do for you?”

else ->”error”
}
}
argumess.contains(“my name is”) -> {

when(random){
0-> “ nice to meet you”
1-> “ nice to meet you”
2-> “ nice to meet you”

else ->”error”
}
}
argumess.contains(“tell me about yourself”) -> {

when(random){
0-> “ i am a static chatbot, and my name is pixel , what can i do for you?”
1-> “ i am a static chatbot, and my name is pixel , what can i do for you?”
2-> “ i am a static chatbot, and my name is pixel , what can i do for you?”

else ->”error”
}
}

argumess.contains(“sing for me”) -> {

when(random){
0-> “sorry i cant sing, but , which song do you like to play”
1-> “sorry i cant sing, but ,which song do you like to play”
2-> “sorry i cant sing ,but , which song do you like to play”

else ->”error”
}
}
argumess.contains(“time”) ->{
time.timestamp()//
}
else ->{
when(random){

0->”i didn’t get that”
1->”i dont know”
2->”excuse me”
else -> “error”
}
}
}
}
}

2)constants- this object contains all the constants which are used as keywords further

package com.rahul.pixelschatbot.utils

object constants {

const val SEND_ID=”SEND_ID”
const val RECIEVE_ID=”RECIEVE_ID”
const val OPEN_GOOGLE=”OPEN”
const val OPEN_SEARCH=”SEARCHING…….”
const val CALL=”callling”
const val PLAY=”playing….”

}

3)time- this object gives the current time in standard date format

package com.rahul.pixelschatbot.utils

import android.app.Service
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.*

object time {


fun timestamp():String{

val times= Timestamp(System.currentTimeMillis())
val sdf=SimpleDateFormat(“HH:mm”)
val finnaltime=sdf.format(Date(times.time))
return finnaltime.toString()
}
}

--

--