Skip to content

Commit 08f96c4

Browse files
committed
fix(android): elevation on pre lollipop
1 parent b88513a commit 08f96c4

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

src/button/button.android.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createStateListAnimator, getEnabledColorStateList, getLayout, isPostLol
22
import { dynamicElevationOffsetProperty, elevationProperty, rippleColorProperty, verticalTextAlignmentProperty } from 'nativescript-material-core/cssproperties';
33
import { Color } from '@nativescript/core/color';
44
import { Background } from '@nativescript/core/ui/styling/background';
5+
import { TextBase } from '@nativescript/core/ui/text-base';
56
import { androidDynamicElevationOffsetProperty, androidElevationProperty, backgroundInternalProperty, Length } from '@nativescript/core/ui/styling/style-properties';
67
import { ButtonBase } from './button-common';
78
import { VerticalTextAlignment } from 'nativescript-material-core';
@@ -13,6 +14,7 @@ let textId;
1314
let containedId;
1415
let flatId;
1516
let grayColorStateList: android.content.res.ColorStateList;
17+
1618
export class Button extends ButtonBase {
1719
nativeViewProtected: com.google.android.material.button.MaterialButton;
1820

@@ -21,7 +23,7 @@ export class Button extends ButtonBase {
2123
get android(): com.google.android.material.button.MaterialButton {
2224
return this.nativeView;
2325
}
24-
26+
2527
@profile
2628
public createNativeView() {
2729
let layoutId;
@@ -64,11 +66,25 @@ export class Button extends ButtonBase {
6466
}
6567
return view;
6668
}
69+
70+
// initNativeView() {
71+
// TextBase.prototype.initNativeView.call(this);
72+
// const nativeView = this.nativeViewProtected;
73+
// const clickListener = new android.view.View.OnClickListener({
74+
// onClick:()=>{
75+
// console.log('overriden button onClick');
76+
// this._emit(ButtonBase.tapEvent);
77+
// }
78+
// });
79+
// nativeView.setOnClickListener(clickListener);
80+
// (<any>nativeView).clickListener = clickListener;
81+
// }
6782
[rippleColorProperty.setNative](color: Color) {
6883
this.nativeViewProtected.setRippleColor(android.content.res.ColorStateList.valueOf(color.android));
6984
}
7085

7186
getDefaultElevation(): number {
87+
console.log('getDefaultElevation', 2, new Error().stack);
7288
return 2; // 2dp @dimen/mtrl_btn_elevation
7389
}
7490

@@ -80,14 +96,16 @@ export class Button extends ButtonBase {
8096
if (isPostLollipop()) {
8197
createStateListAnimator(this, this.nativeViewProtected);
8298
} else {
83-
this.nativeViewProtected.setElevation(value);
99+
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
100+
this.nativeViewProtected.setElevation(newValue);
84101
}
85102
}
86103
[dynamicElevationOffsetProperty.setNative](value: number) {
87104
if (isPostLollipop()) {
88105
createStateListAnimator(this, this.nativeViewProtected);
89106
} else {
90-
this.nativeViewProtected.setTranslationZ(value);
107+
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
108+
this.nativeViewProtected.setTranslationZ(newValue);
91109
}
92110
}
93111
[androidElevationProperty.setNative](value: number) {
@@ -98,12 +116,12 @@ export class Button extends ButtonBase {
98116
}
99117

100118
setCornerRadius(value) {
101-
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
102-
this.nativeViewProtected.setCornerRadius(newValue);
119+
// const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
120+
this.nativeViewProtected.setCornerRadius(value);
103121
}
104122
setStrokeWidth(value) {
105-
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
106-
this.nativeViewProtected.setStrokeWidth(newValue);
123+
// const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
124+
this.nativeViewProtected.setStrokeWidth(value);
107125
}
108126
[backgroundInternalProperty.setNative](value: android.graphics.drawable.Drawable | Background) {
109127
const view = this.nativeTextViewProtected;

src/cardview/cardview.android.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createStateListAnimator, getAttrColor, isPostLollipop } from 'nativescript-material-core/android/utils';
22
import { dynamicElevationOffsetProperty, elevationProperty, rippleColorProperty } from 'nativescript-material-core/cssproperties';
33
import { Color } from '@nativescript/core/color';
4-
import { backgroundInternalProperty } from '@nativescript/core/ui/styling/style-properties';
4+
import { backgroundInternalProperty, Length } from '@nativescript/core/ui/styling/style-properties';
55
import { CardViewBase } from './cardview-common';
66

77
let MDCCardView: typeof com.google.android.material.card.MaterialCardView;
@@ -330,7 +330,8 @@ export class CardView extends CardViewBase {
330330
if (isPostLollipop()) {
331331
createStateListAnimator(this, this.nativeViewProtected);
332332
} else {
333-
androidx.core.view.ViewCompat.setElevation(this.nativeViewProtected, value);
333+
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
334+
androidx.core.view.ViewCompat.setElevation(this.nativeViewProtected, newValue);
334335
}
335336
}
336337
[dynamicElevationOffsetProperty.setNative](value: number) {
@@ -340,7 +341,8 @@ export class CardView extends CardViewBase {
340341
if (isPostLollipop()) {
341342
createStateListAnimator(this, this.nativeViewProtected);
342343
} else {
343-
androidx.core.view.ViewCompat.setTranslationZ(this.nativeViewProtected, value);
344+
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
345+
androidx.core.view.ViewCompat.setTranslationZ(this.nativeViewProtected, newValue);
344346
}
345347
}
346348
[rippleColorProperty.setNative](color: Color) {

src/floatingactionbutton/floatingactionbutton.android.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FloatingActionButtonBase, imageSourceProperty, sizeProperty, srcProperty } from './floatingactionbutton-common';
22
import { ImageSource } from '@nativescript/core/image-source';
33
import { dynamicElevationOffsetProperty, elevationProperty, rippleColorProperty } from 'nativescript-material-core/cssproperties';
4-
import { backgroundInternalProperty, colorProperty } from '@nativescript/core/ui/styling/style-properties';
4+
import { backgroundInternalProperty, colorProperty, Length } from '@nativescript/core/ui/styling/style-properties';
55
import { Background } from '@nativescript/core/ui/styling/background';
66
import { Color } from '@nativescript/core/color';
77

@@ -42,10 +42,12 @@ export class FloatingActionButton extends FloatingActionButtonBase {
4242
}
4343

4444
[elevationProperty.setNative](value: number) {
45-
this.nativeViewProtected.setCompatElevation(value);
45+
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
46+
this.nativeViewProtected.setCompatElevation(newValue);
4647
}
4748

4849
[dynamicElevationOffsetProperty.setNative](value: number) {
50+
const newValue = Length.toDevicePixels(typeof value === 'string' ? Length.parse(value) : value, 0);
4951
this.nativeViewProtected.setTranslationZ(value);
5052
}
5153

0 commit comments

Comments
 (0)