[:en]Android: alertDialog with buttons[:]

[:en]alert

// Get from string resources

dialog.setMessage(getApplicationContext().getString(R.string.pleasewaitwarning));

Step 1: activity_main.xml

 


    
 

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showAlert();
    }


    public void showAlert(){

        new AlertDialog.Builder(this)
                .setTitle("Delete entry")
                  // Get from string resources
                 .setMessage(R.string.addmorewarning)
                //.setMessage("Are you sure you want to delete this entry?")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "You have pressed Yes", Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "You have pressed Cancel", Toast.LENGTH_LONG).show();
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();

    }



    public void showAlertDialog(View v){

        AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
        builder1.setMessage("Write your message here.");
        builder1.setCancelable(true);

        builder1.setPositiveButton(
                "Yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        Toast.makeText(getApplicationContext(), "You have pressed Yes", Toast.LENGTH_LONG).show();
                    }
                });

        builder1.setNegativeButton(
                "No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                        Toast.makeText(getApplicationContext(), "You have pressed No", Toast.LENGTH_LONG).show();
                    }
                });

        AlertDialog alert11 = builder1.create();
        alert11.show();

    }

}


Extra style



    
    

    

    



[:]

Print Friendly, PDF & Email
Scroll to Top