OOZOU
Get in Touch
Back to Blog

An Easy Way to Make Reactive Android Pin Code Feature with Secure Data

Here is an easy way to make a reactive Android pin code feature with secure data.

September 9, 2020
3 min read
An Easy Way to Make Reactive Android Pin Code Feature with Secure Data

Many years ago, making pin code with secure data was not easy without Data Binding. You might create an XML file along with the custom view class, but with Data Binding, you can make a pin code feature easier.

I will demonstrate how to use Data Binding by making a pin code feature that updates the UI reactively according to data changed.

**What's Android Data Binding?**The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. -- Data Binding LibraryI was thinking, “Why do we have to use Data Binding when we have Kotlin Syntactic where we can access UI components directly from fragment/activity?” Data Binding allows you to access ViewModel directly from the XML file reactively, and we can have a simple expression that doesn’t need any programmatic code inside fragment/activity.How to implement it.

Let's start with PinCodeViewModel.kt which contains our add and remove pin code logic.

package com.him.secured_pincode.ui.main

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class PinCodeViewModel : ViewModel() {

    val pinCode = MutableLiveData<String>().also {
        it.value = ""
    }

    val numPadListener = object : NumPadListener {
        override fun onNumberClicked(number: Char) {
            val existingPassCode = pinCode.value ?: ""
            val newPassCode = existingPassCode + number
            pinCode.postValue(newPassCode)
        }

        override fun onEraseClicked() {
            val droppedLast = pinCode.value?.dropLast(1) ?: ""
            pinCode.postValue(droppedLast)
        }
    }

}
<strong>language-</strong>kotlin

Create pin code dots and change the color reactively Create a layout_pin_code_dots.xml. The file contains the pin dots and changes each white dot to a purple dot when the user enters the pin code one by one by adding the simple expression into the dots like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="pinCode"
            type="androidx.lifecycle.MutableLiveData&lt;java.lang.String>" />
    </data>

....
        <View
            android:id="@+id/dot1"
            android:layout_width="@dimen/pincode_dot_size"
            android:layout_height="@dimen/pincode_dot_size"
            android:background="@drawable/pass_code_dot_selector"

<-- We will change the first dot to a purple dot when the user entered the first pin code -->
            android:enabled="@{pinCode.length() >= 1}" />

        <View
            android:id="@+id/dot2"
            android:layout_width="@dimen/pincode_dot_size"
            android:layout_height="@dimen/pincode_dot_size"
            android:layout_marginStart="16dp"
            android:background="@drawable/pass_code_dot_selector"
            android:enabled="@{pinCode.length() >= 2}" />
<strong>...</strong>

Create a num pad XML file and add a callback into each button where we can add pin code into the view model directly without having any extra code inside fragment/activity.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="numPadListener"
            type="com.him.secured_pincode.ui.main.NumPadListener" />

    </data>

...
        <Button
            style="@style/passCodeButton"
            android:onClick="@{(v) -> numPadListener.onNumberClicked('1')}"
            android:text="1" />

        <Button
            style="@style/passCodeButton"
            android:layout_marginStart="24dp"
            android:onClick="@{(v) -> numPadListener.onNumberClicked('2')}"
            android:text="2" />
...

That does it - this is how easy it is using Data Binding for making a pin code feature. Share if you have any other ideas - you can find the source code on Github In the next part, I will demonstrate an easy way to secure your pin code on Android.

Related Articles

How Can I Design a Software: A Beginners Guide for Business Owners

How Can I Design a Software: A Beginners Guide for Business Owners

Designing software is an exciting yet complex process that involves creativity, logical thinking, and problem-solving skills.

Read More →
The Future of Edge Computing: What Businesses Need to Know

The Future of Edge Computing: What Businesses Need to Know

The digital landscape is evolving rapidly, and businesses must adapt to keep up with rising demands for speed, efficiency, and real-time processing.

Read More →
The Future of Retail: AI, AR, and Digital Transformation Trends

The Future of Retail: AI, AR, and Digital Transformation Trends

Retail is evolving at an unprecedented pace, driven by AI (Artificial Intelligence), AR (Augmented Reality), and digital transformation technologies, revolutionizing the retail industry. Traditional brick-and-mortar stores are no longer just about selling products—they are becoming smart, interactiv

Read More →

Have a Project in Mind?

Let's discuss how we can help bring your ideas to life with our expertise in web and mobile development.

Start a Conversationhello@oozou.com
OOZOU Logo

Bangkok · Singapore · Hong Kong

X
Facebook
Instagram
LinkedIn

Services

  • Web Development
  • AI Agents & Generative AI
  • Mobile App Development
  • Data Analytics & Engineering
  • UI/UX & Product Design
  • Digital Transformation

Company

  • About Us
  • Careers
  • Blog
  • Case Studies
  • Today I Learned
  • Contact

More

  • Industries
  • Partner Network
© 2026 OOZOU. All rights reserved.
Privacy PolicyCode of ConductABAC Policy