Firestore with Kotlin

Simple Chat Application with Firestore — Part 2

Panduka Wedisinghe
2 min readJan 3, 2019

This is the part 2 of Android Chat application with Firestore. Here I will be sharing Kotlin project and will be discussing the project with all of you.

In the first part, we discussed how to integrate Firestore in your Android project. No it is the high time to move forward and do some actual coding.

Login Activity

This is the launching activity of the chat application. It is very simple. The user has to enter username and user id to login.

login_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

<EditText
android:id="@+id/editTextUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="96dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextUserId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="User Id"
android:inputType="number"
app:layout_constraintEnd_toEndOf="@+id/editTextUserName"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/editTextUserName"
app:layout_constraintTop_toBottomOf="@+id/editTextUserName" />

<Button
android:id="@+id/buttonLogIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="156dp"
android:text="Log In"
app:layout_constraintEnd_toEndOf="@+id/editTextUserName"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="@+id/editTextUserName"
app:layout_constraintTop_toBottomOf="@+id/editTextUserId" />
</android.support.constraint.ConstraintLayout>

The reason behind having a Login activity is to distinguish users from their user IDs. For the sake of simplicity we will be having only 2 users initially. They are given IDs 1 and 2.

LogInActivity.kt

package com.example.pwe.firestoretestproject

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.pwe.firestoretestproject.Common.Companion.USER_ID
import com.example.pwe.firestoretestproject.Common.Companion.USER_NAME
import kotlinx.android.synthetic.main.login_activity.*

@SuppressLint("Registered")
class LogInActivity : AppCompatActivity() {

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

buttonLogIn.setOnClickListener {

var intent = Intent(this@LogInActivity, ChatActivity::class.java)
intent.putExtra(USER_ID, editTextUserId.text.toString())
intent.putExtra(USER_NAME, editTextUserName.text.toString())
startActivity(intent)
finish()
}
}
}

Constants.kt

class Constants {
companion object {

const val USER_NAME = "user_name"
const val USER_ID = "user_id"

}
}

This is the LogIn activity of the Chat application. It has two input fields where they take User Id and User name as inputs. As this is a demonstration, everything is made simple. Purpose is to illustrate how we are supposed to use Firestore.

When a user enters User Name and User Id and presses Button “Log In”, Chat Activity is launched. User Name and User Id are passed via the Intent. They will be referred in the Chat Activity.

In the next article, I will be covering the Entire Chat Activity and its implementation. I will see you in the next step sooner.

--

--

Panduka Wedisinghe

Android Developer /Software Engineer/Flutter Developer