how to calculate on real time? - android

I want to create calculator which have 2 edit text name input1 and input2 and have spinner which show the operand so I try
input1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
calculate();
return false;
}
});
but it's not on time , I have to press back space 1 time in any edittext to calculate it
I think the problem is "onKey" function
Any ideas?

It sounds like you want the calculations to happen on the fly, and you don't have need for a button.
If that is true, you could attach a TextWatcher to each EditText via the addTextChangedListener(TextWatcher) method. Whenever you receive an event from it, you can call calculate(). In this instance you would probably also want to attach an AdapterView.OnItemSelectedListener to your Spinner.
If you ARE using a button to do your calculations, you would simply have a button, give it an OnClickListener which would call your calculate method.

Related

Overriding setOnKeyListener for all EditTexts in my Android app?

I am attempting to ignore the input from any connected physical/hardware keyboard while still allowing soft-keyboard input.
By using the example in this answer I am able to accomplish what I need, but I must find and add that override to every EditText I have placed in my project.
I was wondering if it is possible to override the setOnKeyListener method for all EditTexts in an activity or the entire app?
Create a global variable that would hold your listener
View.OnKeyListener keyListener = new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
return false;
}
};
Then just set it to all your editText
editText1.setOnKeyListener(keyListener);
editText2.setOnKeyListener(keyListener);
Hope this helps
EDIT:
You could also use an injector. ButterKnife for example
#BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;
By this you library you dont have to do findViewById() and the best thing is you justcan achieve your desired things by just 1 line
nameViews.setOnKeyListener(keyListener);
The above code will set keyListener to 3 EditText

How to prevent a key to be inserted in an EditText if it comes from a specific keyboard

i'm working with a bluetooth barcode scanner acting like a bluetooth keyboard, i want to handle the keys when my user scan something but i know he can focus any EditText in the activity so i want to detect which keyboard he uses in my KeyEvent like this :
if KeyEvent.getDevice().getName().equals("Datalogic Scanner") //do stuff
else //let the edittext add the letter
and prevent the EditText to be edited and instead store and process the values the scanner send.
I tried returning true in dispatchKeyEvent in my activity,
I tried returning true in onKeyDown in my activity,
I tried returning true in a OnKeyListener on my EditText and overriding OnKeyDown on my EditText class, but nothing works, the text still get inserted in my EditText.
Any idea ?
Try to use the dispatchKeyEvent like that :
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
return scanManager.handleDispatchKeyEvent(event);
}
And in the scanManager class, you should do something like that :
public boolean handleDispatchKeyEvent(KeyEvent event) {
// handle the event
return true;
}

Prevent ' Enter key ' to trigger TextWatcher

I was making an application to act as remote keyboard where user will click on an edittext and type and corresponding alphabets will b typed in computer.
I have detected variaous alphabets and numbers with the help of TextWatcher and sent them to my server successfully.
Problem comes when user presses enter key. This also triggers TextWatcher . and since i am sending the latest entered changes , error shows up on server side.
As a solution what i did is , set one onkeylistener as well which will detect the enter key and perform action and CONSUME it , but unfortunately in that case also first textwatcher gets triggered and then onkeylistener.
Here is the code of my onkeylistener
keyboard.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER&&event.getAction()==KeyEvent.ACTION_UP){
out.println("enter");
return true;
}
return false;
}
});
Code of TextWatcher :
private TextWatcher watcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count>0){
out.println(":"+s.subSequence(start, start+count).toString());
}
}
.
.
.
Problem is on pressing enter key it fires TextWatcher's onTextChanged as well as onkeylistener. whereas i want only on key listener to fire.
I want to fire Textwatcher only in case of alphabets , numbers , some symbols etc.
Also if you can suggest different approach for detecting and sending characters ( soft ) will be great .
Ok i think i solved it. in Textwatcher add this if statement:
if(s.toString().substring(start).contains("\n"))
in that way if last key entered was 'enter' then it would go into this is and then u can perform whatever u want.

How to watch multiple edittext boxes for a softkey enter or done press?

I have 14 edittext boxes that a user can change at will. When the text is changed in one of them, a softpad key press on the 'enter/next/done' key should re-run a calculation using the new text. I've tried onKey listener but it doesn't work on the soft keyboard, on;y the hard keypad. I've tried a textwatcher like onTextChanged but it reacts and runs the calculation when only a single digit is entered, before the user can input a two or more digit number. So... I hear the onEditorActionListener works on soft keypads to watch for the keypress, but I can't get the syntax right. Here's what I have:
In the onCreate method:
myEdittext1.setOnEditorActionListener(this);
...
myEdittext14.setOnEditorActionListener(this);
Then, outside the onCreate method, I have:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{ if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
{ //do my calcs}
}
return(true);
}
The code gives me the foreced-close business. Any help would be greatly appreciated.
KeyEvent can be null. You need to check for this situation in your listener.
v The view that was clicked.
actionId Identifier of the action. This will be either the identifier you supplied, or EditorInfo.IME_NULL if being called due to the enter key being pressed.
event If triggered by an enter key, this is the event; otherwise, this is null.
onEditorAction is the appropriate way to listen for finish and Done soft keyboard actions.
so here's a working version:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{ if (event == null || event.getAction()==KeyEvent.ACTION_UP)
{ //do my calcs
return(true);//reset key event for next press
}
}
The enter key returns a 'null' in the event variable, thats why my version at the top of this post crashed.
Nick's code above worked well too, just remember to set the xml property android:imeOptions=actionDone" for all of the edittext boxes.

How do I make my Calculate button redundant?

I have a simple Android app that does some math on 2 numbers that the user inputs and returns the result.
Currently I have a 'calculate' button that needs to be pressed to do the math and return the value.
How can I get rid of this button and just get the app to run the math after the uses has changed either one of the 2 numbers?
Many thanks.
Assuming that you are using EditText-s for the input (because they could be SeekBar-s, who knows), add on each one of them a TextWatcher with this and after each change of one of them refresh the result
I used something like this for an EditText field that I wanted to be auto-updated after changing some other EditText fields:
myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE)
{
calculateNewValue(); // Updates internal variables
// This part will hide the keyboard after input
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
});
This also hides the keyboard when you're done (thanks to a bit of help).
The calculateNewValue() updated the field like this:
private void calculateNewValue()
{
val = YourFormula()
myEditText.setText(String.format(yourFormat, val))
}
The details depend on what language you are using, but basically you could just run the calculation function whenever either one of the numbers is changed. There should be a way to override the function that is called when the data in either text box has changed.
Well, if both numbers start off with 0, you can either check to make sure neither or 0 or trigger the math when both text fields have received text changed events. I'm not sure what platform you are using so I can't give you much more info.

Categories

Resources