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

Get Phone Number from Contact List - Android Tutorial

When you create an application to send sms or an application to make calls, getting a destination number from the contacts list is a common task. In this Android tip, I am going to show the code to fetch a number from the contacts list. Now let me tell you how to achieve the goal. First, you need to create an Intent object for the PICK_ACTION action. To open the contacts list, the table that contains the contacts information must be specified as a parameter of the constructor of the Intent class. You can refer to the table using ContactsContract.Contacts.CONTENT_URI. Then call the startActivityForResult () method passing the Intent object and request code to open the contacts list. After a contact is selected from the contacts list, to get the result, you need to override the onActivityResult(int reqCode, int resultCode, Intent data) method of the activity. You can call the getData() method of the data parameter to get the table or uri that contains the selected contact. From the t

Spinner with Search on DropDown - Android Tutorial

If you have more values on Dropdown of Spinner its hard to select the last item by making a long scroll. To overcome this issue Android introduced a component called  AutoCompleteTextView Yes it is!!! Then why Spinner with Search? There may be some requirement even though gave much knowledge about it. There is a simple and good library that helps us to achieve this -  SearchableSpinner Gradle dependencies {     ...     implementation 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1' } Usage Now replace your Normal Android Spinner on XML with the following < com.toptoche.searchablespinnerlibrary.SearchableSpinner     android:id="@+id/id_city"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@android:color/transparent"     android:padding="5dp" /> ___________________________________________________________

Set Focus on Spinner when select Item on Vertical Scroll - Android Tutorial

We may face an issue on Spinner lies on long vertical scroll, (i.e.) when selected and item from dropdown the focus moves to top of scroll. To avoid this please follow this piece of code spinner.setFocusableInTouchMode( true ); spinner.setOnFocusChangeListener( new View.OnFocusChangeListener() {     @Override     public void onFocusChange(View v, boolean hasFocus) {         if (hasFocus) {             if (spinner.getWindowToken() != null ) {                 spinner.performClick();             }         }     } });   _______________________________________________________________________________ Happy Coding...