Created
November 5, 2014 14:44
-
-
Save alorma/9cd14d05386081fec479 to your computer and use it in GitHub Desktop.
Show a fixed number of decimals
This file contains hidden or 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 mt.bnpp.com.bnpp_mt_android_bleu.utils; | |
import android.text.InputFilter; | |
import android.text.Spanned; | |
/** | |
* Created by A591108 on 05/11/2014. | |
*/ | |
public class DecimalFilter implements InputFilter { | |
private int decimalDigits; | |
public DecimalFilter(int decimalDigits) { | |
this.decimalDigits = decimalDigits; | |
} | |
@Override | |
public CharSequence filter(CharSequence source, int i, int i2, Spanned spanned, int i3, int i4) { | |
int dotPos = -1; | |
int len = spanned.length(); | |
for (int decimalsI = 0; decimalsI < len; decimalsI++) { | |
char c = spanned.charAt(decimalsI); | |
if (c == '.' || c == ',') { | |
dotPos = decimalsI; | |
break; | |
} | |
} | |
if (dotPos >= 0) { | |
// protects against many dots | |
if (source.equals(".") || source.equals(",")) { | |
return ""; | |
} | |
// if the text is entered before the dot | |
if (i4 <= dotPos) { | |
return null; | |
} | |
if (len - dotPos > decimalDigits) { | |
return ""; | |
} | |
} | |
return null; | |
} | |
} |
This file contains hidden or 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 mt.bnpp.com.bnpp_mt_android_bleu.utils; | |
import android.text.InputFilter; | |
import android.text.Spanned; | |
/** | |
* Created by A591108 on 05/11/2014. | |
*/ | |
public class InputFilterMinMax implements InputFilter { | |
private double min, max; | |
public InputFilterMinMax(double min, double max) { | |
this.min = Math.min(min, max); | |
this.max = Math.max(min, max); | |
} | |
@Override | |
public CharSequence filter(CharSequence source, int i, int i2, Spanned spanned, int i3, int i4) { | |
try { | |
double input = Double.parseDouble(spanned.toString() + source.toString()); | |
if (isInRange(min, max, input)) { | |
return null; | |
} | |
} catch (NumberFormatException nfe) { | |
nfe.printStackTrace(); | |
} | |
return ""; | |
} | |
private boolean isInRange(double min, double max, double value) { | |
return value >= min && value <= max; | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="16dp"> | |
<mt.bnpp.com.bnpp_mt_android_bleu.ui.view.MoneyEditText | |
android:layout_width="match_parent" | |
android:layout_height="48dp" | |
android:inputType="numberDecimal" | |
android:ems="10" | |
app:min="1" | |
app:max="250" | |
app:decimals="2" /> | |
</LinearLayout> |
This file contains hidden or 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 mt.bnpp.com.bnpp_mt_android_bleu.ui.view; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.text.InputFilter; | |
import android.util.AttributeSet; | |
import android.widget.EditText; | |
import java.util.ArrayList; | |
import mt.bnpp.com.bnpp_mt_android_bleu.R; | |
import mt.bnpp.com.bnpp_mt_android_bleu.utils.DecimalFilter; | |
import mt.bnpp.com.bnpp_mt_android_bleu.utils.InputFilterMinMax; | |
/** | |
* Created by A591108 on 05/11/2014. | |
*/ | |
public class MoneyEditText extends EditText { | |
private int decimals = 2; | |
private float min = 0f; | |
private float max = 100f; | |
private InputFilter[] oldFilters; | |
public MoneyEditText(Context context) { | |
super(context); | |
init(null); | |
} | |
public MoneyEditText(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(attrs); | |
} | |
public MoneyEditText(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(attrs); | |
} | |
private void init(AttributeSet attrs) { | |
isInEditMode(); | |
if (attrs != null) { | |
TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.MoneyEditText, 0, 0); | |
decimals = a.getInt(R.styleable.MoneyEditText_decimals, decimals); | |
min = a.getFloat(R.styleable.MoneyEditText_min, min); | |
max = a.getFloat(R.styleable.MoneyEditText_max, max); | |
} | |
oldFilters = getFilters(); | |
addFilters(); | |
} | |
private void addFilters() { | |
ArrayList<InputFilter> inputFilters = new ArrayList<InputFilter>(oldFilters.length + 1); | |
for (InputFilter filter : oldFilters) { | |
inputFilters.add(filter); | |
} | |
inputFilters.add(new InputFilterMinMax(1d, 250d)); | |
inputFilters.add(new DecimalFilter(2)); | |
setFilters(inputFilters.toArray(new InputFilter[inputFilters.size()])); | |
} | |
public void setMin(float min){ | |
this.min = min; | |
addFilters(); | |
} | |
public void setMax(float max){ | |
this.max = max; | |
addFilters(); | |
} | |
public void setDecimals(int decimals){ | |
this.decimals = decimals; | |
addFilters(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nooo