Created
August 13, 2018 05:50
-
-
Save SouravKumarPandit/08ff5738b91d51cc0ce7a7b921f46c40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.application.limitless.sourav.dashletsproject.inputfilter; | |
import android.content.Context; | |
import android.support.v7.widget.AppCompatEditText; | |
import android.text.Editable; | |
import android.text.InputFilter; | |
import android.text.InputType; | |
import android.text.TextWatcher; | |
import java.math.BigDecimal; | |
import java.math.RoundingMode; | |
import java.text.DecimalFormat; | |
import java.text.DecimalFormatSymbols; | |
import java.text.ParseException; | |
import java.util.Locale; | |
public class CLCurrencyEditText extends AppCompatEditText { | |
private int MAX_LENGTH = 15; | |
private int MAX_DECIMAL = 3; | |
private int MAX_INTEGER = MAX_LENGTH - MAX_DECIMAL; | |
private int iCursorPosition = 0; | |
private double dMinValue = -1000;// Double.MIN_VALUE; | |
private double dMaxValue = 1000;// Double.MIN_VALUE; | |
private char groupDivider = ','; | |
protected String iPrefixVal = ""; | |
protected String iSufixVal = ""; | |
public CLCurrencyEditText(Context context) | |
{ | |
super(context); | |
this.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED); | |
this.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_LENGTH)}); | |
this.setEms(15); | |
this.addTextChangedListener(onTextChangeListener); | |
} | |
private TextWatcher onTextChangeListener = 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.length() == 0) | |
return; | |
removeTextChangedListener(this); | |
boolean isNegative = false; | |
String text = s.toString(); | |
if (text.charAt(0) == '-') | |
isNegative = true; | |
/* if (text.contains(".")) | |
{ | |
if (text.length() == 1) | |
{ | |
} | |
if (text.length() == 2 && text.charAt(0) == '-'){}else | |
{ | |
String[] spilt = text.split("."); | |
if (spilt.length == 2) | |
{ | |
spilt[0] = iPrefixVal; | |
spilt[1] = iSufixVal; | |
} else | |
text = spilt[0].replace(groupDivider, '\u0020').replace(" ", "").trim(); | |
} | |
} else*/ | |
/* | |
String[] spilt = text.split("."); | |
if (spilt.length == 2) | |
{ | |
spilt[0] = iPrefixVal; | |
spilt[1] = iSufixVal; | |
} | |
*/ | |
text = text.replace(groupDivider, '\u0020').replace(" ", "").replace("-", "").trim(); | |
try | |
{ | |
//save the number | |
text = format(text); | |
} catch (Exception e) | |
{ | |
} | |
if (isNegative) | |
setText('-'+text); | |
else | |
setText(text); | |
addTextChangedListener(this); | |
} | |
@Override | |
public void afterTextChanged(Editable s) | |
{ | |
handleSelection(); | |
// CLCurrencyEditText.this.setSelection(iCursorPosition); | |
} | |
}; | |
private void handleSelection() | |
{ | |
if (this.getText().length() <= MAX_LENGTH) | |
{ | |
this.setSelection(this.getText().length()); | |
} else | |
{ | |
this.setSelection(MAX_LENGTH); | |
} | |
} | |
public int getMaxDecimal() | |
{ | |
return MAX_DECIMAL; | |
} | |
public void setMaxDecimal(int MAX_DECIMAL) | |
{ | |
this.MAX_DECIMAL = MAX_DECIMAL; | |
} | |
private boolean isInRange(double min, double max, double value) | |
{ | |
return max > min ? value >= min && value <= max : value >= max && value <= min; | |
} | |
private String formatInteger(String str) | |
{ | |
BigDecimal parsed = new BigDecimal(str); | |
DecimalFormat formatter = new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.US)); | |
return formatter.format(parsed); | |
} | |
private String formatDecimal(String str) | |
{ | |
BigDecimal parsed = new BigDecimal(str); | |
// example pattern VND #,###.00 | |
DecimalFormat formatter = new DecimalFormat("#,###." + getDecimalPattern(str), new DecimalFormatSymbols(Locale.US)); | |
formatter.setRoundingMode(RoundingMode.DOWN); | |
return formatter.format(parsed); | |
} | |
private String getDecimalPattern(String str) | |
{ | |
int decimalCount = str.length() - str.indexOf(".") - 1; | |
StringBuilder decimalPattern = new StringBuilder(); | |
for (int i = 0; i < decimalCount && i < MAX_DECIMAL; i++) | |
{ | |
decimalPattern.append("0"); | |
} | |
return decimalPattern.toString(); | |
} | |
private String format(String text) throws ParseException | |
{ | |
if (text.contains(".")) | |
{ | |
return formatDecimal(text); | |
} else | |
{ | |
return formatInteger(text); | |
} | |
} | |
public int getMaxLength() | |
{ | |
return MAX_LENGTH; | |
} | |
public void setMaxLength(int MAX_LENGTH) | |
{ | |
this.MAX_LENGTH = MAX_LENGTH; | |
this.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_LENGTH)}); | |
} | |
public char getGroupDivider() | |
{ | |
return groupDivider; | |
} | |
public double getCurrencyDouble() throws ParseException | |
{ | |
String text = getText().toString(); | |
text = text.replace(groupDivider, '\u0020').replace(" ", "").trim(); | |
return Double.parseDouble(text) / Math.pow(10, MAX_DECIMAL); | |
} | |
public String getCurrencyText() throws ParseException | |
{ | |
return getText().toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment