I am trying to implement a relatively simple DialogFragment
that is supposed to contain an image and a OK button and I want to display it on demand from my activity.
I set its layout in onCreateView
via inflater.inflate
, but I can't figure out how to tell it that the implementation for the OK button event handler is located in my custom DialogFragment class. It seems to try to find it in the activity, which is not what I want. Would calling getDialog().dismiss()
be enough to dismiss it?
Here is how I create a dialog in my activity:
ResponseDialog dialog = new ResponseDialog();
dialog.show(getFragmentManager(), "dialog_response_image");
Also, some folks say that my custom DialogFragment should set getDialog().setCanceledOnTouchOutside(true);
, but where should I set this. In onActivityCreated
?
How can I get access to its view from the activity, if I wish to set the source of the image contained by it?
Also, for some reason, it fills up the entire display, even if I use static width / height. Does anyone know how to fix this? - I managed to fix this by swithching to LinearLayout
instead of RelativeLayout
in the DialogFragment
layout XML...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/background_color"
tools:context=".MainActivity"
android:layout_width="200dp"
android:layout_height="400dp"
android:id="@+id/dialogImageReponse" >
<Button
android:id="@+id/dialogButtonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/ok"
android:onClick="Ok" />
</RelativeLayout>