Skip to content

Commit

Permalink
Merge pull request #9114 from garymathews/TIMOB-24639
Browse files Browse the repository at this point in the history
[TIMOB-24639] Android: Implement Ti.UI.TextField.hintType
  • Loading branch information
Lokesh Choudhary committed Aug 7, 2017
2 parents 9d8cc45 + 2392118 commit 7f0c04e
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
TiC.PROPERTY_HINT_TEXT,
TiC.PROPERTY_HINT_TEXT_ID,
TiC.PROPERTY_HINT_TEXT_COLOR,
TiC.PROPERTY_HINT_TYPE,
TiC.PROPERTY_INPUT_TYPE,
TiC.PROPERTY_KEYBOARD_TYPE,
TiC.PROPERTY_MAX_LENGTH,
Expand All @@ -59,6 +60,7 @@ public TextFieldProxy()
defaultValues.put(TiC.PROPERTY_VALUE, "");
defaultValues.put(TiC.PROPERTY_MAX_LENGTH, -1);
defaultValues.put(TiC.PROPERTY_FULLSCREEN, true);
defaultValues.put(TiC.PROPERTY_HINT_TYPE, UIModule.HINT_TYPE_STATIC);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ public class UIModule extends KrollModule implements Handler.Callback
@Kroll.constant public static final int INPUT_TYPE_CLASS_NUMBER = InputType.TYPE_CLASS_NUMBER;
@Kroll.constant public static final int INPUT_TYPE_CLASS_TEXT = InputType.TYPE_CLASS_TEXT;

@Kroll.constant public static final int HINT_TYPE_STATIC = 0;
@Kroll.constant public static final int HINT_TYPE_ANIMATED = 1;

@Kroll.constant public static final int HIDDEN_BEHAVIOR_GONE = View.GONE;
@Kroll.constant public static final int HIDDEN_BEHAVIOR_INVISIBLE = View.INVISIBLE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.AttributedStringProxy;
import ti.modules.titanium.ui.UIModule;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
Expand All @@ -45,6 +48,7 @@
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

Expand Down Expand Up @@ -88,6 +92,7 @@ public class TiUIText extends TiUIView
private boolean disableChangeEvent = false;

protected TiUIEditText tv;
protected TextInputLayout textInputLayout;

public TiUIText(final TiViewProxy proxy, boolean field)
{
Expand Down Expand Up @@ -130,7 +135,10 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
} else {
tv.setGravity(Gravity.TOP | Gravity.LEFT);
}
setNativeView(tv);

textInputLayout = new TextInputLayout(proxy.getActivity());
textInputLayout.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
setNativeView(textInputLayout);
}

@Override
Expand Down Expand Up @@ -159,7 +167,15 @@ public void processProperties(KrollDict d)
}

if (d.containsKey(TiC.PROPERTY_HINT_TEXT)) {
tv.setHint(d.getString(TiC.PROPERTY_HINT_TEXT));
String hintText = d.getString(TiC.PROPERTY_HINT_TEXT);
if (hintText != null) {
int type = TiConvert.toInt(d.get(TiC.PROPERTY_HINT_TYPE), UIModule.HINT_TYPE_STATIC);
if (type == UIModule.HINT_TYPE_STATIC) {
tv.setHint(hintText);
} else if (type == UIModule.HINT_TYPE_ANIMATED) {
textInputLayout.setHint(hintText);
}
}
}

if (d.containsKey(TiC.PROPERTY_HINT_TEXT_COLOR)) {
Expand Down Expand Up @@ -285,6 +301,18 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
tv.setHint(TiConvert.toString(newValue));
} else if (key.equals(TiC.PROPERTY_HINT_TEXT_COLOR)) {
tv.setHintTextColor(TiConvert.toColor((String) newValue));
} else if (key.equals(TiC.PROPERTY_HINT_TYPE)) {
String hintText = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_HINT_TEXT));
if (hintText != null) {
int type = TiConvert.toInt(newValue);
if (type == UIModule.HINT_TYPE_STATIC) {
textInputLayout.setHint("");
tv.setHint(hintText);
} else if (type == UIModule.HINT_TYPE_ANIMATED) {
tv.setHint("");
textInputLayout.setHint(hintText);
}
}
} else if (key.equals(TiC.PROPERTY_ELLIPSIZE)) {
if (TiConvert.toBoolean(newValue)) {
tv.setEllipsize(TruncateAt.END);
Expand Down
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,11 @@ public class TiC
*/
public static final String PROPERTY_HINT_TEXT_COLOR = "hintTextColor";

/**
* @module.api
*/
public static final String PROPERTY_HINT_TYPE = "hintType";

/**
* @module.api
*/
Expand Down
10 changes: 10 additions & 0 deletions apidoc/Titanium/UI/TextField.yml
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ properties:
type: String
since: "6.2.0"

- name: hintType
summary: Hint type to display on the text field.
platforms: [android]
since: {android: "6.2.0"}
description: |
Setting this to <Titanium.UI.HINT_TYPE_ANIMATED> will use the animated TextInputLayout on Android.
type: Number
constants: Titanium.UI.HINT_TYPE_*
default: <Titanium.UI.HINT_TYPE_STATIC>

- name: inputType
summary: Input type to accept in the text field. Also influences the Keyboard type to display.
description: |
Expand Down
239 changes: 239 additions & 0 deletions tests/Resources/ti.ui.textfield.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-2016 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.
*/
var should = require('./utilities/assertions'),
utilities = require('./utilities/utilities'),
didFocus = false;

describe('Titanium.UI.TextField', function () {

beforeEach(function() {
didFocus = false;
});

it('apiName', function () {
var textField = Ti.UI.createTextField({
value: 'this is some text'
});
should(textField).have.readOnlyProperty('apiName').which.is.a.String;
should(textField.apiName).be.eql('Ti.UI.TextField');
});

it('value', function () {
var textfield = Ti.UI.createTextField({
value: 'this is some text'
});
should(textfield.value).be.a.String;
should(textfield.getValue).be.a.Function;
should(textfield.value).eql('this is some text');
should(textfield.getValue()).eql('this is some text');
textfield.value = 'other text';
should(textfield.value).eql('other text');
should(textfield.getValue()).eql('other text');
});

// Skip on Windows Phone since not available, yet
(!(utilities.isIOS() || utilities.isAndroid()) ? it.skip : it)('padding', function () {
var textfield = Ti.UI.createTextField({
value: 'this is some text',
padding: {
left: 20,
right: 20
}
});
should(textfield.padding).be.a.Object;
should(textfield.getPadding).be.a.Function;
should(textfield.setPadding).be.a.Function;

should(textfield.padding.left).eql(20);
should(textfield.padding.right).eql(20);
should(textfield.getPadding().left).eql(20);
should(textfield.getPadding().right).eql(20);

textfield.setPadding({
left: 10,
right: 10
});

should(textfield.padding.left).eql(10);
should(textfield.padding.right).eql(10);
should(textfield.getPadding().left).eql(10);
should(textfield.getPadding().right).eql(10);
});

it('textAlign', function () {
var textfield = Ti.UI.createTextField({
value: 'this is some text',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER
});
if (utilities.isAndroid()) {
should(textfield.textAlign).be.a.String;
} else {
should(textfield.textAlign).be.a.Number;
}
should(textfield.getTextAlign).be.a.Function;
should(textfield.textAlign).eql(Titanium.UI.TEXT_ALIGNMENT_CENTER);
should(textfield.getTextAlign()).eql(Titanium.UI.TEXT_ALIGNMENT_CENTER);
textfield.textAlign = Titanium.UI.TEXT_ALIGNMENT_RIGHT;
should(textfield.textAlign).eql(Titanium.UI.TEXT_ALIGNMENT_RIGHT);
should(textfield.getTextAlign()).eql(Titanium.UI.TEXT_ALIGNMENT_RIGHT);
});

it('verticalAlign', function () {
var textfield = Ti.UI.createTextField({
value: 'this is some text',
verticalAlign: Titanium.UI.TEXT_VERTICAL_ALIGNMENT_BOTTOM
});
if (utilities.isAndroid()) {
should(textfield.verticalAlign).be.a.String;
} else {
should(textfield.verticalAlign).be.a.Number;
}
should(textfield.getVerticalAlign).be.a.Function;
should(textfield.verticalAlign).eql(Titanium.UI.TEXT_VERTICAL_ALIGNMENT_BOTTOM);
should(textfield.getVerticalAlign()).eql(Titanium.UI.TEXT_VERTICAL_ALIGNMENT_BOTTOM);
textfield.verticalAlign = Titanium.UI.TEXT_VERTICAL_ALIGNMENT_TOP;
should(textfield.verticalAlign).eql(Titanium.UI.TEXT_VERTICAL_ALIGNMENT_TOP);
should(textfield.getVerticalAlign()).eql(Titanium.UI.TEXT_VERTICAL_ALIGNMENT_TOP);
});

// FIXME Defaults to undefined on Android. Docs say default is false
(utilities.isAndroid() ? it.skip : it)('passwordMask', function () {
var text = 'this is some text',
textfield = Ti.UI.createTextField({
value: text
});
// passwordMask should default to false
should(textfield.passwordMask).be.false; // undefined on Android
textfield.passwordMask = true;
should(textfield.passwordMask).be.true;
// it should have same text before
should(textfield.value).be.eql(text);
});

// TODO Add tests for:
// autocapitalize
// autocorrect
// borderStyle
// clearonEdit
// color
// editable
// enableReturnKey
// font
// keyboardType
// maxLength
// returnKeyType
// selection
// suppressReturn

it('hintText', function () {
var textfield = Ti.UI.createTextField({
hintText: 'Enter E-Mail ...'
});
should(textfield.getHintText).be.a.Function;
should(textfield.hintText).eql('Enter E-Mail ...');
should(textfield.getHintText()).eql('Enter E-Mail ...');
textfield.hintText = 'Enter Name ...';
should(textfield.hintText).eql('Enter Name ...');
should(textfield.getHintText()).eql('Enter Name ...');
});

it('hintTextColor', function () {
var textfield = Ti.UI.createTextField({
hintText: 'Enter E-Mail ...',
hintTextColor: 'red'
});
should(textfield.getHintTextColor).be.a.Function;
should(textfield.hintTextColor).eql('red');
should(textfield.getHintTextColor()).eql('red');
textfield.hintTextColor = 'blue';
should(textfield.hintTextColor).eql('blue');
should(textfield.getHintTextColor()).eql('blue');
});

(utilities.isIOS() ? it.skip : it)('hintType', function () {
var textfield = Ti.UI.createTextField({
hintText: 'Enter E-Mail ...',
hintType: Ti.UI.HINT_TYPE_ANIMATED
});
should(textfield.getHintType).be.a.Function;
should(textfield.hintType).eql(Ti.UI.HINT_TYPE_ANIMATED);
should(textfield.getHintType()).eql(Ti.UI.HINT_TYPE_ANIMATED);
textfield.hintType = Ti.UI.HINT_TYPE_STATIC;
should(textfield.hintType).eql(Ti.UI.HINT_TYPE_STATIC);
should(textfield.getHintType()).eql(Ti.UI.HINT_TYPE_STATIC);
});

it.skip('width', function (finish) {
this.timeout(5000);
var 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: Ti.UI.SIZE
});
var win = Ti.UI.createWindow({
backgroundColor: '#ddd'
});
win.add(textfield);
win.addEventListener('focus', function () {
var error;

try {
should(win.width).be.greaterThan(100);
should(textfield.width).not.be.greaterThan(win.width);
} catch (err) {
error = err;
}

setTimeout(function() {
win.close();
finish(error);
}, 3000);
});
win.open();
});

// FIXME Intermittently failing on Android on build machine, I think due to test timeout
(utilities.isAndroid() ? it.skip : it)('height', function (finish) {
this.timeout(5000);
var 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: Ti.UI.SIZE,
height: Ti.UI.SIZE,
color: 'black'
}),
bgView = Ti.UI.createView({
width: 200,
height: 100,
backgroundColor: 'red'
}),
win = Ti.UI.createWindow({
backgroundColor: '#eee'
});
bgView.add(textfield);
win.add(bgView);

win.addEventListener('focus', function () {
var error;

if (didFocus) return;
didFocus = true;

try {
should(bgView.height).be.eql(100);
should(textfield.height).not.be.greaterThan(100);
} catch (err) {
error = err;
}

setTimeout(function() {
win.close();
finish(error);
}, 3000);
});
win.open();
});

});

0 comments on commit 7f0c04e

Please sign in to comment.