I've used addTextChangedListener(new TextWatcher()
to remove leading zeroes however it only works in android soft keyboard numpad
instead of my custom numpad
. n0
is the zero button. basically whenever i hit the zero button, my app crashes.
//custom zero button n0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { input1.append("0"); operationA(); } });
I just need the code that the zero button will not return any value when clicked to avoid leading zeros. Below is the addTextChangedListener(new TextWatcher()
input1.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s.toString().length() == 1 && s.toString().startsWith("0")) { s.clear(); } } });
Getting these error:
AndroidRuntime: FATAL EXCEPTION: main Process: context, PID: 12408 java.lang.NumberFormatException: empty String at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071) at java.lang.Double.valueOf(Double.java:511) at context.operationA(MainActivity.java:478) at context$17.onClick(MainActivity.java:383) at android.view.View.performClick(View.java:5610) at android.view.View$PerformClick.run(View.java:22265) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
the error occurred when you try to parse string as double
in operationA
method but the String is Empty ,check if string is not Empty before convert it