How do I fire an event when click occurs outside a dialog

When dialog.setCanceledOnTouchOutside(true); then you just override onCancel() like this: dialog.setOnCancelListener( new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { //When you touch outside of dialog bounds, //the dialog gets canceled and this method executes. } } ); Type your code inside the onCancel() method so it runs when the dialog gets canceled.

Show dialog from fragment?

I must cautiously doubt the previously accepted answer that using a DialogFragment is the best option. The intended (primary) purpose of the DialogFragment seems to be to display fragments that are dialogs themselves, not to display fragments that have dialogs to display. I believe that using the fragment’s activity to mediate between the dialog and … Read more

findViewById() returns null for Views in a Dialog

Calling findViewById() will search for views within your Activity’s layout and not your dialog’s view. You need to call findViewById() on the specific View that you set as your dialog’s layout. Try this private void initSearch() { AlertDialog.Builder searchDialog = new AlertDialog.Builder(this); LayoutInflater inflater = this.getLayoutInflater(); searchDialog.setTitle(“Search Photos”); searchDialog.setMessage(“Specify tag and value…”); // R.layout.search_dialog is … Read more

Change background of ProgressDialog

The comment of Aleks G (below the question) points in the right direction. The appearance of the dialog is defined by a separate style (android:alertDialogStyle). But one cannot apply the style directly to a ProgressDialog. Now, how do I get that yellow background? Step 1: Define a theme that inherits from Theme.Dialog: <style name=”MyTheme” parent=”@android:style/Theme.Dialog”> … Read more

Get date from datepicker using dialogfragment

Constructor fo DatePickerDialog takes DatePickerDialog.OnDateSetListener as second parameter, so maybe you should implement that interface in your parent activity EditSessionActivity (not in DatePickerFragment ) and change this line: return new DatePickerDialog(getActivity(), this, year, month, day); into this: return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day); And then your activity should looks like this: public class EditSessionActivity … Read more

How to create a number picker dialog?

I have made a small demo of NumberPicker. This may not be perfect but you can use and modify the same. public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener { private static TextView tv; static Dialog d ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.textView1); Button b = (Button) findViewById(R.id.button11); b.setOnClickListener(new … Read more

How to make custom dialog with rounded corners in android

Create an XML file in drawable, say dialog_bg.xml: <?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android”> <solid android:color=”@color/white”/> <corners android:radius=”30dp” /> <padding android:left=”10dp” android:top=”10dp” android:right=”10dp” android:bottom=”10dp” /> </shape> set it as the background in your layout XML: android:background=”@drawable/dialog_bg” Set the background of the dialog’s root view to transparent, because Android puts your dialog layout within a root view … Read more

How to set DialogFragment’s width and height?

If you convert directly from resources values: int width = getResources().getDimensionPixelSize(R.dimen.popup_width); int height = getResources().getDimensionPixelSize(R.dimen.popup_height); getDialog().getWindow().setLayout(width, height); Then specify match_parent in your layout for the dialog: android:layout_width=”match_parent” android:layout_height=”match_parent” You only have to worry about one place (place it in your DialogFragment#onResume). Its not perfect, but at least it works for having a RelativeLayout as the … Read more