Menelaos Kotsollaris
Menelaos Kotsollaris

Reputation: 5506

View.OnClickListener in a custom DialogFragment

I am trying to implement a custom DialogFragment, following this tutorial. My problem is that I fail to handle my custom's view's button.setOnClickListener event. The strangest part is that I have no problem in geting the .getText() of my button, I just can't find a way to handle the click event. Bellow is my code:

SettingsDialogFragment.java

 public class SettingsDialogFragment extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.dialog_settings, null);

    final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings);
    String s = colorButton.getText().toString();
    System.out.println("its working "+s);

    //NOT working
    colorButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            System.out.println("OnClick");
        }
    });

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_settings, null))
            // Add action buttons
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id)
                {

                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SettingsDialogFragment.this.getDialog().cancel();
                }
            });
    return builder.create();
}

` My custom view code (dialog_settings.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="200dp"
          android:layout_height="wrap_content">
<EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:scaleType="center"
    android:background="#00CCCC"
    android:contentDescription="@string/app_name"
    android:text="@string/dialog_settings_title"
    android:id="@+id/editText"/>
<Button
    android:id="@+id/colorButton_dialogSettings"
    android:inputType="textEmailAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/color_picker_title"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stroke"
    android:layout_marginLeft="55dp"
    android:id="@+id/radioButtonStroke"
    android:checked="false"
    android:layout_below="@+id/colorButton_dialogSettings"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fill"
    android:id="@+id/radioButton_fill"
    android:checked="false"
    android:layout_below="@+id/colorButton_dialogSettings"
    android:layout_toRightOf="@+id/radioButtonStroke"
    android:layout_toEndOf="@+id/radioButtonStroke"
    android:layout_marginLeft="10dp"
    />

Upvotes: 2

Views: 4606

Answers (2)

Elltz
Elltz

Reputation: 10859

I am just showing you the important part.. i hope you find their respective lines in your code

final View view = inflater.inflate(R.layout.dialog_settings, null);
// inflating your view..for drawback, this line is [A]

your colorButton has a reference to view.findViewById(R.id.colorButton_dialogSettings) which is from the viewgroup view..which you reference an onclick listener for it..

builder.setView(inflater.inflate(R.layout.dialog_settings, null))

this code right here sets the content view for your dialog. it inflates a the layout and does it's work.. so in the end your builder is not referencing it's content view to view but a new inflated R.layout.dialog_settings layout..

so to solve it just do this

builder.setView(view) // hope you know the view parameter

the view is what you instantiated at line [A] ..

Hope i made sense, and lucid enough for you..let me know if it helps

Upvotes: 6

Johnny
Johnny

Reputation: 705

New Answer

Change your onCreateDialog to this:

import android.view.View.OnClickListener; 

public class SettingsDialogFragment extends DialogFragment implements onClickListener
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.dialog_settings, null);

    final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings);
    String s = colorButton.getText().toString();
    System.out.println("its working "+s);

    colorButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId()) {
                case R.id.colorButton_dialogSettings
                    System.out.println("OnClick");
                    break;
                default:
                    break;
        }
    });

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_settings, null))
            // Add action buttons
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id)
                {

                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SettingsDialogFragment.this.getDialog().cancel();
                }
            });
    return builder.create();
}

In your activity:

private Button colorButton = (Button) findViewById(R.id.colorButton_dialogSettings);

**Old answer that requires you to write your own showDialog method**

Try deleting your button code in the onCreateDialog and add this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_dialog, container, false);
        Button colorButton =(Button) v.findViewById(R.id.colorButton_dialogSettings);
            public void onClick(View v) {
            // When button is clicked, call up to owning activity.
                ((FragmentDialog)getActivity()).showDialog();
                System.out.println("OnClick");
            }
        });

        return v;

Upvotes: 0

Related Questions