Skip to main content

How to set an ATM Input EditText

This tutorial will show you how to form an ATM Input EditText



Let's see how to do this

Create your xml with a EditText
<EditText
    android:id="@+id/id_edttext_amt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="$0.00"
    android:gravity="center"
    android:inputType="number"/>

Now in you activity,

Implement TextWatcher for the EditText you need ATM type input

private String current = "";
edtAtm.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after{
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(!s.toString().equals(current)){
            edtAtm.removeTextChangedListener(this);

            String cleanString = s.toString().replaceAll("[$,.]", "");

            double parsed = Double.parseDouble(cleanString);
            String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));

            current = formatted;
            edtAtm.setText(formatted);
            edtAtm.setSelection(formatted.length());

            edtAtm.addTextChangedListener(this);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});

To show different currency in EditText - do a trick

Create a xml like
<LinearLayout
    android:id="@+id/id_fee_lay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edittext_style"
    android:visibility="gone"
    android:layout_marginBottom="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:text="₱"/>

    <EditText
        android:id="@+id/id_cancel_fee"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:singleLine="true"
        android:gravity="top|left"
        android:inputType="numberDecimal"
        android:background="@android:color/transparent"
        android:hint="0.00"/>

</LinearLayout>

Fix the currency symbol in TextView and do the remaining on EditText TextWatcher
Few changes in activity
private String current = "";
if(s.toString().length()>0) {
    if (!s.toString().equals(current)) {
        edtCanFee.removeTextChangedListener(this);

        String cleanString = s.toString().replaceAll("[ ,.]", "");

        double parsed = Double.parseDouble(cleanString);
        String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));

        current = formatted;

        StringTokenizer tokens = new StringTokenizer(formatted, "$");
        String first = tokens.nextToken();

        edtCanFee.setText(first);
        edtCanFee.setSelection(first.length());

        edtCanFee.addTextChangedListener(this);
    }
}

The output will be like,

Another way to achieve Currency EditText

Using third-party library currency_edittext:
compile 'com.github.faranjit:currency-edittext:1.0.1'

How to use:
These lines formats simply your input for default locale

<faranjit.currency.edittext.CurrencyEditText
        android:id="@+id/edt_currency"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:textColor="@android:color/black" />

For more
________________________________________________________________________________________
Happy Coding...



Comments

Popular posts from this blog

Bluetooth Chat Application - Android Tutorial

In this tutorial, we will see about how to design an Android layout for chat application using Chat Bubbles  and the main part is chat via Bluetooth . Main objective of this post is to give an idea about how to allow two-way text chat over Bluetooth in android. Bubbles: Chat bubbles are background image that expands horizontally and vertically as required based on the message posted. Bubbles are Nine-patch Images. Image Nine-patch Image In creating Android chat bubbles, nine-patch image plays a crucial role.  Nine-patch image  is a bitmap which stretches to fit the content posted in the View where it is applied as a background. A NinePatch drawable is a standard PNG image that includes an extra 1-pixel-wide border. It must be saved with the extension  .9.png , and saved into the  res/drawable/  directory of your project. The border is used to define the stretchable and static areas of the image. You indicate a stretchable section ...

Zoom Image - Android Tutorial

Here we are going to see how to zoom an image in Imageview Will see it through a sample 1. Create xml with an ImageView <? 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" >     < LinearLayout         android:layout_width="200dp"         android:layout_height="200dp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"    ...

Barcode or QR scanner - Android Tutorial

This tutorial explains how to scan Bar-code or QR-code . We have many third party libraries and here we can see one among them zxing-android-embedded library How to use this Step 1: Add the dependency   implementation 'com.journeyapps:zxing-android-embedded:3.5.0' Step 2 : Initialize it in your activity IntentIntegrator qbScan = new IntentIntegrator(this);  Step 3: Call on button call or on any trigger qbScan.initiateScan(); For Portrait mode, in Manifest file   <activity     android:name="com.journeyapps.barcodescanner.CaptureActivity"     android:screenOrientation="portrait"     tools:replace="screenOrientation" /> _____________________________________________________________________________ Happy Coding...