How to Programmatically Set Maxlength in Android Textview

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



Leave a reply



Submit