@@ -12,15 +12,15 @@ const R_ATTR_STATE_SELECTED = 0x010100a1;
1212const TITLE_TEXT_VIEW_ID = 16908310 ; // http://developer.android.com/reference/android/R.id.html#title
1313
1414interface TabChangeListener {
15- new ( owner : SegmentedBar ) : android . widget . TabHost . OnTabChangeListener ;
15+ new ( owner : SegmentedBar ) : android . widget . TabHost . OnTabChangeListener ;
1616}
1717
1818interface TabContentFactory {
19- new ( owner : SegmentedBar ) : android . widget . TabHost . TabContentFactory ;
19+ new ( owner : SegmentedBar ) : android . widget . TabHost . TabContentFactory ;
2020}
2121
2222interface TabHost {
23- new ( context : android . content . Context , attrs : android . util . AttributeSet ) : android . widget . TabHost ;
23+ new ( context : android . content . Context , attrs : android . util . AttributeSet ) : android . widget . TabHost ;
2424}
2525
2626let apiLevel : number ;
@@ -144,17 +144,18 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
144144 this . nativeViewProtected . setTypeface ( value instanceof Font ? value . getAndroidTypeface ( ) : value ) ;
145145 }
146146
147- [ selectedBackgroundColorProperty . getDefault ] ( ) : android . graphics . drawable . Drawable . ConstantState {
147+ [ selectedBackgroundColorProperty . getDefault ] ( ) : android . graphics . drawable . Drawable {
148148 const viewGroup = < android . view . ViewGroup > this . nativeViewProtected . getParent ( ) ;
149- return viewGroup . getBackground ( ) . getConstantState ( ) ;
149+ return viewGroup . getBackground ( ) ;
150150 }
151- [ selectedBackgroundColorProperty . setNative ] ( value : Color | android . graphics . drawable . Drawable . ConstantState ) {
152- const viewGroup = < android . view . ViewGroup > this . nativeViewProtected . getParent ( ) ;
151+ [ selectedBackgroundColorProperty . setNative ] ( value : Color | android . graphics . drawable . Drawable ) {
152+ const nativeView = this . nativeViewProtected ;
153+ const viewGroup = < android . view . ViewGroup > nativeView . getParent ( ) ;
153154 if ( value instanceof Color ) {
154155 const color = value . android ;
155156 const backgroundDrawable = viewGroup . getBackground ( ) ;
156- if ( apiLevel > 21 && backgroundDrawable && typeof backgroundDrawable . setColorFilter === "function" ) {
157- const newDrawable = backgroundDrawable . getConstantState ( ) . newDrawable ( ) ;
157+ if ( apiLevel > 21 && backgroundDrawable ) {
158+ const newDrawable = tryCloneDrawable ( backgroundDrawable , nativeView . getResources ( ) ) ;
158159 newDrawable . setColorFilter ( color , android . graphics . PorterDuff . Mode . SRC_IN ) ;
159160 org . nativescript . widgets . ViewHelper . setBackground ( viewGroup , newDrawable ) ;
160161 } else {
@@ -164,15 +165,26 @@ export class SegmentedBarItem extends SegmentedBarItemBase {
164165 arr [ 0 ] = R_ATTR_STATE_SELECTED ;
165166 stateDrawable . addState ( arr , colorDrawable ) ;
166167 stateDrawable . setBounds ( 0 , 15 , viewGroup . getRight ( ) , viewGroup . getBottom ( ) ) ;
167-
168168 org . nativescript . widgets . ViewHelper . setBackground ( viewGroup , stateDrawable ) ;
169169 }
170170 } else {
171- org . nativescript . widgets . ViewHelper . setBackground ( viewGroup , value . newDrawable ( ) ) ;
171+ const backgroundDrawable = tryCloneDrawable ( value , nativeView . getResources ( ) ) ;
172+ org . nativescript . widgets . ViewHelper . setBackground ( viewGroup , backgroundDrawable ) ;
172173 }
173174 }
174175}
175176
177+ function tryCloneDrawable ( value : android . graphics . drawable . Drawable , resources : android . content . res . Resources ) : android . graphics . drawable . Drawable {
178+ if ( value ) {
179+ const constantState = value . getConstantState ( ) ;
180+ if ( constantState ) {
181+ return constantState . newDrawable ( resources ) ;
182+ }
183+ }
184+
185+ return value ;
186+ }
187+
176188export class SegmentedBar extends SegmentedBarBase {
177189 nativeViewProtected : android . widget . TabHost ;
178190 private _tabContentFactory : android . widget . TabHost . TabContentFactory ;
0 commit comments