EditText is cleared after pressing done & is acting weird - android

Attached to my EditText is a new TextView.OnEditorActionListener()
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(i == EditorInfo.IME_ACTION_DONE){
dataSnapshot.child("add").child(child.getKey()).child("comment").getRef().setValue(String.valueOf(serviceComment.getText()));
return true;
}else{
return false;
}
}
After execution, the text inside my EditText is cleared when I want it to persist.
Additionally, after it is cleared, and I type in the same exact thing, the code doesn't execute and the keyboard doesn't disappear until I type in something different from what I previously typed.
My ultimate goal is to have the text within my EditText to persist after clicking done and hiding the keyboard.
Here is the XML for my EditText
<EditText
android:id="#+id/txtComment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorSecondary"
android:hint="Enter a comment"
android:imeOptions="actionDone"
android:maxLength="200"
android:maxLines="1"
android:singleLine="true"
android:textColor="#color/colorBlack"
android:textSize="14sp" />

To hide the keyboard you have to perform this inside the listener callback:
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
Please try it.
Also, the Edit Text clear issue might be due to the line:
dataSnapshot.child("add").child(child.getKey()).child("comment").getRef().setValue(String.valueOf(serviceComment.getText()));
This line might be causing UI Hang and can be the reason.
Try calling that operation in another thread.

Related

How to get "Done" to show and exit keyboard on AutoCompleteTextiView in Android?

My problem: I'm using an AutoCompleteTextView list of options for the user and despite having the activity xml setup with android:imeOptions="actionDone" the keyboard still provides a return option instead presenting the keyboard with the "Done" option.
What I have tried: I searched a few posts but could not find one with this specific issue. So I tried the following solution posted from another post applicable for EditText, so I could apply this "Done" action to multiple AutoCompleteTextViews within the same activity (posted here).
Again, the issue is that despite the XML AutoCompleteTextView is setup for actionDone the keyboard shows the return arrow.
XML
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/viewSource"
android:hint="#string/hint_source"
android:imeOptions="actionDone"
android:padding="5dp"
android:background="#color/colorWhite"
app:layout_constraintStart_toEndOf="#id/lblViewSource"
android:layout_marginStart="5dp"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="12sp"
android:layout_marginEnd="5dp"
app:layout_constraintBaseline_toBaselineOf="#+id/lblViewSource"/>
The AutoCompleteTextView calling code
sourceTitle.setOnEditorActionListener(new DoneOnEditorActionListener());
Custom Class Code for managing "Done" and keyboard close
class DoneOnEditorActionListener implements AutoCompleteTextView.OnEditorActionListener {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
}
The following ended up solving my problem. Once I added and defined the inputType in the activity XML the done button appeared. So, it appears that the inputType needs defined with the imeOptions or the done button will not appear. This was one of the suggestions from the following link though none of answers were approved.
Done is not working in softKeyboard in Autocomplete TextView in android
android:inputType="text"
android:imeOptions="actionDone"

Trying to make the soft keyboard enter button say search or display search magnifying glass and handle its click for an EditText

I have tried the highly popular answer suggested here
Although this hasn't worked for me like it seems to have for the other users. Perhaps it is outdated or I have something conflicting in my code.
Here is the xml for my EditText:
<EditText
android:id="#+id/editText_letter_entry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#drawable/edit_text_rounded"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
android:drawableEnd="#drawable/ic_search_black_24dp"
android:hint="#string/enter_letters"
android:importantForAutofill="no"
android:inputType="text|textNoSuggestions|textVisiblePassword"
android:maxLength="15"
android:maxLines="1"
android:imeOptions="actionSearch"
android:paddingLeft="#dimen/et_padding_left_right"
android:paddingRight="#dimen/et_padding_left_right"
app:layout_constraintBottom_toTopOf="#+id/bottomBannerAdView"
app:layout_constraintEnd_toStartOf="#+id/guideline2Container"
app:layout_constraintStart_toStartOf="#+id/guideline1Container"
app:layout_constraintTop_toTopOf="parent" />
As you can see I have the android:imeOptions="search" & android:inputType="text" along with textNoSuggestions & textVisiblePassword as the inputType. I have tried this with only text as the inputType like the other answer suggested but this didn't make any difference and I don't want any suggestions on the keyboard.
As for this listener I managed to get that working although in a bit of a hacky way because the action id is coming back as IME_ACTION_UNSPECIFIED, So I just handled this instead. This can be seen here:
et_letter_entry.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED ) {
preformSearch();
return true;
}
return false;
}
});
I've currently got an icon for a new line rather than search or a search icon.
Why doesn't the xml seem to be affected by android:imeOptions="search" in my case?
Why is the enter button displaying a new line icon?
Why is the action id returning as unspecified?
Thanks
Remove
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
from your EditText and it will fix your problem.
The conflict was due to the android:digits is set. In this situation android:inputType is set to numeric by default disregarding what you entered there(android:inputType="text|textNoSuggestions|textVisiblePassword") in your xml, thus making your android:imeOptions="actionSearch" irrelevant.
Hope it helps.

Execute function at soft key enter

I'm trying to add a way to execute a function on a soft key enter (or whatever the bottom right hand key would be, I assume its usually a enter/done key) once a edit text field has been filled in with numbers. I also have a calculate button that I would like to keep as a back up in an attempt to 'idiot proof' the app a little bit. below is a snippet of my code thus far; this part is working:
onCalculate refers to a button I have.
I have error checking for null values and whatever else.
EditText something
public void onCalculate (View v){
do stuff....
}
I want to add something down here to 'do stuff' in the event the user presses the done/enter/bottom right hand soft key instead of pressing the button. Below is a snippet from my layout XML:
<EditText
android:id="#+id/editText1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="numberDecimal"
android:textSize="15sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_margin/>
I know I probably need to create some kind of key listener for the enter key, but I'm not sure how to go about it.
You can achieve this by associating an OnEditorActionListener to the EditText. For example:
editText1.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText1.setOnEditorActionListener(new new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE)
{
onCalculate(editText1);
return true;
}
}
});

How to add "Next" on android keyboard

I've seen in some applications that in the keyboard appears a Button called next that puts the focus on the next Edit Text. I want to add this in my application, Do you know how I can do it? Or it's only on the application's keyboard? Thanks a lot.
Sorry, I haven't more information about this.
In the layout of edittext add android:imeOptions attribute. Here you can add different action buttons like Go, Search , Send , Next, Done etc. But to use this the EditText has to be a singleLine Else there will be an enter button there. So also use the android:singleLine="true".
So here is your solution
<EditText
android:id=//put ur id
android:layout_width=//according to need
android:layout_height=//accordintoneed
android:imeOptions="actionNext"
android:singleLine="true"/>
EDIT
for addition i should add some more for future use or for the help of others..
You can handle the imeoption actions from your code. for that you have to set setOnEditorActionListener to the Edittext and when a action is pressed you can get it on public boolean onEditorAction(TextView v, int actionId, KeyEvent event) method. and if you not handle the code by yourself the imeoptions action will do the default action (usually going to next control).
Here is an example code for handling the action
EditText e = (EditText)findViewById(R.id.editText1);
e.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT ) {
// handle next button
return true;
}
return false;
}
});
You just have to use the imeOptions tag in your layout. See imeOptions in docu.
<EditText
android:id="#+id/someField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"/>
You have to add these two line given below in your EditText
android:imeOptions="actionNext"
android:singleLine="true"
i tried all those answers posted here but none actually work. gladly I found myself a solution for this.
<EditText
android:id="#+id/etRegisterFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Firstname"
android:imeOptions="actionNext"
android:inputType="textCapWords"
android:maxLines="1" />
<EditText
android:id="#+id/etRegisterLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Lastname"
android:imeOptions="actionNext"
android:inputType="textCapWords"
android:maxLines="1" />
Please do notice that I ADD android:inputType. Thats it .
Cheers / Happy coding

AutocompleteTextView: on "NEXT" highlight next TextView, on "DONE", make keyboard disappear

I have two AutocompleTextViews and I want to switch to the next if the user presses "NEXT", and make the virtual keyboard disappear when he hits "DONE" at the second AutocompleTextView. So far, the buttons "NEXT"/"DONE" do nothing at all.... Unfortunately I found no resources addressing this problem.
Any suggestions?
thx
EDIT: Just want to add that this was asked when Android was on version 2.3 or something like that.
I ran into this problem and fixed it by setting the imeOptions on the AutocompleteTextView to actionNext.
Example:
<AutoCompleteTextView
android:id="#+id/dialog_product_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:completionThreshold="1"
android:imeOptions="actionNext"
/>
I found this solution for the "NEXT" problem: in your View source code write something like this
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (firstAutoComplete.hasFocus()) {
// sends focus to another field (user pressed "Next")
otherText.requestFocus();
return true;
}
else if (secondAutoComplete.hasFocus()) {
// sends focus to another field (user pressed "Next")
anotherText.requestFocus();
return true;
}
}
return false;
}
It seems to be an old Android http://code.google.com/p/android/issues/detail?id=4208.
Here I found my solution: http://groups.google.com/group/android-developers/browse_thread/thread/e53e40bfe255ecaf.
Just add these two lines into AutoCompleteTextView xml Code:
android:completionThreshold="1"
android:imeOptions="actionNext"
Don't forget to add inputType="text" otherwise you'll have enter button instead of next/done one:
<AutoCompleteTextView
android:id="#+id/suppliers"
android:layout_width="#dimen/summary_input_width"
android:layout_height="wrap_content"
android:hint="#string/current_supplier"
android:imeOptions="actionNext"
android:inputType="text"
android:lines="1" />

Categories

Resources