AlertDialog dialog = dialogBuilder.create(); dialog.show(); Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE); theButton.setOnClickListener(new CustomListener(dialog));
AlertDialog.Builder builder = new AlertDialog.Builder(AddSchedule.this); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setTitle("Alert dialog title"); builder.setMessage("Dialog message"); builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { //DO TASK } }); builder.setNegativeButton("Button2", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { //DO TASK } }); AlertDialog dialog = builder.create(); dialog.show(); //After calling show method, you need to check your condition and //enable/ disable buttons of dialog if(your_condition_true) dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); //BUTTON1 is positive button
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this); builder.setCancelable(false) .setMessage("Please Enter data") .setView(edtLayout) //<-- layout containing EditText .setPositiveButton("Enter", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //All of the fun happens inside the CustomListener now. //I had to move it to enable data validation. } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); Button theButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE); theButton.setOnClickListener(new CustomListener(alertDialog));
if(d != null) Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE); positiveButton.setOnClickListener(new View.OnClickListener()
//1. create a dialog object 'dialog' MyCustomDialog builder = new MyCustomDialog(getActivity(), "Try Again", errorMessage); AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { ... } }).create(); //2. now setup to change color of the button dialog.setOnShowListener( new OnShowListener() { @Override public void onShow(DialogInterface arg0) { dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(COLOR_I_WANT); } } dialog.show()
@Override protected void onPrepareDialogBuilder(Builder builder) { super.onPrepareDialogBuilder(builder); builder.setPositiveButton("Test", this); //Set the button here so it gets created } @Override protected void showDialog(Bundle state) { super.showDialog(state); //Call show on default first so we can override the handlers final AlertDialog d = (AlertDialog) getDialog(); d.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Boolean wantToCloseDialog = false; //Do stuff, possibly set wantToCloseDialog to true then... if(wantToCloseDialog) d.dismiss(); //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false. } }); }
@Test public void shouldAllowNullButtonListeners() throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(application); builder.setPositiveButton("OK", null); AlertDialog dialog = builder.create(); dialog.show(); ShadowView.clickOn(dialog.getButton(AlertDialog.BUTTON_POSITIVE)); }
@Test public void shouldOnlyCreateRequestedButtons() throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(application); builder.setPositiveButton("OK", null); AlertDialog dialog = builder.create(); dialog.show(); assertThat(dialog.getButton(AlertDialog.BUTTON_POSITIVE).getVisibility()) .isEqualTo(View.VISIBLE); assertThat(dialog.getButton(AlertDialog.BUTTON_NEGATIVE).getVisibility()).isEqualTo(View.GONE); }
@Test public void clickingPositiveButtonDismissesDialog() throws Exception { AlertDialog alertDialog = new AlertDialog.Builder(application) .setPositiveButton("Positive", null).create(); alertDialog.show(); assertTrue(alertDialog.isShowing()); alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); assertFalse(alertDialog.isShowing()); }
@Test public void clickingNegativeButtonDismissesDialog() throws Exception { AlertDialog alertDialog = new AlertDialog.Builder(application) .setNegativeButton("Negative", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create(); alertDialog.show(); assertTrue(alertDialog.isShowing()); alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).performClick(); assertFalse(alertDialog.isShowing()); }
@Test public void shouldNotExplodeWhenNestingAlerts() throws Exception { final Activity activity = Robolectric.buildActivity(Activity.class).create().get(); final AlertDialog nestedDialog = new AlertDialog.Builder(activity) .setTitle("Dialog 2") .setMessage("Another dialog") .setPositiveButton("OK", null) .create(); final AlertDialog dialog = new AlertDialog.Builder(activity) .setTitle("Dialog 1") .setMessage("A dialog") .setPositiveButton("Button 1", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { nestedDialog.show(); } }).create(); dialog.show(); assertThat(ShadowDialog.getLatestDialog()).isEqualTo(dialog); dialog.getButton(Dialog.BUTTON_POSITIVE).performClick(); assertThat(ShadowDialog.getLatestDialog()).isEqualTo(nestedDialog); }
@Test public void clickingNeutralButtonDismissesDialog() throws Exception { AlertDialog alertDialog = new AlertDialog.Builder(application) .setNeutralButton("Neutral", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create(); alertDialog.show(); assertTrue(alertDialog.isShowing()); alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).performClick(); assertFalse(alertDialog.isShowing()); }
@Test public void testReset() { PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putBoolean(activity.getString(R.string.pref_color_code), false) .apply(); assertFalse(Preferences.colorCodeEnabled(activity)); assertNotNull(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_reset)); shadowOf(activity).clickMenuItem(R.id.menu_reset); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertTrue(Preferences.colorCodeEnabled(activity)); }
@Test public void testClearRecentSearches() { ShadowSearchRecentSuggestions.historyClearCount = 0; assertNotNull(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_clear_recent)); shadowOf(activity).clickMenuItem(R.id.menu_clear_recent); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertEquals(1, ShadowSearchRecentSuggestions.historyClearCount); }
@Test public void testSubmitFailed() { ((EditText) activity.findViewById(R.id.edittext_title)).setText("title"); ((EditText) activity.findViewById(R.id.edittext_content)).setText("http://example.com"); shadowOf(activity).clickMenuItem(R.id.menu_send); ShadowAlertDialog.getLatestAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE) .performClick(); verify(userServices).submit(any(Context.class), eq("title"), eq("http://example.com"), eq(true), submitCallback.capture()); submitCallback.getValue().onDone(false); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, LoginActivity.class); }
@Test public void testExitSaveDraft() { ((EditText) activity.findViewById(R.id.edittext_body)).setText("Reply"); shadowOf(activity).clickMenuItem(android.R.id.home); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertThat(activity).isFinishing(); assertThat(Preferences.getDraft(activity, "1")).contains("Reply"); }
@Test public void testSubmitSuccessful() { ((EditText) activity.findViewById(R.id.edittext_title)).setText("title"); ((EditText) activity.findViewById(R.id.edittext_content)).setText("http://example.com"); shadowOf(activity).clickMenuItem(R.id.menu_send); ShadowAlertDialog.getLatestAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE) .performClick(); verify(userServices).submit(any(Context.class), eq("title"), eq("http://example.com"), eq(true), submitCallback.capture()); submitCallback.getValue().onDone(true); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, NewActivity.class); assertThat(activity).isFinishing(); }
@Test public void testExitDiscardDraft() { ((EditText) activity.findViewById(R.id.edittext_body)).setText("Reply"); shadowOf(activity).clickMenuItem(android.R.id.home); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); assertThat(activity).isFinishing(); assertThat(Preferences.getDraft(activity, "1")).isNullOrEmpty(); }
@Test public void testOnBackPressed() { shadowOf(activity).clickMenuItem(android.R.id.home); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertThat(activity).isFinishing(); }
@Config(sdk = 21) @Test public void testRemoveAccount() { AccountManager.get(activity).addAccountExplicitly(new Account("existing", BuildConfig.APPLICATION_ID), "password", null); Preferences.setUsername(activity, "existing"); drawerAccount.performClick(); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); assertThat(alertDialog.getListView().getAdapter()).hasCount(1); alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL).performClick(); assertThat(alertDialog).isNotShowing(); assertThat(AccountManager.get(activity).getAccounts()).isEmpty(); }