Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I define my OnClickListener to close a custom dialog view in Android?

I have defined the following custom dialog view:

public class MyDialog extends Dialog {
    public MyDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);

        getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
}

The dialog uses the following layout which contains only a "Dismiss Me" button:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" >

        <Button
            android:id="@+id/dismiss_btn"
            android:layout_width="100dip"
            android:layout_height="30dip"
            android:layout_centerHorizontal="true"
            android:text="Dismiss me"
            android:textSize="8dip"
            android:textColor="#ffffff" />
</RelativeLayout>

My MainActivity displays a button, triggerDialogBtn, which will show my dialog when pressed. I also defined a handler method for the button dismiss_btn on my dialog which is intended to dismiss my dialog when pressed.

public class MainActivity extends Activity{
    private Button triggerDialogBtn;
    private MyDialog myDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //initialize my dialog
        myDialog = new MyDialog(this);

        //the button which will trigger the dialog to pop up
        Button triggerDialogBtn = (Button)findViewById(R.id.trigger_btn);
        triggerDialogBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog.show(); //my dialog pop up
            }
        });

        // I would like to dismiss my dialog when the "Dismiss me" button on the dialog is pressed
        /**** BUT I GOT NULL POINTER EXCEPTION HERE ******/

        //This is the "Dismiss me" button defined on dialog layout
        Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn); 
        dismissMeBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 myDialog.dismiss(); 
            }
        });
    }
}

Layout of main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- The trigger button which will pop up my dialog when pressed -->
    <Button
        android:id="@+id/trigger_btn"
        android:layout_width="80dip"
        android:layout_height="20dip"
        android:text="Trigger dialog" />
</LinearLayout>

The problem is that if I define the click handler for dismiss_btn (which is located on my dialog ) inside MainActivity, I cannot dismiss the dialog when dismiss_btn is pressed; it always throws NullPointerException.

So, where and how can I define the handler to dismiss my dialog when the button on my dialog is pressed?

UPDATED

I also tried to put the dismiss_btn handler in my custom dialog view class as below:

public class MyDialog extends Dialog {
    public MyDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);

        Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn);

        //ERROR: The method setOnClickListener(View.OnClickListener) in the 
        //       type View is not applicable 
        //       for the arguments (new DialogInterface.OnClickListener(){})

        dismissMeBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ???.dismiss(); // What should replace "???"
            }
        });

        getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
}

I get the indicated error don't know what to replace the question marks with.

like image 573
Leem Avatar asked Dec 22 '22 11:12

Leem


1 Answers

Your activity's content view (as set at the beginning of onCreate) is layout "main", and from that activity you're trying to access a view (dismiss_btn) in another layout entirely. Try moving this code:

  Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn); //This is the "Dismiss me" button defined on dialog layout
  dismissMeBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             /* Changed to reflect access from listener within MyDialog */
             MyDialog.this.dismiss(); 
        }
      });

Into your dialog - edit as needed.

like image 146
Nathan Fig Avatar answered Dec 27 '22 02:12

Nathan Fig