Android Alert Dialog with one, two, and three buttons

One button import android.support.v7.app.AlertDialog; public class MainActivity extends AppCompatActivity { public void showAlertDialogButtonClicked(View view) { // setup the alert builder AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(“My title”); builder.setMessage(“This is my message.”); // add a button builder.setPositiveButton(“OK”, null); // create and show the alert dialog AlertDialog dialog = builder.create(); dialog.show(); } } Two buttons public class … Read more

AlertDialog with EditText, open soft keyboard automatically with focus on EditText doesn’t work

Ok I managed to get it working: Builder builder = new Builder(this); final EditText input = new EditText(this); builder .setTitle(R.string.dialog_title_addsubject) .setMessage(R.string.dialog_addsubject) .setView(input) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String value = input.getText().toString(); if (input.getText().toString().trim().length() == 0) { Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show(); } else { db.insertSubject(value); getData(); } InputMethodManager imm = (InputMethodManager) … Read more

AlertDialog styling – how to change style (color) of title, message, etc

You need to define a Theme for your AlertDialog and reference it in your Activity’s theme. The attribute is alertDialogTheme and not alertDialogStyle. Like this: <style name=”Theme.YourTheme” parent=”@android:style/Theme.Holo”> … <item name=”android:alertDialogTheme”>@style/YourAlertDialogTheme</item> </style> <style name=”YourAlertDialogTheme”> <item name=”android:windowBackground”>@android:color/transparent</item> <item name=”android:windowContentOverlay”>@null</item> <item name=”android:windowIsFloating”>true</item> <item name=”android:windowAnimationStyle”>@android:style/Animation.Dialog</item> <item name=”android:windowMinWidthMajor”>@android:dimen/dialog_min_width_major</item> <item name=”android:windowMinWidthMinor”>@android:dimen/dialog_min_width_minor</item> <item name=”android:windowTitleStyle”>…</item> <item name=”android:textAppearanceMedium”>…</item> <item name=”android:borderlessButtonStyle”>…</item> <item name=”android:buttonBarStyle”>…</item> </style> … Read more

Simplest yes/no dialog fragment

A DialogFragment is really just a fragment that wraps a dialog. You can put any kind of dialog in there by creating and returning the dialog in the onCreateDialog() method of the DialogFragment. Heres an example DialogFragment: class MyDialogFragment extends DialogFragment{ Context mContext; public MyDialogFragment() { mContext = getActivity(); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) … Read more

How to resize AlertDialog on the Keyboard display

If your dialog was an activity using one of the Dialog themes you could effect this behavior by setting the adjustResize flag for the windowSoftInputMode parameter of the activity. I’m using: android:windowSoftInputMode=”adjustResize|stateHidden” I think you can still use this flag with regular dialogs, but I’m not sure how to apply it. You may have to … Read more

How to add an icon before each item in alert dialog?

You need custom ListAdapter to add your image. One way is to subclass the ArrayAdapter (used by default by the AlertDialog). Here is an example: final Item[] items = { new Item(“Email”, android.R.drawable.ic_menu_add), new Item(“Facebook”, android.R.drawable.ic_menu_delete), new Item(“…”, 0),//no icon for this one }; ListAdapter adapter = new ArrayAdapter<Item>( this, android.R.layout.select_dialog_item, android.R.id.text1, items){ public View … Read more