Skip to content

Commit

Permalink
Merge pull request #2171 from ayeung/timob-9055
Browse files Browse the repository at this point in the history
TIMOB-9055: Android: The text in a button is not centered, it's slightly...
  • Loading branch information
billdawson committed May 11, 2012
2 parents 4419e95 + f340274 commit 1c4cac5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand All @@ -15,7 +15,6 @@
import org.appcelerator.kroll.common.TiConfig;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.proxy.TiViewProxy;
Expand All @@ -40,7 +39,7 @@ public TiUIButton(final TiViewProxy proxy) {
Log.d(LCAT, "Creating a button");
}
Button btn = new Button(proxy.getActivity());
btn.setPadding(8, 2, 8, 8);
btn.setPadding(8, 0, 8, 0);
btn.setGravity(Gravity.CENTER);
setNativeView(btn);
}
Expand All @@ -54,7 +53,6 @@ public void processProperties(KrollDict d)
if (d.containsKey(TiC.PROPERTY_IMAGE)) {
Object value = d.get(TiC.PROPERTY_IMAGE);
Bitmap bitmap = null;
BitmapDrawable image;
if (value instanceof String) {
try {
String url = getProxy().resolveUrl(null, (String) value);
Expand All @@ -68,47 +66,7 @@ public void processProperties(KrollDict d)
}

if (bitmap != null) {
image = new BitmapDrawable(btn.getResources(), bitmap);

TiDimension optionH = layoutParams.optionHeight;
TiDimension optionW = layoutParams.optionWidth;
int buttonHeight;
int buttonWidth;
int paddingTop = btn.getPaddingTop();
int paddingBottom = btn.getPaddingBottom();
int paddingLeft = btn.getPaddingLeft();
int paddingRight = btn.getPaddingRight();
int imgIntrisicHeight = image.getIntrinsicHeight();
int imgIntrisicWidth = image.getIntrinsicWidth();

if (optionH == null && optionW != null) {
buttonWidth = optionW.getIntValue() - paddingLeft - paddingRight;
if (imgIntrisicWidth > buttonWidth) {
bitmap = Bitmap.createScaledBitmap(bitmap, buttonWidth, imgIntrisicHeight * buttonWidth
/ imgIntrisicWidth, true);
image = new BitmapDrawable(btn.getResources(), bitmap);
}
} else if (optionH != null && optionW == null) {
buttonHeight = optionH.getIntValue() - paddingTop - paddingBottom;
if (imgIntrisicHeight > buttonHeight) {
bitmap = Bitmap.createScaledBitmap(bitmap, imgIntrisicWidth * buttonHeight / imgIntrisicHeight,
buttonHeight, true);
image = new BitmapDrawable(btn.getResources(), bitmap);
}
} else if (optionH != null && optionW != null) {
buttonHeight = optionH.getIntValue() - paddingTop - paddingBottom;
buttonWidth = optionW.getIntValue() - paddingLeft - paddingRight;
if (imgIntrisicWidth > buttonWidth || imgIntrisicHeight > buttonHeight) {
bitmap = Bitmap.createScaledBitmap(
bitmap,
Math.min(Math.min(imgIntrisicWidth, buttonWidth), imgIntrisicWidth * buttonHeight
/ imgIntrisicHeight),
Math.min(Math.min(imgIntrisicHeight, buttonHeight), imgIntrisicHeight * buttonWidth
/ imgIntrisicWidth), true);
image = new BitmapDrawable(btn.getResources(), bitmap);
}
}

BitmapDrawable image = new BitmapDrawable(btn.getResources(), bitmap);
btn.setCompoundDrawablesWithIntrinsicBounds(image, null, null, null);
}
}
Expand Down
4 changes: 1 addition & 3 deletions apidoc/Titanium/UI/Button.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ properties:
summary: Image to display on the button, specified as a local
path, URL or (on iOS only) a `Blob`.
description: |
On iOS and Mobile Web, the image is displayed to the left of the title.
On Android, this property is identical to `backgroundImage`.
The image is displayed to the left of the title.
Support for using <Titanium.Blob> for this property is only available on iOS.
type: [String, Titanium.Blob]
Expand Down

0 comments on commit 1c4cac5

Please sign in to comment.