diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java index 0af9c82ac5d..da19265a5b2 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java @@ -67,6 +67,7 @@ public class TiUIText extends TiUIView implements TextWatcher, OnEditorActionLis private int viewHeightInLines; private int maxLines = Integer.MAX_VALUE; private int hintTextPadding; + private HashMap defaultPadding = new HashMap(); private InputFilterHandler inputFilterHandler; protected TiUIEditText tv; @@ -159,6 +160,11 @@ public boolean onKey(View v, int keyCode, KeyEvent event) textInputLayout.addView(this.tv, new TextInputLayout.LayoutParams( TextInputLayout.LayoutParams.MATCH_PARENT, TextInputLayout.LayoutParams.MATCH_PARENT)); + // store default padding + this.defaultPadding.put(TiC.PROPERTY_TOP, this.tv.getPaddingTop()); + this.defaultPadding.put(TiC.PROPERTY_RIGHT, this.tv.getPaddingRight()); + this.defaultPadding.put(TiC.PROPERTY_BOTTOM, this.tv.getPaddingBottom()); + this.defaultPadding.put(TiC.PROPERTY_LEFT, this.tv.getPaddingLeft()); setNativeView(textInputLayout); } @@ -326,28 +332,39 @@ private void updateTextField() private void setTextPadding(HashMap d) { - int paddingLeft = textInputLayout.getPaddingLeft(); - int paddingRight = textInputLayout.getPaddingRight(); - int paddingTop = textInputLayout.getPaddingTop(); - int paddingBottom = textInputLayout.getPaddingBottom(); - - if (d.containsKey(TiC.PROPERTY_LEFT)) { - paddingLeft = TiConvert.toInt(d.get(TiC.PROPERTY_LEFT), 0); - } + int paddingLeft = tv.getPaddingLeft(); + int paddingRight = tv.getPaddingRight(); + int paddingTop = tv.getPaddingTop(); + int paddingBottom = tv.getPaddingBottom(); + + if (d == null) { + // reset to default padding + paddingLeft = (int) this.defaultPadding.get(TiC.PROPERTY_LEFT); + paddingRight = (int) this.defaultPadding.get(TiC.PROPERTY_RIGHT); + paddingTop = (int) this.defaultPadding.get(TiC.PROPERTY_TOP); + paddingBottom = (int) this.defaultPadding.get(TiC.PROPERTY_BOTTOM); + } else { + if (d.containsKey(TiC.PROPERTY_LEFT)) { + paddingLeft = (int) TiConvert.toTiDimension(TiConvert.toInt(d.get(TiC.PROPERTY_LEFT), 0), + TiDimension.TYPE_LEFT).getAsPixels(textInputLayout); + } - if (d.containsKey(TiC.PROPERTY_RIGHT)) { - paddingRight = TiConvert.toInt(d.get(TiC.PROPERTY_RIGHT), 0); - } + if (d.containsKey(TiC.PROPERTY_RIGHT)) { + paddingRight = (int) TiConvert.toTiDimension(TiConvert.toInt(d.get(TiC.PROPERTY_RIGHT), 0), + TiDimension.TYPE_RIGHT).getAsPixels(textInputLayout); + } - if (d.containsKey(TiC.PROPERTY_TOP)) { - paddingTop = TiConvert.toInt(d.get(TiC.PROPERTY_TOP), 0); - } + if (d.containsKey(TiC.PROPERTY_TOP)) { + paddingTop = (int) TiConvert.toTiDimension(TiConvert.toInt(d.get(TiC.PROPERTY_TOP), 0), + TiDimension.TYPE_TOP).getAsPixels(textInputLayout); + } - if (d.containsKey(TiC.PROPERTY_BOTTOM)) { - paddingBottom = TiConvert.toInt(d.get(TiC.PROPERTY_BOTTOM), 0); + if (d.containsKey(TiC.PROPERTY_BOTTOM)) { + paddingBottom = (int) TiConvert.toTiDimension(TiConvert.toInt(d.get(TiC.PROPERTY_BOTTOM), 0), + TiDimension.TYPE_BOTTOM).getAsPixels(textInputLayout); + } } - - textInputLayout.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); + tv.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); } @Override @@ -984,6 +1001,9 @@ public void setHintText(int type, CharSequence hintText) this.textInputLayout.setHintEnabled(true); } + this.defaultPadding.put(TiC.PROPERTY_TOP, (type == UIModule.HINT_TYPE_ANIMATED) + ? this.hintTextPadding : this.defaultPadding.get(TiC.PROPERTY_BOTTOM)); + this.tv.setPadding( this.tv.getPaddingLeft(), (type == UIModule.HINT_TYPE_ANIMATED) ? this.hintTextPadding : this.tv.getPaddingBottom(), diff --git a/tests/Resources/android/snapshots/textfieldPadding@2.75x.png b/tests/Resources/android/snapshots/textfieldPadding@2.75x.png new file mode 100644 index 00000000000..86e7eb32cc3 Binary files /dev/null and b/tests/Resources/android/snapshots/textfieldPadding@2.75x.png differ diff --git a/tests/Resources/ti.ui.textfield.test.js b/tests/Resources/ti.ui.textfield.test.js index 30bee55770d..7d33e739b92 100644 --- a/tests/Resources/ti.ui.textfield.test.js +++ b/tests/Resources/ti.ui.textfield.test.js @@ -144,6 +144,43 @@ describe('Titanium.UI.TextField', () => { win.open(); }); + it.android('android padding (visual check)', function (finish) { + this.timeout(5000); + const textField = Ti.UI.createTextField({ + value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec ullamcorper massa, eget tempor sapien. Phasellus nisi metus, tempus a magna nec, ultricies rutrum lacus. Aliquam sit amet augue suscipit, dignissim tellus eu, consectetur elit. Praesent ligula velit, blandit vel urna sit amet, suscipit euismod nunc.', + width: 100, + height: 40, + backgroundColor: 'white', + color: 'black', + padding: { + top: 10, + bottom: 10 + } + }); + const bgView = Ti.UI.createView({ + width: 200, + height: 100, + backgroundColor: 'red' + }); + win = Ti.UI.createWindow({ + backgroundColor: '#eee' + }); + bgView.add(textField); + win.add(bgView); + + win.addEventListener('postlayout', function postlayout() { // FIXME: Support once! + win.removeEventListener('postlayout', postlayout); // only run once + try { + should(textField).matchImage('snapshots/textfieldPadding.png'); + } catch (err) { + return finish(err); + } + finish(); + }); + + win.open(); + }); + describe('.hintText', () => { let textField; beforeEach(() => {