Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
TiC.PROPERTY_SHADOW_OFFSET,
TiC.PROPERTY_SHADOW_COLOR,
TiC.PROPERTY_SHADOW_RADIUS,
TiC.PROPERTY_TINT_COLOR
TiC.PROPERTY_TINT_COLOR,
TiC.PROPERTY_PADDING
})
public class ButtonProxy extends TiViewProxy
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
Expand Down Expand Up @@ -141,14 +142,15 @@ public void processProperties(KrollDict d)
super.processProperties(d);

boolean needShadow = false;

Activity activity = proxy.getActivity();
AppCompatButton btn = (AppCompatButton) getNativeView();
if (!d.containsKey(TiC.PROPERTY_IMAGE) && d.containsKey(TiC.PROPERTY_BACKGROUND_COLOR)) {
// Reset the padding here if the background color is set. By default the padding will be calculated
// for the button, but if we set a background color, it will not look centered unless we reset the padding.
btn.setPadding(8, 0, 8, 0);

if (d.containsKey(TiC.PROPERTY_PADDING) || (!d.containsKey(TiC.PROPERTY_IMAGE)
&& d.containsKey(TiC.PROPERTY_BACKGROUND_COLOR))) {
HashMap padding = (HashMap) d.get(TiC.PROPERTY_PADDING);
setPadding(padding);
}

if ((btn instanceof MaterialButton) && d.containsKey(TiC.PROPERTY_TOUCH_FEEDBACK)) {
// We only override MaterialButton's native ripple effect if "touchFeedback" property is defined.
ColorStateList colorStateList = null;
Expand Down Expand Up @@ -213,6 +215,35 @@ public void processProperties(KrollDict d)
btn.invalidate();
}

private void setPadding(HashMap padding)
{
int paddingLeft = nativeView.getPaddingLeft();
int paddingTop = nativeView.getPaddingTop();
int paddingRight = nativeView.getPaddingRight();
int paddingBottom = nativeView.getPaddingBottom();
Activity activity = proxy.getActivity();
AppCompatButton btn = (AppCompatButton) getNativeView();

if (padding.containsKey(TiC.PROPERTY_LEFT)) {
paddingLeft = TiConvert.toTiDimension(TiConvert.toInt(padding.get(TiC.PROPERTY_LEFT), 0),
TiDimension.TYPE_LEFT).getAsPixels(nativeView);
}
if (padding.containsKey(TiC.PROPERTY_RIGHT)) {
paddingRight = TiConvert.toTiDimension(TiConvert.toInt(padding.get(TiC.PROPERTY_RIGHT), 0),
TiDimension.TYPE_RIGHT).getAsPixels(nativeView);
}
if (padding.containsKey(TiC.PROPERTY_TOP)) {
paddingTop = TiConvert.toTiDimension(TiConvert.toInt(padding.get(TiC.PROPERTY_TOP), 0),
TiDimension.TYPE_TOP).getAsPixels(nativeView);
}
if (padding.containsKey(TiC.PROPERTY_BOTTOM)) {
paddingBottom = TiConvert.toTiDimension(TiConvert.toInt(padding.get(TiC.PROPERTY_BOTTOM), 0),
TiDimension.TYPE_BOTTOM).getAsPixels(nativeView);
}

btn.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}

@Override
public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy)
{
Expand Down Expand Up @@ -274,6 +305,8 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
} else if (key.equals(TiC.PROPERTY_SHADOW_COLOR)) {
shadowColor = TiConvert.toColor(TiConvert.toString(newValue), activity);
btn.setShadowLayer(shadowRadius, shadowX, shadowY, shadowColor);
} else if (key.equals(TiC.PROPERTY_PADDING)) {
setPadding((HashMap) newValue);
} else {
super.propertyChanged(key, oldValue, newValue, proxy);
}
Expand Down
31 changes: 30 additions & 1 deletion apidoc/Titanium/UI/Button.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ properties:
default: true
since: "11.0.0"

- name: padding
summary: Sets the padding of this Button.
type: ButtonPadding
platforms: [android]
since: {android: 12.7.0}

- name: selectedColor
summary: Button text color used to indicate the selected state, as a color name or hex triplet.
description: |
Expand Down Expand Up @@ -356,7 +362,7 @@ properties:
description: Only one of `title` or `titleid` should be specified.
type: String
since: "1.5"

- name: tooltip
summary: The default text to display in the control's tooltip.
description: |
Expand Down Expand Up @@ -416,3 +422,26 @@ examples:
Titanium.API.info("You clicked the button");
};
```

---
name: ButtonPadding
extends: Padding
summary: Dictionary object of parameters for the <Titanium.UI.Button.padding> that describes the padding.
since: "12.7.0"
platforms: [android]
properties:
- name: left
type: Number
summary: Left padding

- name: right
type: Number
summary: Right padding

- name: top
type: Number
summary: Top padding

- name: bottom
type: Number
summary: Bottom padding
Loading