Execute function at soft key enter - android

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;
}
}
});

Related

EditText is cleared after pressing done & is acting weird

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.

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

How to word wrap the hint of a single line AutoCompleteTextView?

I have an AutoCompleteTextView where the user is to write in a single-line search, i.e. no line breaks are allowed. Using android:singleLine="true" allows this to work properly.
However, I also wish to set a hint to display when no other text has been entered, and this text does require more than one line. My problem is that with Android 4+, the hint is not wrapped to several lines. With Android 2.3, however, the content does get wrapped and multiple lines are displayed. Users are still not able enter more than one line, so this is the way I want it.
Using for example android:maxLines="2" does not help since that allows the user to insert a line break when searching. I've also tried android:scrollHorizontally="false" and android:ellipsize="none" and android:layout_weight=1 without success.
Any ideas on how I can get the hint to wrap multiple lines and still only accept a single line from users with Android 4 as well? Below is the code I'm using at the moment.
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:completionThreshold="3"
android:hint="This is the hint to be displayed. It's rather long and doesn't fit in one line"
android:singleLine="true" />
Try changing singleLine to:
android:lines="1"
And now to disable "Enter":
EDIT_TEXT.setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
return true;
}
return false;
}
});
Try adding an OnFocusChangeListener to change the value of singleLine or maxLines:
autoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange (View v, boolean hasFocus) {
// This might do it on its own
((AutoCompleteTextView) v).setSingleLine(hasFocus);
// If setSingleLine() doesn't work try this
AutoCompleteTextView auto = (AutoCompleteTextView) v;
if(hasFocus)
auto.setMaxLines(1);
else
auto.setMaxLines(2); // or more if necessary
// Only keep the one that works, you obviously don't need both!
}
});

EditText is not taking numeric input

I have an EditText view in my android application. My EditText is not taking integer values as an input however it is taking all the other ones. the following is my EditText View.
<EditText
android:id="#+id/txtSearchItems"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:hint="Search"
android:textSize="14dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true">
</EditText>
On the possible issue that i faced once and suspects is: The EditText might be contained in an XML layout file and the layout file will be used for a Dialog instance. The problem occurs when the onKeyListener of the Dialog instance is returning some invalid default value i.e. 'return true' for irrelevant keys. In such case your EditText will be behaving strangely. The sample code for the scenario is
Dialog d=new Dialog(context);
d.setContentView(R.layout.mylayout);
d.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
Log.i("onkey", keyCode + " == Onkey");
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return true; // !!! this is wrong it should be return false;
}
});
}
Try this?
android:inputType="number"
android:digits="0123456789"
If it works, you can add letters in with the numbers in digits that you want to allow. Here's a handy list of all the inputTypes you could try. They mostly change what appears on the soft keyboard.
You need to add android:inputType="number" in your xml just like follows,
<EditText
android:id="#+id/txtSearchItems"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:hint="Search"
android:textSize="14dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:inputType="number"> <------------- this line added
</EditText>

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