Skip to content

Commit 0387a46

Browse files
author
farfromrefug
committed
fix(android): more views created using inflator allowing to customize the theme
1 parent fd8aeba commit 0387a46

File tree

12 files changed

+70
-69
lines changed

12 files changed

+70
-69
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.widget.ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
3+
style="?android:attr/progressBarStyle"
4+
android:layout_width="wrap_content"
5+
android:layout_height="wrap_content"
6+
android:visibility="invisible"
7+
android:indeterminate="true">
8+
</android.widget.ProgressBar>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
<style name="CircularProgress" parent="@android:style/Widget.Material.ProgressBar">
5+
</style>
6+
</resources>

packages/core/platforms/android/java/com/nativescript/material/core/Utils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.animation.StateListAnimator;
44
import android.animation.AnimatorSet;
55
import android.animation.ObjectAnimator;
6+
import android.view.LayoutInflater;
67
import android.view.View;
78
import android.view.ViewGroup;
89
import android.content.Context;
@@ -19,6 +20,7 @@ public class Utils {
1920
static final int shortAnimTime = android.R.integer.config_shortAnimTime;
2021
static final int statePressed = android.R.attr.state_pressed;
2122
static final int stateEnabled = android.R.attr.state_enabled;
23+
static LayoutInflater inflater = null;
2224

2325
public static void createStateListAnimator(Context context, View view, float elevation, float pressedZ) {
2426
int duration = context.getResources().getInteger(shortAnimTime);
@@ -102,4 +104,12 @@ public static void handleClearFocus(View view) {
102104
}
103105
}
104106
}
107+
108+
public static View inflateLayout(Context context, String layoutStringId) {
109+
int layoutId = context.getResources().getIdentifier(layoutStringId, "layout", context.getPackageName());
110+
if (inflater == null) {
111+
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
112+
}
113+
return (View)inflater.inflate(layoutId, null);
114+
}
105115
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.google.android.material.progressindicator.LinearProgressIndicator xmlns:android="http://schemas.android.com/apk/res/android"
3+
style="@style/LinearProgress">
4+
</com.google.android.material.progressindicator.LinearProgressIndicator>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
<style name="LinearProgress" parent="@style/Widget.MaterialComponents.LinearProgressIndicator">
5+
</style>
6+
</resources>

src/activityindicator/index.android.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
import { heightProperty } from '@nativescript/core';
12
import { ActivityIndicatorBase, indeterminateProperty, maxValueProperty, valueProperty } from './index-common';
3+
import { inflateLayout } from '@nativescript-community/ui-material-core/android/utils';
24

35
export class ActivityIndicator extends ActivityIndicatorBase {
4-
// nativeViewProtected: com.google.android.material.progressindicator.CircularProgressIndicator;
6+
nativeViewProtected: com.google.android.material.progressindicator.CircularProgressIndicator;
57

68
// createNativeView() {
79
// const progressBar = new com.google.android.material.progressindicator.CircularProgressIndicator(this._context);
810
// progressBar.setVisibility(android.view.View.INVISIBLE);
911
// progressBar.setIndeterminate(true);
1012
// return progressBar;
1113
// }
14+
createNativeView() {
15+
return inflateLayout(this._context, 'ns_material_circular_progress') as com.google.android.material.progressindicator.CircularProgressIndicator;
16+
}
1217

1318
[valueProperty.getDefault](): number {
1419
return 0;

src/button/button.android.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { VerticalTextAlignment, verticalTextAlignmentProperty } from '@nativescript-community/text';
22
import { dynamicElevationOffsetProperty, elevationProperty, getRippleColor, rippleColorAlphaProperty, rippleColorProperty, shapeProperty, themer } from '@nativescript-community/ui-material-core';
3-
import { createStateListAnimator, getColorStateList, getHorizontalGravity, getLayout, getVerticalGravity, isPostLollipop } from '@nativescript-community/ui-material-core/android/utils';
3+
import { createStateListAnimator, getColorStateList, getHorizontalGravity, getVerticalGravity, inflateLayout, isPostLollipop } from '@nativescript-community/ui-material-core/android/utils';
44
import {
55
Background,
66
Color,
77
CoreTypes,
8-
Font,
98
ImageSource,
109
Length,
1110
Utils,
@@ -18,10 +17,6 @@ import {
1817
import { textAlignmentProperty, textTransformProperty } from '@nativescript/core/ui/text-base';
1918
import { ButtonBase, imageSourceProperty, srcProperty } from './button-common';
2019

21-
let LayoutInflater: typeof android.view.LayoutInflater;
22-
23-
const layoutIds = {};
24-
2520
export class Button extends ButtonBase {
2621
nativeViewProtected: com.google.android.material.button.MaterialButton;
2722
nativeTextViewProtected: com.google.android.material.button.MaterialButton;
@@ -30,7 +25,7 @@ export class Button extends ButtonBase {
3025

3126
@profile
3227
public createNativeView() {
33-
let layoutId;
28+
// let layoutId;
3429
const variant = this.variant;
3530
// let layoutIdName = 'ns_material_button';
3631
let layoutStringId: string;
@@ -52,16 +47,8 @@ export class Button extends ButtonBase {
5247
if (this.src) {
5348
layoutStringId += '_icon';
5449
}
55-
layoutId = layoutIds[layoutStringId];
56-
if (!layoutId) {
57-
layoutId = layoutIds[layoutStringId] = getLayout(this._context, layoutStringId);
58-
}
59-
if (!LayoutInflater) {
60-
LayoutInflater = android.view.LayoutInflater;
61-
}
62-
const view = LayoutInflater.from(this._context).inflate(layoutId, null, false) as com.google.android.material.button.MaterialButton;
50+
const view = inflateLayout(this._context, layoutStringId) as com.google.android.material.button.MaterialButton;
6351
if (this.src) {
64-
layoutStringId += '_icon';
6552
view.setIconGravity(0x2); //com.google.android.material.button.MaterialButton.ICON_GRAVITY_TEXT_START
6653
// view.setIconSize(Utils.layout.toDevicePixels(24));
6754
}

src/core/android/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,10 @@ export function getVerticalGravity(textAlignment: VerticalTextAlignment) {
256256
return 80; //Gravity.BOTTOM
257257
}
258258
}
259+
260+
export function inflateLayout(context: android.content.Context, layoutId: string) {
261+
if (!NUtils) {
262+
NUtils = (com as any).nativescript.material.core.Utils;
263+
}
264+
return NUtils.inflateLayout(context, layoutId);
265+
}

src/core/index.android.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Background, Button, Color, Length, PercentLength, Utils, View, androidDynamicElevationOffsetProperty, androidElevationProperty, backgroundInternalProperty } from '@nativescript/core';
2-
import { ad } from '@nativescript/core/utils';
32
import { ShapeProperties } from '.';
43
import { createRippleDrawable, createStateListAnimator, getAttrColor, getColorStateList, handleClearFocus, isPostLollipop, isPostMarshmallow } from './android/utils';
54
import { cssProperty, dynamicElevationOffsetProperty, elevationProperty, rippleColorAlphaProperty, rippleColorProperty } from './cssproperties';
@@ -20,7 +19,7 @@ function cornerTreat(cornerFamily: CornerFamily): com.google.android.material.sh
2019
let context: android.content.Context;
2120
function getContext() {
2221
if (!context) {
23-
context = Utils.ad.getApplicationContext();
22+
context = Utils.android.getApplicationContext();
2423
}
2524
return context;
2625
}
@@ -299,7 +298,7 @@ export function overrideViewBase() {
299298
this.focus();
300299
}
301300
public clearFocus() {
302-
ad.dismissSoftInput(this.nativeViewProtected);
301+
Utils.android.dismissSoftInput(this.nativeViewProtected);
303302
handleClearFocus(this.nativeViewProtected);
304303
}
305304

src/progress/progress.android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Color, PercentLength, backgroundColorProperty, backgroundInternalProperty, colorProperty, heightProperty } from '@nativescript/core';
22
import { ProgressBase, busyProperty, indeterminateProperty, progressBackgroundColorProperty, progressColorProperty } from './progress-common';
33
import { getRippleColor } from '@nativescript-community/ui-material-core';
4+
import { inflateLayout } from '@nativescript-community/ui-material-core/android/utils';
45

56
export class Progress extends ProgressBase {
67
nativeViewProtected: com.google.android.material.progressindicator.LinearProgressIndicator;
78

8-
// added in 1.3.0
99
createNativeView() {
10-
return new com.google.android.material.progressindicator.LinearProgressIndicator(this._context);
10+
return inflateLayout(this._context, 'ns_material_linear_progress') as com.google.android.material.progressindicator.LinearProgressIndicator;
1111
}
1212

1313
[progressColorProperty.setNative](color: Color) {

0 commit comments

Comments
 (0)