I am trying to display result of two user inputs-Number field but in vain. It should happen in real time, ie without any need to press any button, result should be displayed as field data changes with default value of it be 'Zero'. Here number1 and number2 are two inputs and grosstotal is result of number1+number2. Please help.
Update : I have put in code as suggested [by @Jacky] but app is crashing as soon as i try to input any number. Tried using debug [from the point I am doing input of data on emulator] and have put in its result too for it may be of any help. Kindly help.
MainActivity :
package com.ck.cg;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText getnumber1;
private EditText getnumber2;
private TextView displaygrossTotal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getnumber1 = (EditText) findViewById(R.id.number1);
getnumber2 = (EditText) findViewById(R.id.number2);
displaygrossTotal = (TextView) findViewById(R.id.grossTotal);
getQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
GetSum();
}
});
getRate.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
GetSum();
}
});
}
private void GetSum() {
int a = 0, b = 0, total;
if (getnumber1.getText() != null && !getnumber1.getText().toString().equals("")) {
try {
a = Integer.parseInt(getnumber1.getText().toString());
} catch (Exception e) {
a = 0;
}
}
if (getnumber2.getText() != null && !getnumber2.getText().toString().equals("")) {
try {
b = Integer.parseInt(getnumber2.getText().toString());
} catch (Exception e) {
b = 0;
}
}
total = a + b;
displaygrossTotal.setText(total);
}
}
xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ck.cg.MainActivity">
<EditText
android:id="@+id/number1"
android:labelFor="@+id/number1"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/number_1"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/number2"
android:labelFor="@+id/number2"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/number_2"
android:ems="10"
android:inputType="numberDecimal"
app:layout_constraintStart_toEndOf="@+id/number1"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/grossTotal"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/gross_total"
app:layout_constraintStart_toEndOf="@+id/number2"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="50dp"
android:layout_height="45dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="+"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Debug :
W/ResourceType: No package identifier when getting value for resource number 0x00000001
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ck.cg, PID: 6801
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:335)
at android.widget.TextView.setText(TextView.java:4555)
at com.ck.cg.MainActivity.GetSum(MainActivity.java:77)
at com.ck.cg.MainActivity.access$000(MainActivity.java:10)
at com.ck.cg.MainActivity$1.afterTextChanged(MainActivity.java:37)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8203)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10382)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1218)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:579)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:509)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:508)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
at android.widget.TextView.doKeyDown(TextView.java:6285)
at android.widget.TextView.onKeyDown(TextView.java:6075)
at android.view.KeyEvent.dispatch(KeyEvent.java:2688)
at android.view.View.dispatchKeyEvent(View.java:9960)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630)
at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:405)
at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1798)
at android.app.Activity.dispatchKeyEvent(Activity.java:3021)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:319)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4331)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6246)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6220)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6181)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3651)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)