use of com.codename1.ui.TextArea in project CodenameOne by codenameone.
the class HTMLForm method reset.
/**
* Called when a form reset is needed and resets all the form fields to their default values.
*/
void reset() {
for (Enumeration e = defaultValues.keys(); e.hasMoreElements(); ) {
Object input = e.nextElement();
if (input instanceof TextArea) {
// catches both textareas and text input fields
String defVal = (String) defaultValues.get(input);
if (defVal == null) {
defVal = "";
}
((TextArea) input).setText(defVal);
} else if (input instanceof ComboBox) {
OptionItem defVal = (OptionItem) defaultValues.get(input);
ComboBox combo = ((ComboBox) input);
if (defVal != null) {
combo.setSelectedItem(defVal);
} else if (combo.size() > 0) {
combo.setSelectedIndex(0);
}
}
}
for (Enumeration e = defaultCheckedButtons.elements(); e.hasMoreElements(); ) {
Button b = (Button) e.nextElement();
if (!b.isSelected()) {
setButton(b, true);
}
}
for (Enumeration e = defaultUncheckedButtons.elements(); e.hasMoreElements(); ) {
Button b = (Button) e.nextElement();
if (b.isSelected()) {
setButton(b, false);
}
}
}
use of com.codename1.ui.TextArea in project CodenameOne by codenameone.
the class ResetableTextWatcher method startEditing.
/**
* Start editing the given text-area
* This method is executed on the UI thread, so UI manipulation is safe here.
* @param activity Current running activity
* @param textArea The TextAreaData instance that wraps the CN1 TextArea that our internal EditText needs to overlap. We use
* a TextAreaData so that the text area properties can be accessed off the EDT safely.
* @param codenameOneInputType One of the input type constants in com.codename1.ui.TextArea
* @param initialText The text that appears in the Codename One text are before the call to startEditing
* @param isEditedFieldSwitch if true, then special case for async edit mode - the native editing is already active, no need to show
* native field, just change the connected field
*/
private synchronized void startEditing(Activity activity, TextAreaData textArea, String initialText, int codenameOneInputType, final boolean isEditedFieldSwitch) {
int txty = lastTextAreaY = textArea.getAbsoluteY() + textArea.getScrollY();
int txtx = lastTextAreaX = textArea.getAbsoluteX() + textArea.getScrollX();
lastTextAreaWidth = textArea.getWidth();
lastTextAreaHeight = textArea.getHeight();
int paddingTop = 0;
int paddingLeft = textArea.paddingLeft;
int paddingRight = textArea.paddingRight;
int paddingBottom = textArea.paddingBottom;
if (textArea.isTextField) {
switch(textArea.getVerticalAlignment()) {
case Component.BOTTOM:
paddingTop = textArea.getHeight() - textArea.paddingBottom - textArea.fontHeight;
break;
case Component.CENTER:
paddingTop = textArea.getHeight() / 2 - textArea.fontHeight / 2;
break;
default:
paddingTop = textArea.paddingTop;
break;
}
} else {
paddingTop = textArea.paddingTop;
}
int id = activity.getResources().getIdentifier("cn1Style", "attr", activity.getApplicationInfo().packageName);
if (!isEditedFieldSwitch) {
mEditText = new EditView(activity, textArea.textArea, this, id);
} else {
mEditText.switchToTextArea(textArea.textArea);
}
if (textArea.getClientProperty("blockCopyPaste") != null || Display.getInstance().getProperty("blockCopyPaste", "false").equals("true")) {
// The code below is taken from this stackoverflow answer: http://stackoverflow.com/a/22756538/756809
if (android.os.Build.VERSION.SDK_INT < 11) {
mEditText.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.clear();
}
});
} else {
mEditText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
}
} else if (isEditedFieldSwitch) {
// reset copy-paste protection
if (android.os.Build.VERSION.SDK_INT < 11) {
mEditText.setOnCreateContextMenuListener(null);
} else {
mEditText.setCustomSelectionActionModeCallback(null);
}
}
if (!isEditedFieldSwitch) {
mEditText.addTextChangedListener(mEditText.mTextWatcher);
}
mEditText.setBackgroundDrawable(null);
mEditText.setFocusableInTouchMode(true);
mEditLayoutParams = new FrameLayout.LayoutParams(0, 0);
// Set the appropriate gravity so that the left and top margins will be
// taken into account
mEditLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
mEditLayoutParams.setMargins(txtx, txty, 0, 0);
mEditLayoutParams.width = textArea.getWidth();
mEditLayoutParams.height = textArea.getHeight();
mEditText.setLayoutParams(mEditLayoutParams);
if (textArea.isRTL()) {
mEditText.setGravity(Gravity.RIGHT | Gravity.TOP);
} else {
mEditText.setGravity(Gravity.LEFT | Gravity.TOP);
}
mEditText.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
Component nextDown = textArea.nextDown;
boolean imeOptionTaken = true;
int ime = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
if (textArea.isSingleLineTextArea()) {
if (textArea.getClientProperty("searchField") != null) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_SEARCH);
} else {
if (textArea.getClientProperty("sendButton") != null) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_SEND);
} else {
if (textArea.getClientProperty("goButton") != null) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_GO);
} else {
if (textArea.isTextField && textArea.getDoneListener() != null) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_DONE);
} else if (nextDown != null && nextDown instanceof TextArea && ((TextArea) nextDown).isEditable() && ((TextArea) nextDown).isEnabled()) {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_NEXT);
} else {
mEditText.setImeOptions(ime | EditorInfo.IME_ACTION_DONE);
imeOptionTaken = false;
}
}
}
}
}
mEditText.setSingleLine(textArea.isSingleLineTextArea());
mEditText.setAdapter((ArrayAdapter<String>) null);
mEditText.setText(initialText);
if (!textArea.isSingleLineTextArea() && textArea.textArea.isGrowByContent() && textArea.textArea.getGrowLimit() > -1) {
defaultMaxLines = mEditText.getMaxLines();
mEditText.setMaxLines(textArea.textArea.getGrowLimit());
}
if (textArea.nativeHintBool && textArea.getHint() != null) {
mEditText.setHint(textArea.getHint());
}
if (!isEditedFieldSwitch) {
addView(mEditText, mEditLayoutParams);
}
invalidate();
setVisibility(VISIBLE);
bringToFront();
mEditText.requestFocus();
Object nativeFont = textArea.nativeFont;
if (nativeFont == null) {
nativeFont = impl.getDefaultFont();
}
Paint p = (Paint) ((AndroidImplementation.NativeFont) nativeFont).font;
mEditText.setTypeface(p.getTypeface());
mEditText.setTextScaleX(p.getTextScaleX());
mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, p.getTextSize());
int fgColor = textArea.fgColor;
mEditText.setTextColor(Color.rgb(fgColor >> 16, (fgColor & 0x00ff00) >> 8, (fgColor & 0x0000ff)));
boolean password = false;
if ((codenameOneInputType & TextArea.PASSWORD) == TextArea.PASSWORD) {
codenameOneInputType = codenameOneInputType ^ TextArea.PASSWORD;
password = true;
}
if (textArea.isSingleLineTextArea()) {
mEditText.setInputType(getAndroidInputType(codenameOneInputType));
// if not ime was explicity requested and this is a single line textfield of type ANY add the emoji keyboard.
if (!imeOptionTaken && codenameOneInputType == TextArea.ANY) {
mEditText.setInputType(getAndroidInputType(codenameOneInputType) | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
}
if (Display.getInstance().getProperty("andAddComma", "false").equals("true") && (codenameOneInputType & TextArea.DECIMAL) == TextArea.DECIMAL) {
defaultKeyListener = mEditText.getKeyListener();
mEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));
}
}
if (password) {
int type = mInputTypeMap.get(codenameOneInputType, InputType.TYPE_CLASS_TEXT);
if ((type & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) == InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) {
type = type ^ InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
}
// turn off suggestions for passwords
mEditText.setInputType(type | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mEditText.setTransformationMethod(new MyPasswordTransformationMethod());
}
int maxLength = textArea.maxSize;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
mEditText.setFilters(FilterArray);
mEditText.setSelection(mEditText.getText().length());
showVirtualKeyboard(true);
if (Boolean.FALSE.equals(textArea.getClientProperty("android.cursorVisible"))) {
// This provides an imperfect workaround for this issue:
// https://github.com/codenameone/CodenameOne/issues/2317
// Blinking cursor causes text to disappear on some versions of android
// Can't seem to find how to detect whether device is affected, so
// just providing a client property to disable the blinking cursor
// on a particular text field.
mEditText.setCursorVisible(false);
}
/*
// Leaving this hack here for posterity. It seems that this manually
// blinking cursor causes the paste menu to disappear
// https://github.com/codenameone/CodenameOne/issues/2147
// Removing the hack below, fixes this issue. And in the test device
// I'm using the blinking of text doesn't seem to occur, so perhaps
// it was fixed via other means. Test device:
// Name: Samsung Galaxy S3 (T-Mobile)
// OS: 4.3
// Manufacturer: Samsung
// Model: 4.3
// Chipset: armeabi-v7a 1512MHz
// Memory: 16000000000
// Heap: 256000000
// Display: 720 x 1280
//
// UPDATE Feb. 13, 2018:
// This issue seems to be still present in some devices, but it isn't clear even
// how to detect it.
// Issue reported and reproduced here https://github.com/codenameone/CodenameOne/issues/2317
// Issue has been observed in a Virtual Box installation with 5.1.1, but
// cannot be reproduced in a Nexus 5 running 5.1.1.
//
if (Build.VERSION.SDK_INT < 21) {
// HACK!!! On Android 4.4, it seems that the natural blinking cursor
// causes text to disappear when it blinks. Manually blinking the
// cursor seems to work around this issue, so that's what we do here.
// This issue is described here: http://stackoverflow.com/questions/41305052/textfields-content-disappears-during-typing?noredirect=1#comment69977316_41305052
mEditText.setCursorVisible(false);
final boolean[] cursorVisible = new boolean[]{false};
if (cursorTimer != null) {
cursorTimer.cancel();
}
cursorTimer = new Timer();
cursorTimerTask = new TimerTask() {
public void run() {
AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() {
public void run() {
EditView v = mEditText;
if (v != null) {
cursorVisible[0] = !cursorVisible[0];
v.setCursorVisible(cursorVisible[0]);
}
}
});
}
};
cursorTimer.schedule(cursorTimerTask, 100, 500);
}
*/
}
use of com.codename1.ui.TextArea in project CodenameOne by codenameone.
the class ResetableTextWatcher method setEditedTextField.
public static void setEditedTextField(final TextArea textarea) {
Display display = Display.getInstance();
Runnable task = new Runnable() {
public void run() {
AndroidImplementation.getInstance().setFocusedEditingText(textarea);
}
};
if (display.isEdt()) {
task.run();
} else {
display.callSeriallyAndWait(task);
}
}
use of com.codename1.ui.TextArea in project CodenameOne by codenameone.
the class ResetableTextWatcher method reLayoutEdit.
public static void reLayoutEdit(boolean force) {
if (mIsEditing && !isActiveTextEditorHidden() && sInstance != null && sInstance.mEditText != null) {
final TextArea txt = sInstance.mEditText.mTextArea;
if (!force && lastTextAreaX == txt.getAbsoluteX() + txt.getScrollX() && lastTextAreaY == txt.getAbsoluteY() + txt.getScrollY() && lastTextAreaWidth == txt.getWidth() && lastTextAreaHeight == txt.getHeight()) {
return;
}
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (mIsEditing && !isActiveTextEditorHidden() && sInstance != null && sInstance.mEditText != null) {
if (txt != null) {
if (sInstance.mEditText.mTextArea != txt) {
// has changed in between, skip or would change location back to old field
return;
}
final int txty = lastTextAreaY = txt.getAbsoluteY() + txt.getScrollY();
final int txtx = lastTextAreaX = txt.getAbsoluteX() + txt.getScrollX();
final int w = lastTextAreaWidth = txt.getWidth();
final int h = lastTextAreaHeight = txt.getHeight();
sInstance.impl.getActivity().runOnUiThread(new Runnable() {
public void run() {
if (mIsEditing && !isActiveTextEditorHidden() && sInstance != null && sInstance.mEditText != null) {
if (sInstance.mEditText.mTextArea != txt) {
// has changed in between, skip or would change location back to old field
return;
}
sInstance.mEditLayoutParams.setMargins(txtx, txty, 0, 0);
sInstance.mEditLayoutParams.width = w;
sInstance.mEditLayoutParams.height = h;
sInstance.mEditText.requestLayout();
sInstance.invalidate();
if (sInstance.getVisibility() != VISIBLE) {
sInstance.setVisibility(VISIBLE);
}
sInstance.bringToFront();
}
}
});
}
}
}
});
}
}
use of com.codename1.ui.TextArea in project CodenameOne by codenameone.
the class ResetableTextWatcher method onEditorAction.
/**
* This method will be called by our EditText control when the action
* key (Enter/Go/Send) on the soft keyboard will be pressed.
* @param actionCode
* @return task to run after call to super.onEditorAction. Returns null if action was consumed (tapped Next in async mode) and super.onEditorAction do not have to be called.
*/
Runnable onEditorAction(int actionCode) {
actionCode = actionCode & 0xf;
final int fActionCode = actionCode;
boolean hasNext = false;
if (EditorInfo.IME_ACTION_NEXT == actionCode && mEditText != null && mEditText.mTextArea != null) {
Component next = mEditText.mTextArea.getNextFocusDown();
if (next == null) {
next = mEditText.mTextArea.getComponentForm().findNextFocusVertical(true);
}
if (next != null && next instanceof TextArea && ((TextArea) next).isEditable() && ((TextArea) next).isEnabled()) {
hasNext = true;
nextTextArea = (TextArea) next;
}
}
if (hasNext && nextTextArea != null && impl.isAsyncEditMode()) {
// in async edit mode go right to next field edit to avoid hiding and showing again the native edit text
TextArea theNext = nextTextArea;
nextTextArea = null;
edit(sInstance.impl, theNext, theNext.getConstraint());
return null;
} else {
return new Runnable() {
@Override
public void run() {
endEditing(REASON_IME_ACTION, false, fActionCode);
}
};
}
}
Aggregations