AlertDialog with single button example : Android


Posted on : Mon, 20th Apr 15 10:41 am UTC
100+ votes


Figure : Android AlertDialog with Single Button

To create an Android AlertDialog with just a single button we can make use of any one method that is provide i.e. one of setPositiveButton(), setNegativeButton() or setNeutralButton() .



Advertisements





Lets create an example where in we have some Information that has to be displayed to the user in a form of a Dialog, it should have a Dismiss button, when clicked the message must hide.

AlertDialog Snippet Language : Java
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setTitle("Information!");

        builder.setMessage("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n" +
                "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\n" +
                "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\n" +
                "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\n" +
                "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non\n" +
                "proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");

        builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"Get Started!",Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        });

        AlertDialog diag = builder.create();

        //Display the message!
        diag.show();

Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

Follow us : Facebook - Twitter - Google +