How to get View of Menu on ActionBar

6,013 views
Skip to first unread message

Saurabh Gupta

unread,
Mar 9, 2012, 7:26:39 AM3/9/12
to ActionBarSherlock
Hi

I am using setHasOptionsMenu(true) and onCreateOptionsMenu() to have a
Menu on the Action Bar.

Later, I need to have a Popover Dialog on the Click of this Menu item.
For this I need to have the View of the Menu on the Action Bar (View
coordinates so that I can place my Popover Dialog properly).

My problem is that I am not able to get this View. I have used below
code for this, but in vain:


1) public boolean onOptionsItemSelected(MenuItem item) {
getView().refreshDrawableState();
View view = getView().findViewById(R.id.abs__actions);
return super.onOptionsItemSelected(item);
}

2) public boolean onOptionsItemSelected(MenuItem item) {
View view = item.getActionView().findViewById(R.id.abs__actions));
return super.onOptionsItemSelected(item);
}


Please suggest the way of getting the View for the Menu. Also, if
there are two Menus then what will be the case.
Any help will be much appreciated!

Thanks in advance :)

Jake Wharton

unread,
Mar 9, 2012, 12:31:00 PM3/9/12
to actionba...@googlegroups.com
There is no easy way to get at the actual View for an action item. The only way would be to set a custom view, use the regular action item styling, and then getActionView.

Saurabh Gupta

unread,
Mar 12, 2012, 12:58:40 AM3/12/12
to ActionBarSherlock
Hi,

Thanks for replying.
Can you please give an example how to do what you have said below.
Actually, I am new to Android development and don't know the concepts
really well.

Thanks in advance for your help.

On Mar 9, 10:31 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> There is no easy way to get at the actual View for an action item. The only
> way would be to set a custom view, use the regular action item styling, and
> then getActionView.
>
> ---
> Jake Whartonhttp://about.me/jakewharton
>
>
>
> On Fri, Mar 9, 2012 at 4:26 AM, Saurabh Gupta <saurabh3...@gmail.com> wrote:
> > Hi
>
> > I am using setHasOptionsMenu(true) and onCreateOptionsMenu() to have a
> > Menu on the Action Bar.
>
> > Later, I need to have a Popover Dialog on the Click of this Menu item.
> > For this I need to have the View of the Menu on the Action Bar (View
> > coordinates so that I can place my Popover Dialog properly).
>
> > My problem is that I am not able to get this View. I have used below
> > code for this, but in vain:
>
> > 1) public boolean onOptionsItemSelected(MenuItem item) {
> >    getView().refreshDrawableState();
> >    View view = getView().findViewById(R.id.abs__actions);
> >    return super.onOptionsItemSelected(item);
> > }
>
> > 2) public boolean onOptionsItemSelected(MenuItem item) {
> >    View view = item.getActionView().findViewById(R.id.abs__actions));
> >    return super.onOptionsItemSelected(item);
> > }
>
> > Please suggest the way of getting the View for the Menu. Also, if
> > there are two Menus then what will be the case.
> > Any help will be much appreciated!
>
> > Thanks in advance :)- Hide quoted text -
>
> - Show quoted text -

Shaver Sport

unread,
Mar 12, 2012, 12:29:49 PM3/12/12
to actionba...@googlegroups.com
Here's what I did:

myTab = mActionBar.newTab().setCustomView(
                createCustomTabView(R.drawable.ic_tab_mytab));

 public View createCustomTabView(int resid) {
        Drawable icon = getResources().getDrawable(resid);
        ImageView iconView = new ImageView(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER_VERTICAL;
        iconView.setLayoutParams(lp);
        iconView.setImageDrawable(icon);
        return iconView;
    }

Shaver Sport

unread,
Mar 12, 2012, 12:31:27 PM3/12/12
to actionba...@googlegroups.com
Course that's for a tab, not an action item.  Maybe you can use some of that though.

minghu huang

unread,
Sep 12, 2012, 1:58:03 PM9/12/12
to actionba...@googlegroups.com, saura...@gmail.com
I have the same problem... I am trying, following your way...
Reply all
Reply to author
Forward
0 new messages