Posts

Showing posts from May, 2013

Set limit for fraction in decimal numbers in EditText

Image
Already we know that we can set which type of input the edittext should accept from user using
android:inputType="numberDecimal"

But there is no predefined function to set the limit for the edittext to How many digit it should accept after the decimal point from user. We can achieve this by using TextWatcher.


Full code example.

Following program creates a Decimal Filter.

DecimalFilter.java
import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.EditText; public class DecimalFilter implements TextWatcher { int count= -1 ; EditText et; Activity activity; public DecimalFilter(EditText edittext, Activity activity) { et = edittext; this.activity = activity; } public void afterTextChanged(Editable s) { if (s.length() > 0) { String str = et.getText().toString(); et.setOnKeyListener(new OnKeyListener() { pub…