I know how to limit the size via XML (android: maxLength), but I am dynamically creating a warning dialog in the code. Is there something similar that I can use? (Prefer also to have API 10 compatible solution)
I use the alert dialog to invite the user for a text value that will be displayed graphically later.
public void onClickPosition(View v) { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle(R.string.title_Position); alert.setMessage(R.string.message_Position); final EditText input = new EditText(this); input.setText(_currentClass.getPosition()); alert.setView(input); alert.setPositiveButton(R.string.option_Okay, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { _currentClass.setPosition(input.getText().toString()); TextView textView = (TextView)findViewById(R.id.textPosition); textView.setText(_currentClass.getPosition()); } });
try something like this:
InputFilter[] FilterArray = new InputFilter[1]; FilterArray[0] = new InputFilter.LengthFilter(MAX_LENGTH); input.setFilters(FilterArray);
You can set the equivalent maxLengthin code with InputFilter. Suppose you want to EditTextaccept a maximum of 20 characters:
maxLength
InputFilter
EditText
input.setFilters(new InputFilter[] { new InputFilter.LengthFilter(20) });
setFilters() , . , , , , .
setFilters()