This is a currency edittext example. We see how to use such a component in our app. This is especially useful when you are creating a finance-related application such a crypto app.

1. CurrencyEditText

CurrencyEditText is an Android EditText with automatic currency formatting.

Here are the demo screenshots:


Download the sample apk here.

Step 1: Installation

Add this to your root build.gradle file:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Now add the dependency to your app build.gradle file:

implementation 'com.github.marcoscgdev:CurrencyEditText:1.0.1'

Step 2: Using the widget

<com.marcoscg.currencyedittext.CurrencyEditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Step 3: Write Code

Retrieving the value:

val value = currencyEditText.getNumericValue()

Using a custom locale

currencyEditText.setLocale(Locale.UK)

Setting max number length

currencyEditText.setMaxLength(6)

Full Example

Below is a full example:

1. Layouts

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"
    tools:context=".MainActivity">

    <com.marcoscg.currencyedittext.CurrencyEditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp"
        android:textSize="32sp"
        android:gravity="center_horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp"
        android:text="Get numeric value"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

2. Code

MainActivity.kt

package com.marcoscg.currencyedittextsample

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.marcoscg.currencyedittextsample.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.editText.setMaxLength(6)

        binding.button.setOnClickListener {
            Toast.makeText(this, "Value: ${binding.editText.getNumericValue()}", Toast.LENGTH_LONG).show()
        }
    }
}

Reference

Download code here.
Read more here.

[Read Individually.](https://android.camposha.info/en/example/currencyedittext/)