how to handle onclick event of button inside popup window in android

Posted by henna on Stack Overflow See other posts from Stack Overflow or by henna
Published on 2010-06-08T12:45:15Z Indexed on 2010/06/08 12:52 UTC
Read the original article Hit count: 279

Filed under:

In my application i have a button initially on the screen and in onclick of the button ,popup window should open. In the popup window i have an imagebutton , now on click of this button i want to start an activity. The popup window opens but i dont understand how to handle the onclick of the imagebtn inside popup window.

In main.xml i have a button and in popup_example.xml i have an imagebutton.

Hi my Java code is as follows:

           final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    final Button b=(Button)findViewById(R.id.btn);
    b.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main)));
            pw.showAtLocation(v, Gravity.LEFT,0,0);
            pw.update(8,-70,150,270);

              //if onclick written here, it gives null pointer exception.
            ImageButton img=(ImageButton)findViewById(R.id.home);
            img.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    Intent.....
                }
            });


             //if onclick is written here it gives runtime exception.

});

and i have two xml layouts.........1)main.xml 2)popup_example.xml

main.xml

popup_example.xml

<ImageButton android:id="@+id/home"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:focusable="true"
 android:src="@drawable/vitalss"
 android:layout_weight="1"
   android:background="#8E2323"
 />

© Stack Overflow or respective owner

Related posts about android