Search code examples
javascriptjavaandroidandroid-studioandroid-alertdialog

Android studio Alert dialog block spaces for input in text


I find this more difficult than working on an XML file. How do I block spaces in the text input of an alert dialog? For example, how does it work because I didn't find much information about this on the internet. Thank you

    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("");
    groupNameField.setFilters(new InputFilter[]{new InputFilter.LengthFilter(25)});
    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();
}

Solution

  • I find this more difficult than working on an XML file.

    You could (and probably should) create custom dialogs that have their own view/XML file.

    To do this, extend DialogFragment:

    public class MyDialog extends DialogFragment {
    
    EditText editText;
    
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
      
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // get your custom layout
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.my_custom_layout, null);
        builder.setView(view).setCancelable(false);
    
        editText = view.findViewById(R.id.editText);
    
        // add your code here
    }
    }
    

    XML (R.layout.my_custom_layout):

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="foo"
         android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
            android:inputType="textFilter" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    and in MainActivity:

    public void openDialog(Context context) {
    MyDialog dialog = new MyDialog();
    dialog.show(getSupportFragmentManager(), "myDialog");
    }
    

    also check Android disable space only for Edittext to disable space from XML and

    https://www.youtube.com/watch?v=ARezg1D9Zd0&t=446s&ab_channel=CodinginFlow for a more in depth tutorial on custom dialogs and how to send and recieve info from them.