how to handle onclick event of button inside popup window in android
- by henna
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"
 />