Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-17821] Android: Add Support for Custom View to ActionBar #9425

Merged
merged 21 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -3258,6 +3258,11 @@ public class TiC
*/
public static final String PROPERTY_CUSTOM = "custom";

/**
* @module.api
*/
public static final String PROPERTY_CUSTOM_VIEW = "customView";

/**
* @module.api
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import android.support.v7.app.AppCompatActivity;

@SuppressWarnings("deprecation")
@Kroll.proxy(propertyAccessors = { TiC.PROPERTY_ON_HOME_ICON_ITEM_SELECTED })
@Kroll.proxy(propertyAccessors = { TiC.PROPERTY_ON_HOME_ICON_ITEM_SELECTED, TiC.PROPERTY_CUSTOM_VIEW })
public class ActionBarProxy extends KrollProxy
{
private static final int MSG_FIRST_ID = KrollProxy.MSG_LAST_ID + 1;
Expand All @@ -37,6 +37,7 @@ public class ActionBarProxy extends KrollProxy
private static final int MSG_SET_SUBTITLE = MSG_FIRST_ID + 109;
private static final int MSG_SET_DISPLAY_SHOW_HOME = MSG_FIRST_ID + 110;
private static final int MSG_SET_DISPLAY_SHOW_TITLE = MSG_FIRST_ID + 111;
private static final int MSG_SET_CUSTOM_VIEW = MSG_FIRST_ID + 112;
private static final String SHOW_HOME_AS_UP = "showHomeAsUp";
private static final String HOME_BUTTON_ENABLED = "homeButtonEnabled";
private static final String BACKGROUND_IMAGE = "backgroundImage";
Expand Down Expand Up @@ -378,6 +379,20 @@ private Drawable getDrawableFromUrl(String url)
return tfh.loadDrawable(imageUrl.resolve(), false);
}

private void handleSetCustomView(Object view)
{
if (view != null) {
if (view instanceof TiViewProxy) {
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(((TiViewProxy) view).getOrCreateView().getNativeView());
} else {
Log.w(TAG, "Invalid value passed for a custom view. Expected Ti.UI.View or null");
}
} else {
actionBar.setCustomView(null);
}
}

@Override
public boolean handleMessage(Message msg)
{
Expand Down Expand Up @@ -427,6 +442,9 @@ public boolean handleMessage(Message msg)
case MSG_SET_HOME_BUTTON_ENABLED:
handlesetHomeButtonEnabled(msg.getData().getBoolean(HOME_BUTTON_ENABLED));
return true;
case MSG_SET_CUSTOM_VIEW:
handleSetCustomView(msg.obj);
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of setters without getters. Ideally, this should be a customView property which we should then call handleSetCustomView() when the property changes.

}
return super.handleMessage(msg);
}
Expand All @@ -445,6 +463,14 @@ public void onPropertyChanged(String name, Object value)
message.sendToTarget();
}
}
if (TiC.PROPERTY_CUSTOM_VIEW.equals(name)) {
if (TiApplication.isUIThread()) {
handleSetCustomView(value);
} else {
Message message = getMainHandler().obtainMessage(MSG_SET_CUSTOM_VIEW, value);
message.sendToTarget();
}
}
super.onPropertyChanged(name, value);
}

Expand Down
11 changes: 10 additions & 1 deletion apidoc/Titanium/Android/ActionBar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ methods:
See also:
[hide](http://developer.android.com/reference/android/app/ActionBar.html#hide())
in the Android API Reference.

- name: setDisplayShowHomeEnabled
summary: Shows or hides the action bar home icon
parameters:
Expand Down Expand Up @@ -235,3 +235,12 @@ properties:
- name: title
summary: Sets the title of the action bar.
type: String

- name: customView
summary: Sets a view to be used for a custom navigation mode.
type: Titanium.UI.View
description: |
Inserts a custom view for navigation in the space
between the application's home button and menu actions.
The custom view is trimmed to fit the space available.
since: "7.1.0"