1

I have an App in which information from a restaurant order ie 3 x chips, 2 x burger, 4 x cans, Total is: £13-40 is displayed in a Toast. All very well, but I would prefer to have the user shown this information in a Dialog Box, with, say, Accept and Decline buttons. How do I go about this? Obviously, the xml part is straightforward, but how do I add the code in MainActivity - at present, I have a Submit Order button, which then pops up the toast with the order. This is my Toast lines of code..

DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
          result.append("\nTotal: £"+decimalFormat.format(totalamount)); //totalamount);  
          //Displaying the message on the toast  
          Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();  
user1641906
  • 117
  • 1
  • 2
  • 14

2 Answers2

0

Use an AlertDialog like below and the code below in your submit order button click

 DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
      result.append("\nTotal: £"+decimalFormat.format(totalamount));
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
  alertDialogBuilder.setMessage(result.toString());
  alertDialogBuilder.setPositiveButton("Accept", 
  new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface arg0, int arg1) {
        //do what you want to do if user clicks ok

     }
  });
  alertDialogBuilder.setNegativeButton("Decline", 
  new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
        //do what you want to do if user clicks cancel.
     }
  });

  AlertDialog alertDialog = alertDialogBuilder.create();
  alertDialog.show();
dora
  • 2,047
  • 3
  • 18
  • 20
0

You need to use an AlertDialog. For example:

DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
      result.append("\nTotal: £"+decimalFormat.format(totalamount));

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Hello");
    builder.setMessage(result.toString());
    builder.setPositiveButton(android.R.string.ok, 
    new DialogInterface.OnClickListener() {

          @Override
           public void onClick(DialogInterface arg0, int arg1) {
          //User accepted

    });
    builder.setNegativeButton(android.R.string.cancel, 
    new DialogInterface.OnClickListener() {

          @Override
           public void onClick(DialogInterface arg0, int arg1) {
          //User didn't accept

    });
    AlertDialog dialog = builder.create();
    dialog.show();
Bidhan
  • 10,607
  • 3
  • 39
  • 50