How Do I Limit The Number of Characters Entered in an Alert Dialog EditText

android, android-alertdialog, android-edittext

Solution

try something like this:

InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(MAX_LENGTH);
input.setFilters(FilterArray);

Problem

I know how to limit the size through XML (android:maxLength), but I am dynamically creating an alert dialog in code. Is there something similar I can use? (Prefer to have API 10 compatible solution as well) I am using the alert dialog to prompt the user for a text value to be shown 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()); } }); ```

Original source