From 2842679d3426295b6674dfcfb8591fad79de6cb2 Mon Sep 17 00:00:00 2001 From: Fabrice Di Meglio Date: Tue, 5 Jun 2012 16:41:18 -0700 Subject: Make CheckBox / RadioButton / ToggleButton / Star widgets aware of layout direction - see bug #5429822 UI should be mirrored for RTL locales (Arabic, Hebrew, farsi) Change-Id: I8badb57d095c6a0a1d0c82bedf30b02502013ae0 --- core/java/android/widget/CompoundButton.java | 39 +++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'core/java/android/widget/CompoundButton.java') diff --git a/core/java/android/widget/CompoundButton.java b/core/java/android/widget/CompoundButton.java index 0a71c5ad5432..421a3247e8c5 100644 --- a/core/java/android/widget/CompoundButton.java +++ b/core/java/android/widget/CompoundButton.java @@ -224,6 +224,30 @@ public abstract class CompoundButton extends Button implements Checkable { info.setChecked(mChecked); } + @Override + public int getCompoundPaddingLeft() { + int padding = super.getCompoundPaddingLeft(); + if (!isLayoutRtl()) { + final Drawable buttonDrawable = mButtonDrawable; + if (buttonDrawable != null) { + padding += buttonDrawable.getIntrinsicWidth(); + } + } + return padding; + } + + @Override + public int getCompoundPaddingRight() { + int padding = super.getCompoundPaddingRight(); + if (isLayoutRtl()) { + final Drawable buttonDrawable = mButtonDrawable; + if (buttonDrawable != null) { + padding += buttonDrawable.getIntrinsicWidth(); + } + } + return padding; + } + @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); @@ -231,20 +255,23 @@ public abstract class CompoundButton extends Button implements Checkable { final Drawable buttonDrawable = mButtonDrawable; if (buttonDrawable != null) { final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; - final int height = buttonDrawable.getIntrinsicHeight(); - - int y = 0; + final int drawableHeight = buttonDrawable.getIntrinsicHeight(); + final int drawableWidth = buttonDrawable.getIntrinsicWidth(); + int top = 0; switch (verticalGravity) { case Gravity.BOTTOM: - y = getHeight() - height; + top = getHeight() - drawableHeight; break; case Gravity.CENTER_VERTICAL: - y = (getHeight() - height) / 2; + top = (getHeight() - drawableHeight) / 2; break; } + int bottom = top + drawableHeight; + int left = isLayoutRtl() ? getWidth() - drawableWidth : 0; + int right = isLayoutRtl() ? getWidth() : drawableWidth; - buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height); + buttonDrawable.setBounds(left, top, right, bottom); buttonDrawable.draw(canvas); } } -- cgit v1.2.3