Hi Guys, Maybe you all are expert in terms of using RecyclerView in android. This blog is simple example for using filter option with RecyclerView adapter. As for now you will instantiate RecyclerView and set the adapter to RecyclerView as following way. RecyclerView list = (RecyclerView) findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(this)); list.setHasFixedSize(true); ArrayList<Number> numbers = new ArrayList<>(); String ONEs[] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"}; String TENs[] = {"ZERO", "TEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY", "HUNDRED"}; String HUNDREDS[] = {"ZERO", "HUNDRED", "TWO HUND...
Already we know that we can set which type of input the edittext should accept from user using
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
How to set filter for ourEditText using this DecimalFilter. Sample code:
MainActivity.java
screen shot of this code.
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, this post was EXTREMELY helpful! I have added your class to my library. Thanks again.
ReplyDeleteyou are welcome friend :)
DeleteI have one question. I am trying to create an additional constructor for DecimalFilter similiar to this...
ReplyDeletepublic DecimalFilter(EditText edittext, String afterTypingMessage) {
et = edittext;
}
And then would like to be able to call a Toast when the user tries to type past the decimal limit. I can't seem to figure out how to do that.
Can you show me your code where you try to call Toast message?
DeleteWell that's the thing. I don't see anywhere in your class where it triggers a spot I can call it. From what I can tell, after it reaches the decimal size limit, the class doesn't get called again. I don't fully understand the underlying methods, which is why i can't determine how/where to code a toast message for my needs.
ReplyDeleteWhere in the class does the code execute when someone tries typing in after they have reached the limit?
See the updates friend. I have added toast message :)
DeleteThank you for the updates. That is actually what I thought was supposed to be done as well, but the problem is, that Toast message only executes when you enter the decimal limit and also when you backspace from the limit, NOT when you try to type past the limit. From what I can tell, once you reach the limit, any other keystrokes (except the delete key) are ignored. I have set breakpoints on the entire class. When you reach the limit, and try and type, nothing in the class gets invoked, hence why I can't figure out where to put the Toast message so it works properly :(
ReplyDeleteThe idea I am going for is, that if you are typing and the characters go through, then that is fine. But if you have reached the limit, and you are typing and nothing is showing up, I want the Toast message to advise you that you have reached the limit and that is why no more characters are being entered. That way the user doesn't think the app is just broken or something. Does that make sense?
What about this? This toast also invoked when you reach the limit only. isn't?
DeleteThe toast goes off once you have typed the final digit (and if you delete the final digit). I want it to only go off when trying to type past the limit. This way, if the limit is 2 decimal places, and the user is trying to type a third digit, they won't be confused, because they will keep getting a toast saying "You can't go past 2 digits". Right now, it is confusing to the user.
DeleteSee the update Brian Choan. By the way trying to toast from setOnKeyListener we will achieve it. From here the toast message will appear from you are enter the last digit.
Deletehow to achieve validation on input field if amount does not have decimal then only allow 5 digit amount but if user entered decimal then allow 8 digit in android using edittext
ReplyDelete