undercoverindian
12/27/2017 - 12:44 AM

Dialogs

Showing an AlertDialog, OptionsDialog

EditText messageInput= new EditText(getApplicationContext());
messageInput.setPadding(90, 100, 90, 100);

builder.setView(messageInput);
final String[] items = {"Option 1", "Option 2"};

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//using "Activity.this" is necessary

builder.setTitle("Dialog Title");
builder.setNegativeButton("Dismiss", null);

builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
  
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        
        if (i == 0)
            //perform action 1
            
        if (i == 1)
            //perform action 2
    }
});

builder.show();
 AlertDialog.Builder builder = new AlertDialog.Builder (MainActivity.this);
 //using "Activity.this" is necessary
 
builder.setTitle("Dialog Title");
builder.setMessage("Dialog Message");
builder.setCancelable(true);

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        //perform action
        }
    });

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
   
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
          dialogInterface.cancel();
        }
    });
    
//alternative for setting positive/negative buttons
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Dismiss", null);

builder.show();