I have searched to find a solution to this problem without anyluck.
I have a simpe android app that the user enters a number into one editText (@+id/incBox) field and a given percentage of the that number is automatically placed into another editText (@+id/excBox).
I have implemented a setOnKeyListener for both editText fields, that gets the entered number and does the calculation to update the other field (vice versa).
This code works fine in the emulator everytime a digit is entered the other editText field is updated . However when running the apk on my samsung galaxy s2 the other field is not updated. For the other field to be updated on the phone you have press Enter on the softkeyboard. What am I missing here? I have even removed the event.getAction() == KeyEvent.ACTION_UP “if” clause to ensure no CANCEL or MULTITOUCH events affect the OnKey listener. How do I get to this to work on the phone?
Another problem that I am experiencing is that after entering a value into a field. Moving to the other field and deleting or pressing enter is quite laggy. Sometimes there is a 0.5 second delay when pressing the backspace button to delete a digit in an already populated field. Is this because of the try catch?
Any help would be appreciated.
this is the main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="top"
android:background="@drawable/bground"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/inclusive"
android:textSize="20px"
android:paddingTop="10px"
android:textStyle="bold" />
<EditText
android:id="@+id/incBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="25px"
android:layout_marginRight="25px"
android:singleLine="true"
>
<requestFocus />
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px"
android:textStyle="bold"
android:gravity="center_horizontal"
android:text="@string/exclusive" />
<EditText
android:id="@+id/excBox"
android:layout_width="fill_parent"
android:layout_height="60px"
android:inputType="numberDecimal"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="25px"
android:layout_marginRight="25px"
android:singleLine="true"
>
</EditText>
</LinearLayout>
and here is the activity.java
public class PercentageCalculatorActivity extends Activity
{
private EditText inclusive;
private EditText exclusive;
DecimalFormat cost = new DecimalFormat("0.00");
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set Activity Layout
setContentView(R.layout.main);
// Get the EditText and Button References
inclusive = (EditText)findViewById(R.id.incBox);
exclusive = (EditText)findViewById(R.id.excBox);
//Set KeyListener to ourself
inclusive.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
try
{
double num = Double.parseDouble( inclusive.getText().toString() );
if ( num > 0)
{
num = num * 0.25;
String exc = cost.format(num).toString();
exclusive.setText(exc);
}
// Close the keyboard on enter press if ( keyCode == 66 ) {
InputMethodManager imm = (InputMethodManager)getSystemService (Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inclusive.getWindowToken(), 0);
}
return false;
}
catch (Throwable e)
{
// set other field to empty if this field is also blank
exclusive.setText("");
return false;
}
}
});
exclusive.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction()==KeyEvent.ACTION_UP )
{
try
{
double num = Double.parseDouble( exclusive.getText().toString() );
if ( num > 0)
{
num = num * 4;
String exc = cost.format(num).toString();
inclusive.setText(exc);
}
if ( keyCode == 66 )
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(exclusive.getWindowToken(), 0);
}
return false;
}
catch (Throwable e)
{
// set other field to empty if this field is also blank
inclusive.setText("");
return false;
}
}
else
{
return false;
}
}
});
It actually depends not on the device but on the keyboard you select. I’ve run into this before. If you swap out the keyboard for a different one, you’ll find that suddenly it starts reacting as you expect. I’m not sure what the cause of this is, but you should give that a try and see if that solves your issue.