Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-24639] Android: Refactor TextInputLayout implementation #9361

Merged
merged 7 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
TiC.PROPERTY_FULLSCREEN,
TiC.PROPERTY_HINT_TEXT,
TiC.PROPERTY_HINT_TEXT_COLOR,
TiC.PROPERTY_HINT_TYPE,
TiC.PROPERTY_KEYBOARD_TYPE,
TiC.PROPERTY_MAX_LENGTH,
TiC.PROPERTY_PASSWORD_MASK,
Expand All @@ -56,6 +57,7 @@ public TextAreaProxy()
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 @@ -24,6 +24,7 @@
import ti.modules.titanium.ui.UIModule;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
Expand All @@ -47,7 +48,6 @@
import android.view.ViewGroup;
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 @@ -137,7 +137,8 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
}

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

setNativeView(textInputLayout);
}

Expand All @@ -162,19 +163,19 @@ public void processProperties(KrollDict d)
tv.setText("");
}

if (d.containsKey(TiC.PROPERTY_BACKGROUND_COLOR)) {
tv.setBackgroundColor(Color.TRANSPARENT);
}

if (d.containsKey(TiC.PROPERTY_COLOR)) {
tv.setTextColor(TiConvert.toColor(d, TiC.PROPERTY_COLOR));
}

if (d.containsKey(TiC.PROPERTY_HINT_TEXT)) {
String hintText = d.getString(TiC.PROPERTY_HINT_TEXT);
if (d.containsKey(TiC.PROPERTY_HINT_TEXT) || d.containsKey(TiC.PROPERTY_HINT_TYPE)) {
String hintText = TiConvert.toString(d.get(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);
}
setHintText(type, hintText);
}
}

Expand Down Expand Up @@ -248,10 +249,10 @@ public void processProperties(KrollDict d)

private void setTextPadding(HashMap<String, Object> d)
{
int paddingLeft = tv.getPaddingLeft();
int paddingRight = tv.getPaddingRight();
int paddingTop = tv.getPaddingTop();
int paddingBottom = tv.getPaddingBottom();
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);
Expand All @@ -269,7 +270,7 @@ private void setTextPadding(HashMap<String, Object> d)
paddingBottom = TiConvert.toInt(d.get(TiC.PROPERTY_BOTTOM), 0);
}

tv.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
textInputLayout.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}

@Override
Expand All @@ -295,22 +296,25 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
tv.setText(truncateText);
tv.setSelection(cursor);
}
} else if (key.equals(TiC.PROPERTY_BACKGROUND_COLOR)) {
tv.setBackgroundColor(Color.TRANSPARENT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The below needs to be called after changing TextView background color to transparent since we're in an else-if block (the super class' method only gets called in the else block).

super.propertyChanged(key, oldValue, newValue, proxy);

super.propertyChanged(key, oldValue, newValue, proxy);
} else if (key.equals(TiC.PROPERTY_COLOR)) {
tv.setTextColor(TiConvert.toColor((String) newValue));
} else if (key.equals(TiC.PROPERTY_HINT_TEXT)) {
tv.setHint(TiConvert.toString(newValue));
int type = proxy.getProperties().getInt(TiC.PROPERTY_HINT_TYPE);
setHintText(type, 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);
Object attributedHintText = proxy.getProperty(TiC.PROPERTY_ATTRIBUTED_HINT_TEXT);
if (attributedHintText instanceof AttributedStringProxy) {
setAttributedStringHint((AttributedStringProxy) attributedHintText);
} else {
String hintText = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_HINT_TEXT));
if (hintText != null) {
int type = TiConvert.toInt(newValue);
setHintText(type, hintText);
}
}
} else if (key.equals(TiC.PROPERTY_ELLIPSIZE)) {
Expand Down Expand Up @@ -422,13 +426,13 @@ public void onTextChanged(CharSequence s, int start, int before, int count)
public void focus()
{
super.focus();
if (nativeView != null) {
if (tv != null) {
if (proxy.hasProperty(TiC.PROPERTY_EDITABLE)
&& !(TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_EDITABLE)))) {
TiUIHelper.showSoftKeyboard(nativeView, false);
TiUIHelper.showSoftKeyboard(tv, false);
}
else {
TiUIHelper.requestSoftInputChange(proxy, nativeView);
TiUIHelper.requestSoftInputChange(proxy, tv);
}
}
}
Expand All @@ -439,11 +443,11 @@ public void onFocusChange(View v, boolean hasFocus)
if (hasFocus) {
Boolean clearOnEdit = (Boolean) proxy.getProperty(TiC.PROPERTY_CLEAR_ON_EDIT);
if (clearOnEdit != null && clearOnEdit) {
((EditText) nativeView).setText("");
tv.setText("");
}
Rect r = new Rect();
nativeView.getFocusedRect(r);
nativeView.requestRectangleOnScreen(r);
tv.getFocusedRect(r);
tv.requestRectangleOnScreen(r);

}
super.onFocusChange(v, hasFocus);
Expand Down Expand Up @@ -805,7 +809,20 @@ public void setAttributedStringText(AttributedStringProxy attrString) {
public void setAttributedStringHint(AttributedStringProxy attrString) {
Spannable spannableText = AttributedStringProxy.toSpannable(attrString, TiApplication.getAppCurrentActivity());
if (spannableText != null) {
tv.setHint(spannableText);
int type = getProxy().getProperties().getInt(TiC.PROPERTY_HINT_TYPE);
setHintText(type, spannableText);
}
}

public void setHintText(int type, CharSequence hintText) {
if (type == UIModule.HINT_TYPE_STATIC) {
textInputLayout.setHint("");
textInputLayout.setHintEnabled(false);
tv.setHint(hintText);
} else if (type == UIModule.HINT_TYPE_ANIMATED) {
tv.setHint("");
textInputLayout.setHint(hintText);
textInputLayout.setHintEnabled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ public TiUISearchBar(final TiViewProxy proxy)

View nativeView = getNativeView();
if (nativeView instanceof EditText) {
this.tv = (EditText)nativeView;
this.tv = (EditText) nativeView;
} else if (nativeView instanceof TextInputLayout) {
this.tv = ((TextInputLayout)nativeView).getEditText();
} else {
throw new IllegalStateException();
this.tv = ((TextInputLayout) nativeView).getEditText();
}
if (this.tv == null) {
throw new Error("could not obtain EditText component");
}

this.tv.setImeOptions(EditorInfo.IME_ACTION_DONE);
Expand Down
65 changes: 64 additions & 1 deletion apidoc/Titanium/UI/TextArea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,53 @@ properties:
default: <Titanium.UI.KEYBOARD_APPEARANCE_DEFAULT>
platforms: [iphone, ipad]

- name: attributedHintText
summary: Hint text attributed string.
description: |
The attributed hint text by the text area. If set, avoid setting common attributes
in text area, such as `hintText`, `color` and `font`, as unexpected behaviors may result.

This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED.

Prior to Release 3.6.0, assign this property an object from the
<Titanium.UI.iOS.AttributedString> class.

Since Appcelerator CLI 4.1.0 (Alloy 1.7.0), for Alloy, you can use an `<AttributedHintText>`
element inside a `<TextField>` element and set the text property as node text:

<Alloy>
<Window>
<TextField>
<AttributedHintText>
Alloy is great!
</AttributedHintText>
</TextField>
</Window>
</Alloy>

Then set attributes in the TSS file:

"AttributedString" : {
attributes: [
{
type: Ti.UI.ATTRIBUTE_FOREGROUND_COLOR,
value: 'red',
range: [0, 5]
},
{
type: Ti.UI.ATTRIBUTE_UNDERLINES_STYLE,
value: Ti.UI.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
range: [9, 5]
}
]
}
type: Titanium.UI.AttributedString
platforms: [android, iphone, ipad]
since:
android: 3.6.0
iphone: 3.2.0
ipad: 3.2.0

- name: attributedString
summary: TextArea attributed string.
description: |
Expand Down Expand Up @@ -247,9 +294,22 @@ properties:
</android>

Another way to change the hint text color is to use this property and specify a color.

This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED. Instead a theme must be
used that defines the `android:textColorHint` attribute.
type: String
default: Default Android theme's hint text color.

- name: hintType
summary: Hint type to display on the text field.
platforms: [android]
since: {android: "7.0.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: handleLinks
summary: Specifies if the text area should allow user interaction with the given URL in the given range of text.
description: |
Expand Down Expand Up @@ -346,7 +406,10 @@ properties:
platforms: [iphone, ipad]

- name: textAlign
summary: Text alignment within this text area.
summary: |
Text alignment within this text area.

This has no effect on `hintText` when `hintType` is Ti.UI.HINT_TYPE_ANIMATED.
type: [String, Number]
constants: Titanium.UI.TEXT_ALIGNMENT_*
default: <Titanium.UI.TEXT_ALIGNMENT_LEFT>
Expand Down
12 changes: 10 additions & 2 deletions apidoc/Titanium/UI/TextField.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ properties:
The attributed hint text by the textField. If set, avoid setting common attributes
in textField, such as `hintText`, `color` and `font`, as unexpected behaviors may result.

This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED.

Prior to Release 3.6.0, assign this property an object from the
<Titanium.UI.iOS.AttributedString> class.

Expand Down Expand Up @@ -300,6 +302,9 @@ properties:
by the <Titanium.UI.TextField.attributedHintText> which provides an advanced configuration to
style hint texts.

This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED. Instead a theme must be
used that defines the `android:textColorHint` attribute.

*Note for Android*:
The hint text color in Android is determined by the theme of the application. By default, the
theme is 'Theme.AppCompat' which is a Dark theme. When you create the background to be white,
Expand Down Expand Up @@ -327,7 +332,7 @@ properties:
- name: hintType
summary: Hint type to display on the text field.
platforms: [android]
since: {android: "6.2.0"}
since: {android: "7.0.0"}
description: |
Setting this to <Titanium.UI.HINT_TYPE_ANIMATED> will use the animated TextInputLayout on Android.
type: Number
Expand Down Expand Up @@ -545,7 +550,10 @@ properties:
since: 5.0.0

- name: textAlign
summary: Text alignment within this text field.
summary: |
Text alignment within this text field.

This has no effect on `hintText` when `hintType` is Ti.UI.HINT_TYPE_ANIMATED.
type: [String, Number]
constants: Titanium.UI.TEXT_ALIGNMENT_*
default: <Titanium.UI.TEXT_ALIGNMENT_LEFT>
Expand Down
2 changes: 0 additions & 2 deletions apidoc/Titanium/UI/UI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1280,8 +1280,6 @@ properties:
platforms: [android]

- name: HINT_TYPE_ANIMATED
deprecated:
since: "6.2.0"
summary: |
Use when creating a TextField to specify the hintType as animated.
type: Number
Expand Down