1

We need this in order to prohibit the user from inputting null values as file names. The save button should be disabled unless userInput is not null.

Here is the current code:

public void openDialog() {
    @SuppressLint("InflateParams") View view = (LayoutInflater.from(AudioRecorder.this)).inflate(R.layout.audio_name_input, null);

    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(AudioRecorder.this);
    alertBuilder.setView(view);
    final EditText userInput = view.findViewById(R.id.userInput);

    alertBuilder.setCancelable(true);
    alertBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            inputName = String.valueOf(userInput.getText());
            Toast.makeText(AudioRecorder.this, "Next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
            filePathMaking();
        }
    });
    alertBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });

    Dialog dialog = alertBuilder.create();
    dialog.show();
}
Greg
  • 357
  • 1
  • 11

2 Answers2

1

You can do something like this:

if(input != null){
    button.setEnabled(true); //you can  click your button now
}else{
    button.setEnabled(false); //you can not click your button
}


Edit according to your comment:

Here is an example for generic custom dialog:

This will be your dialog class (or something similar, it's only an example):

public class FullSizeImageDialog extends Dialog {
private ImageView imageView;
private ProgressBar fullImageProgreesBar;
private Context dialogContext;

public FullSizeImageDialog(@NonNull Context context) {
    super(context);
    setContentView(R.layout.full_size_image_dialog);
    dialogContext = context;
    imageView = findViewById(R.id.full_size_image);
    fullImageProgreesBar = findViewById(R.id.fullImageProgreesBar);
    }
}

And this is your layout for the dialog (R.id.full_size_image in my case):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
   android:background="#66F9B639">


 <!--Place your views here-->


 </android.support.constraint.ConstraintLayout>

And when you want to show your dialog it's super easy:

FullSizeImageDialog dialog = new FullSizeImageDialog ();
dialog.show();

And now, you can put your logic inside of your custom dialog class.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • How do you disable .setPositiveButton("Save")? it is not an ordinary button. – Greg Jul 13 '19 at 09:05
  • I would make a custom dialog class for handling everything there, I will edit my answer – Tamir Abutbul Jul 13 '19 at 09:05
  • I have edited my answer, notice that for this simple task you can use `TextChangedListener` like in the above answers but I wanted to show you a cleaner solution for more complicated tasks. – Tamir Abutbul Jul 13 '19 at 09:10
1

Add a TextChangedListener to the edit text. Make the button enabled or disabled by the user input.

You can access the positive button as dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); and

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67