diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/TabGroupProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/TabGroupProxy.java index aedf4b092c7..d5cd9e2a1bc 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/TabGroupProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/TabGroupProxy.java @@ -45,6 +45,7 @@ TiC.PROPERTY_TABS_BACKGROUND_COLOR, TiC.PROPERTY_TABS_BACKGROUND_SELECTED_COLOR, TiC.PROPERTY_SWIPEABLE, + TiC.PROPERTY_AUTO_TAB_TITLE, TiC.PROPERTY_EXIT_ON_CLOSE, TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK }) @@ -68,11 +69,13 @@ public class TabGroupProxy extends TiWindowProxy implements TiActivityWindow private Object selectedTab; // NOTE: Can be TabProxy or Number private String tabGroupTitle = null; private static int id_toolbar; + private boolean autoTabTitle = false; public TabGroupProxy() { super(); defaultValues.put(TiC.PROPERTY_SWIPEABLE, true); + defaultValues.put(TiC.PROPERTY_AUTO_TAB_TITLE, autoTabTitle); defaultValues.put(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK, true); } @@ -246,6 +249,9 @@ public void handleCreationDict(KrollDict options) } } + if (options.containsKeyAndNotNull(TiC.PROPERTY_AUTO_TAB_TITLE)) { + autoTabTitle = options.getBoolean(TiC.PROPERTY_AUTO_TAB_TITLE); + } if (options.containsKeyAndNotNull(TiC.PROPERTY_TABS)) { setTabs(options.get(TiC.PROPERTY_TABS)); } @@ -277,7 +283,11 @@ public void setTitle(String title) { // If the native view is drawn directly set the String as a title for the SupportActionBar. if (view != null) { - ((TiUIAbstractTabGroup) view).updateTitle(title); + if (autoTabTitle) { + ((TiUIAbstractTabGroup) view).updateTitle(""); + } else { + ((TiUIAbstractTabGroup) view).updateTitle(title); + } } else { // If the native view is not yet drawn save the value to be passed during creation. this.tabGroupTitle = title; @@ -336,7 +346,11 @@ public void windowCreated(TiBaseActivity activity, Bundle savedInstanceState) } // If we have set a title before the creation of the native view, set it now. if (this.tabGroupTitle != null) { - ((TiUIAbstractTabGroup) view).updateTitle(this.tabGroupTitle); + if (autoTabTitle) { + ((TiUIAbstractTabGroup) view).updateTitle(""); + } else { + ((TiUIAbstractTabGroup) view).updateTitle(this.tabGroupTitle); + } } setModelListener(view); @@ -381,6 +395,15 @@ public void onWindowActivityCreated() // Finish open handling by loading proxy settings. handlePostOpen(); + if (autoTabTitle) { + // expand title to fix truncated title + TiUIAbstractTabGroup tabGroup = (TiUIAbstractTabGroup) view; + tabGroup.updateTitle(tabs.get(0).getProperty(TiC.PROPERTY_TITLE).toString() + " " + + tabs.get(0).getProperty(TiC.PROPERTY_TITLE).toString()); + + // clear it again + tabGroup.updateTitle(""); + } super.onWindowActivityCreated(); } @@ -394,10 +417,10 @@ protected void handlePostOpen() } // Load any tabs added before the tab group opened. - TiUIAbstractTabGroup tg = (TiUIAbstractTabGroup) view; + TiUIAbstractTabGroup tabGroup = (TiUIAbstractTabGroup) view; for (TabProxy tab : tabs) { if (tab != null) { - tg.addTab(tab); + tabGroup.addTab(tab); } } @@ -405,7 +428,7 @@ protected void handlePostOpen() // then we need to invoke onTabSelected so focus/blur event fire appropriately TabProxy tab = getActiveTabProxy(); if (tab != null) { - tg.selectTab(tab); + tabGroup.selectTab(tab); selectedTab = tab; } diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tabgroup/TiUIAbstractTabGroup.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tabgroup/TiUIAbstractTabGroup.java index ea4f56ad62e..90a8e54d7ea 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tabgroup/TiUIAbstractTabGroup.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tabgroup/TiUIAbstractTabGroup.java @@ -158,14 +158,16 @@ public abstract class TiUIAbstractTabGroup extends TiUIView // endregion // region private fields + private boolean autoTabTitle = false; + private int lastTab = -1; + private AtomicLong fragmentIdGenerator = new AtomicLong(); + private ArrayList tabFragmentIDs = new ArrayList(); + protected ArrayList tabs = new ArrayList(); private final boolean isUsingSolidTitaniumTheme; private int colorBackgroundInt; private int colorSurfaceInt; private int colorPrimaryInt; private int colorOnSurfaceInt; - private final AtomicLong fragmentIdGenerator = new AtomicLong(); - private final ArrayList tabFragmentIDs = new ArrayList<>(); - protected ArrayList tabs = new ArrayList<>(); // endregion public TiUIAbstractTabGroup(final TabGroupProxy proxy, TiBaseActivity activity) @@ -422,6 +424,12 @@ public void selectTab(int tabIndex) @Override public void onPageScrolled(int i, float v, int i1) { + if (autoTabTitle && i != lastTab) { + if (tabs.get(i).getWindowProxy() != null) { + updateTitle(tabs.get(i).getWindowProxy().getProperty(TiC.PROPERTY_TITLE).toString()); + } + lastTab = i; + } } @Override @@ -470,6 +478,9 @@ public void processProperties(KrollDict d) if (d.containsKey(TiC.PROPERTY_SWIPEABLE)) { this.swipeable = d.getBoolean(TiC.PROPERTY_SWIPEABLE); } + if (d.containsKey(TiC.PROPERTY_AUTO_TAB_TITLE)) { + this.autoTabTitle = d.getBoolean(TiC.PROPERTY_AUTO_TAB_TITLE); + } if (d.containsKey(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK)) { this.smoothScrollOnTabClick = d.getBoolean(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK); } @@ -488,6 +499,8 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP this.swipeable = TiConvert.toBoolean(newValue); } else if (key.equals(TiC.PROPERTY_SMOOTH_SCROLL_ON_TAB_CLICK)) { this.smoothScrollOnTabClick = TiConvert.toBoolean(newValue); + } else if (key.equals(TiC.PROPERTY_AUTO_TAB_TITLE)) { + this.autoTabTitle = TiConvert.toBoolean(newValue); } else if (key.equals(TiC.PROPERTY_TABS_BACKGROUND_COLOR)) { for (TiUITab tabView : tabs) { updateTabBackgroundDrawable(tabs.indexOf(tabView)); diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tabgroup/TiUITab.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tabgroup/TiUITab.java index 3add6c0fcac..66f8d4297f1 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tabgroup/TiUITab.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/tabgroup/TiUITab.java @@ -53,7 +53,7 @@ public View getContentView() return windowProxy.getOrCreateView().getOuterView(); } - private TiWindowProxy getWindowProxy() + public TiWindowProxy getWindowProxy() { if (proxy != null) { Object windowProxy = proxy.getProperty(TiC.PROPERTY_WINDOW); diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiC.java b/android/titanium/src/java/org/appcelerator/titanium/TiC.java index c0b16b8ecb4..f6665c5c474 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/TiC.java +++ b/android/titanium/src/java/org/appcelerator/titanium/TiC.java @@ -268,6 +268,11 @@ public class TiC public static final String PROPERTY_AUTOROTATE = "autorotate"; public static final String PROPERTY_AUTO_REDIRECT = "autoRedirect"; public static final String PROPERTY_AUTO_ENCODE_URL = "autoEncodeUrl"; + public static final String PROPERTY_AUTO_TAB_TITLE = "autoTabTitle"; + + /** + * @module.api + */ public static final String PROPERTY_BACKGROUND_COLOR = "backgroundColor"; public static final String PROPERTY_BACKGROUND_DISABLED_COLOR = "backgroundDisabledColor"; public static final String PROPERTY_BACKGROUND_DISABLED_IMAGE = "backgroundDisabledImage"; diff --git a/apidoc/Titanium/UI/TabGroup.yml b/apidoc/Titanium/UI/TabGroup.yml index 007ee903d7b..35e024b168b 100644 --- a/apidoc/Titanium/UI/TabGroup.yml +++ b/apidoc/Titanium/UI/TabGroup.yml @@ -324,6 +324,13 @@ properties: default: true platforms: [iphone, ipad, macos] + - name: autoTabTitle + summary: If set to `true` it will automatically set the actionbar title to the current tabs window title. + type: Boolean + default: false + since: 10.2.0 + platforms: [android] + - name: barColor summary: | Default navigation bar color (typically for the **More** tab), as a color name or hex triplet.