Wednesday, 29 May 2013

Set limit for fraction in decimal numbers in EditText

            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() {

public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
count--;
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(100);//Re sets the maxLength of edittext to 100.
et.setFilters(fArray);      
}
if (count > 2) {
   Toast.makeText(activity, "Sorry! You cant enter more than two digits after decimal point!", Toast.LENGTH_SHORT).show();
}
return false;
}
});

char t = str.charAt(s.length() - 1);

if (t == '.') {
count = 0;
}

if (count >= 0) {
if (count == 2) {
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(s.length());
et.setFilters(fArray); // sets edittext's maxLength to number of digits now entered.

}
count++;
}
}

}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}

}


How to set filter for ourEditText using this DecimalFilter. Sample code:

MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

EditText editText1, editText2;
Button test;
Activity activity;
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activity = this;
  
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
  
editText1.addTextChangedListener(new DecimalFilter(editText1, activity));
editText2.addTextChangedListener(new DecimalFilter(editText2, activity));
}

public void onCheck(View v) {
Toast.makeText(getApplication(), editText1.getText()+" and "+editText2.getText(), Toast.LENGTH_LONG).show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:inputType="numberDecimal" >

 <requestFocus />
</EditText>

<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:inputType="numberDecimal" />


<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:onClick="onCheck"
android:text="Button" />

</RelativeLayout>

screen shot of this code.



Thank You