Mihai Todor
Mihai Todor

Reputation: 8274

DialogFragment - Implement an OK button and other issues

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>

Upvotes: 0

Views: 2506

Answers (1)

A Random Android Dev
A Random Android Dev

Reputation: 2691

I'll try to answer each of your questions:

1.) About the "how to tell it that the implementation for the OK button event handler is located in my custom DialogFragment class" I'm not sure why you want to do that since you haven't told us.

2.) "Would calling getDialog().dismiss() be enough to dismiss it?". If you wish to dismiss the DialogFragment from your Activity then you just need to call dialog.dismiss() ('dialog' here refers to ResponseDialog dialog = new ResponseDialog(); so obviously you can only call dialog.dismiss() once you've created the ResponseDialog object.

3.) Regarding "getDialog().setCanceledOnTouchOutside(true);" Once again you just need to call

dialog.setCanceledOnTouchOutside(true); 

immediately after this:

ResponseDialog dialog = new ResponseDialog();
dialog.show(getFragmentManager(), "dialog_response_image");

4.) You can get access to its view by calling:

View v = dialog.getView();

Although if you just wish to set the source of the image to be used contained by it, and I assume you want to use a photo or a picture stored in your phone then you would have to use a parametrized constructor for ResponseDialog like this and as a parameter you would have to pass the URI OR the filepath of the picture that you would like displayed in the dialogfragment :

ResponseDialog dialog = new Response(String filepath);

and then in your custom dialog class which I understand is ResponseDialog you would have to use this filepath (received in the constructor) to create a bitmap and then set the bitmap as the source of the ImageView in that DialogFragment.

Upvotes: 1

Related Questions