Skip to content

Commit 74b5d1c

Browse files
committed
Checkbox style option & checkbox padding
1 parent fb42b5a commit 74b5d1c

File tree

1 file changed

+103
-10
lines changed

1 file changed

+103
-10
lines changed

checkbox.android.ts

Lines changed: 103 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
import { CheckBoxInterface } from "./";
2-
import { View } from "ui/core/view";
3-
import { Color } from "color";
4-
import { isAndroid, device } from "platform";
5-
import { Property, PropertyChangeData } from "ui/core/dependency-observable";
6-
import { PropertyMetadata } from "ui/core/proxy";
7-
import { Font } from "ui/styling/font";
1+
import {CheckBoxInterface} from "./";
2+
import {View} from "ui/core/view";
3+
import {Color} from "color";
4+
import {isAndroid, device} from "platform";
5+
import {Property, PropertyChangeData} from "ui/core/dependency-observable";
6+
import {PropertyMetadata} from "ui/core/proxy";
7+
import {Font} from "ui/styling/font";
88
import enums = require("ui/enums");
99
import style = require("ui/styling/style");
10-
10+
import app = require("application");
1111
declare var android: any;
1212

1313
export class CheckBox extends View implements CheckBoxInterface {
1414
private _android: any; /// android.widget.CheckBox
1515
private _fillColor: string;
16-
16+
private _checkStyle: string;
17+
private _checkPadding: string;
18+
private _checkPaddingLeft: string;
19+
private _checkPaddingTop: string;
20+
private _checkPaddingRight: string;
21+
private _checkPaddingBottom: string;
1722
public static checkedProperty = new Property(
1823
"checked",
1924
"CheckBox",
@@ -38,6 +43,55 @@ export class CheckBox extends View implements CheckBoxInterface {
3843
return this._android;
3944
}
4045

46+
get checkStyle() {
47+
return this._checkStyle;
48+
}
49+
50+
set checkStyle(style) {
51+
this._checkStyle = style;
52+
}
53+
54+
set checkPadding(padding){
55+
this._checkPadding = padding;
56+
}
57+
58+
get checkPadding(){
59+
return this._checkPadding;
60+
}
61+
62+
set checkPaddingLeft(padding){
63+
this._checkPaddingLeft = padding;
64+
}
65+
66+
get checkPaddingLeft(){
67+
return this._checkPaddingLeft;
68+
}
69+
70+
71+
set checkPaddingTop(padding){
72+
this._checkPaddingTop = padding;
73+
}
74+
75+
get checkPaddingTop(){
76+
return this._checkPaddingTop;
77+
}
78+
79+
set checkPaddingRight(padding){
80+
this._checkPaddingRight = padding;
81+
}
82+
83+
get checkPaddingRight(){
84+
return this._checkPaddingRight;
85+
}
86+
87+
set checkPaddingBottom(padding){
88+
this._checkPaddingBottom = padding;
89+
}
90+
91+
get checkPaddingBottom(){
92+
return this._checkPaddingBottom;
93+
}
94+
4195
get checked(): boolean {
4296
return this._getValue(CheckBox.checkedProperty);
4397
}
@@ -49,6 +103,7 @@ export class CheckBox extends View implements CheckBoxInterface {
49103
get text(): string {
50104
return this._getValue(CheckBox.textProperty);
51105
}
106+
52107
set text(value: string) {
53108
this._setValue(CheckBox.textProperty, value);
54109
}
@@ -78,13 +133,51 @@ export class CheckBox extends View implements CheckBoxInterface {
78133

79134
this._android = new android.widget.CheckBox(this._context, null);
80135

136+
if(this.checkPaddingLeft){
137+
this._android.setPadding(this.checkPaddingLeft,this._android.getPaddingTop(),this._android.getPaddingRight(),this._android.getPaddingBottom());
138+
}
139+
140+
if(this.checkPaddingTop){
141+
this._android.setPadding(this._android.getPaddingLeft(),this.checkPaddingTop,this._android.getPaddingRight(),this._android.getPaddingBottom());
142+
}
143+
144+
if(this.checkPaddingRight){
145+
this._android.setPadding(this._android.getPaddingLeft(),this._android.getPaddingTop(),this.checkPaddingRight,this._android.getPaddingBottom());
146+
}
147+
148+
if(this.checkPaddingBottom){
149+
this._android.setPadding(this._android.getPaddingLeft(),this._android.getPaddingTop(),this._android.getPaddingRight(),this.checkPaddingBottom);
150+
}
151+
152+
if(this.checkPadding){
153+
let pads = this.checkPadding.toString().split(',');
154+
switch(pads.length){
155+
case 1:
156+
this._android.setPadding(parseInt(pads[0]),parseInt(pads[0]),parseInt(pads[0]),parseInt(pads[0]));
157+
break;
158+
case 2:
159+
this._android.setPadding(parseInt(pads[0]),parseInt(pads[1]),parseInt(pads[0]),parseInt(pads[1]));
160+
break;
161+
case 3:
162+
this._android.setPadding(parseInt(pads[0]),parseInt(pads[1]),parseInt(pads[2]),parseInt(pads[1]));
163+
break;
164+
case 4:
165+
this._android.setPadding(parseInt(pads[0]),parseInt(pads[1]),parseInt(pads[2]),parseInt(pads[3]));
166+
break;
167+
}
168+
}
81169
if (this.text) {
82170
this._android.setText(this.text);
83171
}
84172
if (!this.style.fontSize) {
85173
this.style.fontSize = 15;
86174
}
87175

176+
if (this._checkStyle) {
177+
const drawable = app.android.context.getResources().getIdentifier(this._checkStyle, "drawable", app.android.context.getPackageName());
178+
this._android.setButtonDrawable(drawable);
179+
}
180+
88181

89182
if (this._android) {
90183
if (this.fillColor && device.sdkVersion >= "21") {
@@ -142,7 +235,6 @@ function onTextPropertyChanged(data: PropertyChangeData) {
142235
(<PropertyMetadata>CheckBox.textProperty.metadata).onSetNativeValue = onTextPropertyChanged;
143236

144237

145-
146238
export class CheckBoxStyler implements style.Styler {
147239
private static setColorLabelProperty(view: any, newValue: any) {
148240
var cb = <android.widget.CheckBox>view._nativeView;
@@ -187,6 +279,7 @@ export class CheckBoxStyler implements style.Styler {
187279
size: tv.getTextSize()
188280
};
189281
}
282+
190283
private static resetColorProperty(view: View, nativeValue: number) {
191284
// Do nothing.
192285
}

0 commit comments

Comments
 (0)