Android:: Set max-length of EditText programmatically with other InputFilter
Just try this way
InputFilter
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; ++i)
{
if (!Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]*").matcher(String.valueOf(source.charAt(i))).matches())
{
return "";
}
}
return null;
}
};
How to apply
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edt =(EditText)findViewById(R.id.edt) ;
edt.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(10)});
}
How to programmatically set maxLength in Android TextView?
Should be something like that. but never used it for textview, only edittext :
TextView tv = new TextView(this);
int maxLength = 10;
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(maxLength);
tv.setFilters(fArray);
Android:: Set max-length of EditText programmatically with other InputFilter
Just try this way
InputFilter
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; ++i)
{
if (!Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890]*").matcher(String.valueOf(source.charAt(i))).matches())
{
return "";
}
}
return null;
}
};
How to apply
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edt =(EditText)findViewById(R.id.edt) ;
edt.setFilters(new InputFilter[]{filter,new InputFilter.LengthFilter(10)});
}
Set max length for EditText
public void setEditTextMaxLength(EditText editText, int length) {
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(length);
editText.setFilters(FilterArray);
}
EditText : set number of characters programmatically
You can Use InputFilter for restricting the number of characters in EditView programmatically as:
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(10);
your_edittext.setFilters(FilterArray);
for more help you can see this tutorial for restricting number of characters in EditView:
http://www.tutorialforandroid.com/2009/02/maxlength-in-edittext-using-codes.html
What's the best way to limit text length of EditText in Android
Documentation
Example
android:maxLength="10"
How to Get EditText maxLength setting in code
Only limited parameters have their getters, so I don't think you can read it .
So write length (Say 12) in values folder and use it in xml layout and arrayAdapter .
Now its not hard-coded .
1)Create integer.xml in values *
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="integer" name="max_length">12</item>
</resources>
2)In layout
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="@integer/max_length"
/>
3) in ArrayAdapter :
int maxLength = getResources().getInteger(R.integer.max_length);
Related Topics
API to Automatically Upload APK to Google Play
Finding the Dominant Color of an Image in an Android @Drawable
Android Multiple Screen Sizes with Same Density
Custom Global Application Class Breaks with "Android.App.Application Cannot Be Cast To"
Cordova Platform Add Android Not Working While Listing Android Targets
Live Character Count for Edittext
Loading All the Images from Gallery into the Application in Android
How to Programmatically Set Maxlength in Android Textview
Connectivity_Action Intent Received Twice When Wifi Connected
Android Task Affinity Explanation
Android Recyclerview: Change Layout File List to Grid Onoptionitemselected
Android Sharedpreferences in Fragment
Debugging with Android Studio Stuck at "Waiting for Debugger" Forever
Help Regarding Onclick() Event on an Item of Listview Custom Row Layout
Android - What Is the Meaning of Stableids
Android Datepickerdialog: Set Min and Max Date for Selection