Nativescript: Ability to define a custom view in ActionItem

Created on 4 Feb 2016  路  10Comments  路  Source: NativeScript/NativeScript

Add ability for ActionItem to have a custom view associated with it, instead of using predefined icon/text. For example something like:

<ActionBar title="Something">
  <ActionBar.actionItems>
    <ActionItem ios.position="right" tap="doneTap">
        <ActionItem.actionView>
            <StackLayout>
                <Label text="Label1" backgroundColor="red"/>
                <Label text="Label2" backgroundColor="green"/>
            </StackLayout>
        </ActionItem.actionView>
    </ActionItem>
  </ActionBar.actionItems>
</ActionBar>

Implementation suggestion:
For iOS - use - (id)initWithCustomView:(UIView *)customView of the UIBarButtonItem. The catch is that in order to be able to handle tap the custom view must be a button. So the custom view must be added as a sub view to a UIButton. And then the UIButton should be passed to the initWithCustomView method.

For Android - use setActionView of MenuItem http://developer.android.com/reference/android/view/MenuItem.html#setActionView(android.view.View)

done feature

Most helpful comment

Does this work with "android.position='popup' ? For me, it is not working and places the inner items outside of the popup and into the action bar.

Here is the code for my action items -- the third one has the inner label:

 <ActionItem text="Change Layout" android.position="popup" (tap)="changeDisplayOfItems()"></ActionItem>
    <ActionItem text=" Sort by cost" android.position="popup" (tap)="sortItemsBy('size')"></ActionItem>
    <ActionItem android.position="popup" (tap)="addNewItem()">
      <Label text="Hi"></Label>
    </ActionItem>
    <ActionItem text="Account Info" (tap)="accountInfo()" android.position="popup"></ActionItem>

But this is what is happening:

actionitem

All 10 comments

Hey @PeterStaev,

Great suggestion! If you have already something working you can submit pull request and we will gladly help with the implementation.

@enchev not exactly like this, but have made something similar in a custom actionbar implementation. So will see if to plug it in the {N} code.

I'm sorry but I quite don't get your solution. I'm quite new to NativeScript, do you have a simple sample app about ActionBar and ActionItem customization? I would appreciate really much.

How about _without_ having to use XML? I'm trying to approach the ActionBar programmatically, yet all examples start with XML.

Hey @bfattori,

You can create some View (for example Label) and assign in to ActionItem actionView property:
https://github.com/NativeScript/NativeScript/blob/master/ui/action-bar/action-bar.d.ts#L124

I LOVE YOU GUYS

I LOVE YOU GUYS [2]

Does this work with "android.position='popup' ? For me, it is not working and places the inner items outside of the popup and into the action bar.

Here is the code for my action items -- the third one has the inner label:

 <ActionItem text="Change Layout" android.position="popup" (tap)="changeDisplayOfItems()"></ActionItem>
    <ActionItem text=" Sort by cost" android.position="popup" (tap)="sortItemsBy('size')"></ActionItem>
    <ActionItem android.position="popup" (tap)="addNewItem()">
      <Label text="Hi"></Label>
    </ActionItem>
    <ActionItem text="Account Info" (tap)="accountInfo()" android.position="popup"></ActionItem>

But this is what is happening:

actionitem

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rLoka picture rLoka  路  3Comments

NickIliev picture NickIliev  路  3Comments

guillaume-roy picture guillaume-roy  路  3Comments

dhanalakshmitawwa picture dhanalakshmitawwa  路  3Comments

tsonevn picture tsonevn  路  3Comments