1

How do I set the max of how many characters can be entered. So now the user can enter a lot in the alertdialog While it should have the max. it's not like XML. That would have been easier, any tips for this alert dialog?

    private void RequestNewGroup()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialog);
    builder.setTitle("Enter Group Name ");

    final EditText groupNameField = new EditText(MainActivity.this);
    groupNameField.setHint("");
    builder.setView(groupNameField);

    builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i)
        {
            String groupName = groupNameField.getText().toString();

            if (TextUtils.isEmpty(groupName))
            {
                Toast.makeText(MainActivity.this, "Please write Group Name...", Toast.LENGTH_SHORT).show();
            }
            else
            {
                CreateNewGroup(groupName);
            }
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i)
        {
            dialogInterface.cancel();
        }
    });

    builder.show();
}
DecPK
  • 24,537
  • 6
  • 26
  • 42

1 Answers1

1

You need to add a InputFilter to your EditText

final EditText groupNameField = new EditText(MainActivity.this);
groupNameField.setHint("");

// ### -> max length
groupNameField.setFilters(new InputFilter[]{ new InputFilter.LengthFilter(###) });
javdromero
  • 1,850
  • 2
  • 11
  • 19