Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Toast from AlertDialog?

When the user clicks on the save button, an AlertDialog appears and asks the user to input text for the file name.

If the user clicks the positive button ("Ok") without specifying a name, I want to display a toast which asks them to do so, and keep the AlertDialog open. But the toast never displays and the dialog closes.

The code for the AlertDialog is here:

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle(R.string.save_game);
    alert.setMessage(R.string.request_name);

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    input.setHint(R.string.untitled);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText().toString();
      if(value != null){
          // Do something with value      
      }
      else{
          Toast.makeText(context, R.string.no_name_given, Toast.LENGTH_SHORT).show();
      }
    }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
    }
    });

    alert.show();

How can I make this happen?

Thanks!

like image 758
Rookatu Avatar asked Aug 02 '13 06:08

Rookatu


3 Answers

Public void showToast() {
    Toast.makeText(this, R.string.no_name_given, Toast.LENGTH_SHORT).show();
} 

Just call this method instead of displaying toast from an alert dialog box like this in your code.

else {
    showToast();
}

To keep it open use this method

public void forceOpen() {

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle(R.string.save_game);
    alert.setMessage(R.string.request_name);

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    input.setHint(R.string.untitled);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString();
            if(value != null) {
                // Do something with value      
            }
            else {
                Toast.makeText(context, R.string.no_name_given, Toast.LENGTH_SHORT).show();
            }
        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
        }
    });

    alert.show();

}

Just reopen it.. Not sure why it closes but this will work

like image 50
NightSkyCode Avatar answered Oct 06 '22 00:10

NightSkyCode


Change the code as follows:

if(value != null && value.length()>0){

// Do something with value      
 }else{
          Toast.makeText(context, R.string.no_name_given, Toast.LENGTH_SHORT).show();
}
like image 40
Suji Avatar answered Oct 05 '22 23:10

Suji


You can disable the Ok button.If the condition is validate,then enable button again.

like image 36
G-zone Avatar answered Oct 05 '22 23:10

G-zone