lupidan / PopoverView

A Popover Controller for Android Tablets. It's an easy solution to simulate an iOS UIPopoverController
GNU Lesser General Public License v3.0
202 stars 65 forks source link

Button Event handler in popover not working #6

Closed flaviusdemian closed 10 years ago

flaviusdemian commented 10 years ago

Hy,

I have defined in my popover view a button and defined a click handler for it.
The click handler does not trigger.

RelativeLayout rootView = (RelativeLayout)findViewById(R.id.rootLayout);

    PopoverView popoverView = new PopoverView(this, R.layout.popover_showed_view);
    popoverView.setContentSizeForViewInPopover(new Point(320, 340));
    popoverView.setDelegate(this);

    View popoverViewReference = getLayoutInflater().inflate(R.layout.popover_showed_view, null);    

    if( popoverViewReference != null)
    {
        btn_popoverSearch = (Button) popoverViewReference.findViewById(R.id.btn_test);
        btn_popoverSearch.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                                  //handler code here
            }
        });
    popoverView.showPopoverFromRectInViewGroup(rootView, PopoverView.getFrameForView(v), PopoverView.PopoverArrowDirectionAny, true);
    }

The btn_popoverSearch is found and is not null but the event handler is not called. Any suggestions? Thanks a lot.

flaviusdemian commented 10 years ago

Can anyone help?

lupidan commented 10 years ago

Sorry for the delay. I have to fix my gmail account to receive notifications from github... When you use the constructor with a layout id, like the first line, you are already inflating and creating a view inside the popover view.

What you are doing is: 1: You are creating a popover view for a layout resource, this instantiates internally the FIRST view inside the popover view 2: You are inflating a SECOND view and adding the listener for the SECOND view only, after that, the view is lost 3: You then show the FIRST view, with the button without an onClickListener

What you HAVE to do:

  1. Inflate the view you want and set the onClickListener
  2. Create a popover view with the view you just created
  3. Show the created popover view

Using your code:

//First create popover view reference
View popoverViewReference = getLayoutInflater().inflate(R.layout.popover_showed_view, null);    
if( popoverViewReference != null)
{
    btn_popoverSearch = (Button) popoverViewReference.findViewById(R.id.btn_test);
    btn_popoverSearch.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            //handler code here
        }
    });
}

//Second, create the popover view with the view you just created
PopoverView popoverView = new PopoverView(this, popoverViewReference);
popoverView.setContentSizeForViewInPopover(new Point(320, 340));
popoverView.setDelegate(this);

//Finally, present the popover view
popoverView.showPopoverFromRectInViewGroup(rootView, PopoverView.getFrameForView(v), PopoverView.PopoverArrowDirectionAny, true);

Please tell me if this helps, to close this issue.

flaviusdemian commented 10 years ago

I'll take a look in the week-end but probably it will be ok, Thanks.

flaviusdemian commented 10 years ago

It works, thank you very much.

On Wednesday, November 27, 2013 3:28 PM, Daniel Lupiañez Casares notifications@github.com wrote:

Sorry for the delay. I have to fix my gmail account to receive notifications from github... When you use the constructor with a layout id, like the first line, you are already inflating and creating a view inside the popover view. What you are doing is: 1: You are creating a popover view for a layout resource, this instantiates internally the FIRST view inside the popover view 2: You are inflating a SECOND view and adding the listener for the SECOND view only, after that, the view is lost 3: You then show the FIRST view, with the button without an onClickListener What you HAVE to do:

  1. Inflate the view you want and set the onClickListener
  2. Create a popover view with the view you just created
  3. Show the created popover view Using your code: //First create popover view reference View popoverViewReference = getLayoutInflater().inflate(R.layout.popover_showed_view, null); if( popoverViewReference != null) { btn_popoverSearch = (Button) popoverViewReference.findViewById(R.id.btn_test); btn_popoverSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //handler code here } }); } //Second, create the popover view with the view you just created PopoverView popoverView = new PopoverView(this, popoverViewReference); popoverView.setContentSizeForViewInPopover(new Point(320, 340)); popoverView.setDelegate(this); //Finally, present the popover view popoverView.showPopoverFromRectInViewGroup(rootView, PopoverView.getFrameForView(v), PopoverView.PopoverArrowDirectionAny, true); Please tell me if this helps, to close this issue. — Reply to this email directly or view it on GitHub.