EditText with multiple lines option accepts enter key as value android - android-edittext

This might be silly question ever but still couldn't find solution. I have EditText with multiple line option on press of enter key in softkey moves to next lines and when clicked on post button to post the content of edittext the value has 2 enter option[i.e 2 blank lines]. before posting i do check for
blank and null value but this doesn't prevent posting enter value.
if (!(story_write_text.getText().toString().equals(""))||!(story_write_text.getText().toString().equals(null)))
my question: how to check edittext value if there are only enter values so that i can prevent from posting blank enter value to server.
here's my edittext xml code:
<EditText
android:id="#+id/story_write_text"
style="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text"
android:gravity="left"
android:hint="Write a comment"
android:inputType="textCapSentences"
android:padding="5dp"
android:textSize="15sp" />

just add android:inputType="textPersonName"
to the EditText it will stop it from pressing enter
If it not working then use below code
I would subclass the widget and override the key event handling in order to block the Enter key:
class MyTextView extends EditText
{
...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode==KeyEvent.KEYCODE_ENTER)
{
// Just ignore the [Enter] key
return true;
}
// Handle all other keys in the default way
return super.onKeyDown(keyCode, event);
}
}

Related

Vaadin TextField's value not available in shortcut listener

I want to read a Vaadin Flow TextField's value when the user presses the Enter key. I can add a ShortcutListener to listen for the keypress, but the value of the TextField is null in the listener even though the TextField is not empty:
TextField textField = new TextField();
Shortcuts.addShortcutListener(UI.getCurrent(), () -> {
String text = texttextField.getValue();
// text doesn't contain the latest text
}, Key.ENTER);
Why is this happening and how can I read the value that the user has entered when the keypress listener is triggered?
The behavior boils down to browser events. While the user may have entered some text into the input element, the value of the TextField is not updated to the server until there's a ValueChange event - even the vaadin-text-field element in the browser doesn't "know" of the text (the value property has not been updated) when the enter keypress event occurs. By default, the value of the text field is only updated when the input loses focus. You can work around the issue by explicitly toggling a blur of the TextField:
// member field in class
boolean shortcutFired = false;
// ...
Shortcuts.addShortcutListener(UI.getCurrent(), () -> {
textField.blur();
shortcutFired = true;
}, Key.ENTER);
and listening to the value change event instead of the shortcut listener:
textField.addValueChangeListener(e -> {
if( shortcutFired ) {
String value = e.getValue();
// do something with the value
shortcutFired = false;
}
}
This approach doesn't work too well if you need to keep track of multiple fields; in that case, you might want to make the TextFields update their values to the server more eagerly by setting their ValueChangeMode to ValueChangeMode.EAGER. The downside of this approach is that it increases the amount of traffic between the browser and the network, as every keypress will trigger a server request.

android: display a toast message after clicking on edittext

I don't know if this is possible but I am looking for a way to display a toast message after clicking on edittext.
I tried this but it didn't work:
editText1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(RectangleWidth.this, "message here", Toast.LENGTH_SHORT).show();
return;
}
});
First you should set the edit text to android:focusable="false" in your xml code
Maybe it's not the best solution from a UI point of view. See here: Android EditText onClickListener

How to hide spinner on the basis of text entered in edittext box?

M sorry for wrong English. I want to hide the spinner on the basis of text entered in the edit Text box and not on some click event. This means spinner hiding condition should be checked on the basis of Edit Text value.
Any kind of help will be appreciated.
I have Used TextWatcher class to have a check on the text that is being entered in the edittext. And then have used the method to check ds
private void checkFieldsForEmptyValues()
{
Spinner =(Spinner)findViewById(R.id.site_spinner1);
String s1 = edit1.getText().toString();
String s2 = edit2.getText().toString();
if(s1.equals("admin") && s2.equals("admin"))
{
spinner.setEnabled(false);
}
}

How to prevent dialog from closing automatically on click in blackberry

I am developing an application in which i create a Dialog with Edit Field and save/Cancel button, i use validation on edit field,i want that if user enter valid entry and click save then the dialog should close but now the problem is when user click on save without enter any text in edit field dialog closes automatically,i want to prevent it after entering valid entry dialog should close.
I give cancel button to discard dialog.
Please help..how to achieve this..........
mOKButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field f, int arg1) {
if(_editText.getText()=="")
{
// do nothing
}
else{
_editText.setText(items[getSelectedIndex()]);
close();
}
}
});

Vaadin addStyleName problem

I created a TextField with TextChangeListener. When user types in certain values (in this case 'admin') then addStyleName is invoked on that field and font color becomes red. But afterwards, the value is blank and each entered character is being cleared.
Here is the code of the application. Why after adding new style to TextField its value changes?
public class VaadintestApplication extends Application {
#Override
public void init() {
Window mainWindow = new Window("Vaadintest Application");
setTheme("test");
TextField textField = new TextField("username");
textField.setEnabled(true);
textField.setTextChangeEventMode(TextChangeEventMode.EAGER);
textField.addListener(new TextChangeListener() {
public void textChange(TextChangeEvent event) {
if ("admin".equals(event.getText())) {
((TextField) event.getComponent()).addStyleName("text-error");
} else {
((TextField) event.getComponent()).removeStyleName("text-error");
}
}
});
mainWindow.addComponent(textField);
setMainWindow(mainWindow);
}
}
I would guess that the following happens:
The style name change triggers a repaint on the server, causing the TextField component to be serialized again to the client
The client receives the serialization (the whole bloody thing, not just the changed parts, because that's how things work with Vaadin), and hence it changes the contents of the textfield, while ignoring any changes that are pending from the text change listener
Solutions:
Update the value of the TextField at the same time you add/remove the style name: ((TextField) event.getComponent()).setValue(event.getText())
Create a custom client side widget which extends VTextField and add the functionality there

Resources