Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import android.app.Activity;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiDrawableReference;
Expand Down Expand Up @@ -49,8 +49,8 @@ public void setTitle(String text)
@Kroll.setProperty
public void setContentView(Object obj)
{
if (obj instanceof TiViewProxy) {
setPropertyAndFire(TiC.PROPERTY_CONTENT_VIEW, (TiViewProxy) obj);
if (obj instanceof TiViewProxy && collapseToolbar != null) {
collapseToolbar.setContentView((TiViewProxy) obj);
}
}

Expand Down Expand Up @@ -101,4 +101,22 @@ public void setNavigationIconColor(String value)
{
collapseToolbar.setNavigationIconColor(TiConvert.toColor(value, TiApplication.getAppCurrentActivity()));
}

@Kroll.method
public void addMenuItem(KrollDict options)
{
collapseToolbar.addMenuItem(options);
}

@Kroll.method
public void removeMenuItem(int itemId)
{
collapseToolbar.removeMenuItem(itemId);
}

@Kroll.method
public void clearMenu()
{
collapseToolbar.clearMenu();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
Expand Down Expand Up @@ -49,6 +51,7 @@ public class TiUICollapseToolbar extends TiUIView
ImageView imageView = null;
MaterialToolbar toolbar = null;
KrollFunction homeIconFunction = null;
KrollFunction menuItemClickFunction = null;
boolean homeAsUp = false;
TiViewProxy localContentView = null;

Expand Down Expand Up @@ -94,6 +97,15 @@ public TiUICollapseToolbar(TiViewProxy proxy)
setContentView(localContentView);
}

toolbar.setOnMenuItemClickListener(item -> {
if (proxy.hasListeners(TiC.EVENT_CLICK)) {
KrollDict event = new KrollDict();
event.put("itemId", item.getItemId());
fireEvent(TiC.EVENT_CLICK, event);
}
return true;
});

setNativeView(layout);
} catch (Exception e) {
Log.e(TAG, "Layout error: " + e.getMessage());
Expand Down Expand Up @@ -257,4 +269,38 @@ public void processProperties(KrollDict d)
setNavigationIconColor(TiConvert.toColor(d.getString(TiC.PROPERTY_NAVIGATION_ICON_COLOR), activity));
}
}

public void addMenuItem(KrollDict options)
{
if (toolbar != null && options.containsKeyAndNotNull(TiC.PROPERTY_TITLE)) {
Menu menu = toolbar.getMenu();
int itemId = menu.size();
if (options.containsKeyAndNotNull(TiC.PROPERTY_ITEM_ID)) {
itemId = options.getInt(TiC.PROPERTY_ITEM_ID);
}
MenuItem item = menu.add(Menu.NONE, itemId, Menu.NONE, options.getString(TiC.PROPERTY_TITLE));
if (options.containsKeyAndNotNull(TiC.PROPERTY_SHOW_AS_ACTION)) {
item.setShowAsAction(options.getInt(TiC.PROPERTY_SHOW_AS_ACTION));
}
}
}

public void removeMenuItem(int itemId)
{
if (toolbar != null) {
Menu menu = toolbar.getMenu();
MenuItem item = menu.findItem(itemId);
if (item != null) {
menu.removeItem(itemId);
}
}
}

public void clearMenu()
{
if (toolbar != null) {
Menu menu = toolbar.getMenu();
menu.clear();
}
}
}
117 changes: 88 additions & 29 deletions apidoc/Titanium/UI/Android/CollapseToolbar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,63 +78,122 @@ properties:
type: Callback
permission: write-only

methods:
- name: addMenuItem
summary: Adds a menu item from the passed creation options.
since: "12.7.0"
parameters:
- name: options
summary: |
Creation options. Supported options are
[itemId](Titanium.Android.MenuItem.itemId),
[title](Titanium.Android.MenuItem.title) and
[showAsAction](Titanium.Android.MenuItem.showAsAction).
type: Object

- name: clearMenu
summary: Removes all menu items
since: "12.7.0"

- name: removeMenuItem
summary: Removes the menu item with the provided `itemId`.
since: "12.7.0"
parameters:
- name: itemId
summary: |
The itemId of the menu item
type: Number

events:
- name: click
summary: Fired when the user clicks a menu item.
since: "12.7.0"
properties:
- name: itemId
summary: |
itemId of the clicked menu item
type: Number

examples:
- title: Simple setup
example: |
The following code shows a simple drawer-layout usage.

``` js
const win = Ti.UI.createWindow({
theme: "Theme.Titanium.DayNight.NoTitleBar"
theme: "Theme.Titanium.DayNight.NoTitleBar"
});

const centerView = Ti.UI.createView({
layout: "vertical",
height: Ti.UI.SIZE
layout: "vertical",
height: Ti.UI.SIZE
});

for (let i = 0; i < 40; ++i) {
let lbl = Ti.UI.createLabel({
text: "test " + i,
height: 40
});
centerView.add(lbl);
let lbl = Ti.UI.createLabel({
text: "test " + i,
height: 40
});
centerView.add(lbl);
}

const collapsingToolbar = Ti.UI.Android.createCollapseToolbar({
contentScrimColor: "#333",
top: 0,
image: "default.png",
title: "Collapsing toolbar"
contentScrimColor: "#333",
top: 0,
image: "default.png",
title: "Collapsing toolbar",
contentView: centerView
});


win.addEventListener('open', function() {
collapsingToolbar.contentView = centerView;
collapsingToolbar.onHomeIconItemSelected = function() {
alert("click click");
};
collapsingToolbar.displayHomeAsUp = true;
collapsingToolbar.onHomeIconItemSelected = function() {
alert("click click");
};
collapsingToolbar.displayHomeAsUp = true;
});

win.add(collapsingToolbar);
win.open();
```
- title: Alloy example
example: |
``` js
``` xml
<Alloy>
<Window>
<Window>
<CollapseToolbar platform="android">
<ContentView>
<View backgroundColor="red" height="Ti.UI.SIZE">
<Label>test</Label>
</View>
</ContentView>
</CollapseToolbar>
</Window>
</Alloy>
```
- title: Menu example
example: |
``` js
const win = Ti.UI.createWindow({
theme: "Theme.Titanium.DayNight.NoTitleBar"
});

<CollapseToolbar platform="android">
const collapsingToolbar = Ti.UI.Android.createCollapseToolbar({
contentScrimColor: "#333",
top: 0,
title: "Collapsing toolbar",
});

<ContentView>
<View backgroundColor="red" height="Ti.UI.SIZE">
<Label>test</Label>
</View>
</ContentView>
</CollapseToolbar>

</Window>
</Alloy>
win.addEventListener('open', function() {
collapsingToolbar.addEventListener("click", function(e) {
alert(e.itemId + " clicked");
});
collapsingToolbar.addMenuItem({
title: "Menu item",
showAsAction: Ti.Android.SHOW_AS_ACTION_NEVER
});
});

win.add(collapsingToolbar);
win.open();
```
Loading