From 0d9831519cee0ff50d220a3dcb79d3ced5b2e3c2 Mon Sep 17 00:00:00 2001 From: Rossen Hristov Date: Wed, 29 Apr 2015 09:21:04 +0300 Subject: [PATCH] Fixed: TabViewItem does not get the right resource image #91 --- ui/tab-view/tab-view.android.ts | 39 +++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/ui/tab-view/tab-view.android.ts b/ui/tab-view/tab-view.android.ts index 17aa0311c..0d37598c7 100644 --- a/ui/tab-view/tab-view.android.ts +++ b/ui/tab-view/tab-view.android.ts @@ -5,8 +5,10 @@ import view = require("ui/core/view"); import trace = require("trace"); import imageSource = require("image-source"); import types = require("utils/types"); +import app = require("application"); var VIEWS_STATES = "_viewStates"; +var RESOURCE_PREFIX = "res://"; // merge the exports of the common file with the exports of this file declare var exports; @@ -357,12 +359,13 @@ export class TabView extends common.TabView { var length = newItems.length; var item: definition.TabViewItem; var tab: android.app.ActionBar.Tab; - + var androidApp = app.android; + var resources = androidApp.context.getResources(); for (i; i < length; i++) { item = newItems[i]; tab = actionBar.newTab(); tab.setText(item.title); - this._setIcon(item.iconSource, tab); + this._setIcon(item.iconSource, tab, resources, androidApp.packageName); tab.setTabListener(this._tabListener); @@ -372,23 +375,31 @@ export class TabView extends common.TabView { } } - private _setIcon(iconSource: string, tab: android.app.ActionBar.Tab): void { + private _setIcon(iconSource: string, tab: android.app.ActionBar.Tab, resources: android.content.res.Resources, packageName: string): void { if (!iconSource) { return; } - - var drawable: android.graphics.drawable.BitmapDrawable; - drawable = this._iconsCache[iconSource]; - if (!drawable) { - var is = imageSource.fromFileOrResource(iconSource); - if (is) { - drawable = new android.graphics.drawable.BitmapDrawable(is.android); - this._iconsCache[iconSource] = drawable; + + if (iconSource.indexOf(RESOURCE_PREFIX) === 0 && resources) { + var resourceId: number = resources.getIdentifier(iconSource.substr(RESOURCE_PREFIX.length), 'drawable', packageName); + if (resourceId > 0) { + tab.setIcon(resourceId); } } - - if (drawable) { - tab.setIcon(drawable); + else { + var drawable: android.graphics.drawable.BitmapDrawable; + drawable = this._iconsCache[iconSource]; + if (!drawable) { + var is = imageSource.fromFileOrResource(iconSource); + if (is) { + drawable = new android.graphics.drawable.BitmapDrawable(is.android); + this._iconsCache[iconSource] = drawable; + } + } + + if (drawable) { + tab.setIcon(drawable); + } } }