I am very new too Android. I try to develop an android application to get the total amount of user input items. Here is a sketch of my application.enter image description here
User Must enter First Row Col1 and Col 2. But First Row Col3 can enter or not. In Sub 1 textview the total value of First Row should display. This value should also display in Result textview. Likewise if user inserts data to Second Row, values must enter to under Col1 and Col2. If user likes, can enter value to under Col 3. Second Row total value Should display in Sub 2 textview. If User Enter Values to Second Row, then the Result textview should automatically update to the total value of Sub 1 and Sub 2 textviews. I try to do this as follows, But I cannot find a correct way to do this. Please if someone helps me to solve this, I really thankful. This is the java class
public class AddTwo extends AppCompatActivity {
EditText edit1, edit2, edit3;
EditText edit4, edit5, edit6;
TextView textViewSub1, textViewSub2, textViewResult;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_add_two);
/*First row variables*/
edit1 = (EditText) findViewById(R.id.editText1);
edit2 = (EditText) findViewById(R.id.editText2);
edit3 = (EditText) findViewById(R.id.editText3);
textViewSub1 = (TextView) findViewById(R.id.TextViewsub1);
/*Second row variables*/
edit4 = (EditText) findViewById(R.id.editText5);
edit5 = (EditText) findViewById(R.id.editText6);
edit6 = (EditText) findViewById(R.id.editText7);
textViewSub2 = (TextView) findViewById(R.id.TextViewsub2);
/*Final Total TextView variable*/
textViewResult = (TextView) findViewById(R.id.textView_result);
//use TextWatcher for edit1 in First Row Col1
edit1.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
//use TextWatcher for first First Row Col2
edit2.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//textViewResult.setText(addNumbers());
}
});
//use TextWatcher for first First Row Col3
edit3.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 editable) {
textViewResult.setText(addNumbers());
}
});
}
//Define method addString()
private String addNumbers() {
//declaring variables
int number1, number2;
double num1, num2;
double d1, d2;
double da1, da2;
Double orderTotal1 = null, orderTotal2 = null;
Double ordTot1 = null, ordTot2 = null;
DecimalFormat df = new DecimalFormat("0.00##");
//For First Row
/*if only the first and second edittext fields in First Row is filled*/
if ((edit1.getText().toString() != "" && edit1.getText().length() > 0) && (edit2.getText().toString() != "" && edit2.getText().length() > 0)) {
num1 = Double.parseDouble((edit1.getText().toString()));
number1 = Integer.parseInt(edit2.getText().toString());
ordTot1 = num1 * number1;
textViewSub1.setText(df.format(ordTot1));
/*else if the first,secondn third (all) edittext fields in First Row is filled*/
} else if ((edit1.getText().toString() != "" && edit1.getText().length() > 0) && (edit2.getText().toString() != "" && edit2.getText().length() > 0) && (edit3.getText().toString() != "" && edit3.getText().length() > 0)) {
num1 = Double.parseDouble((edit1.getText().toString()));
number1 = Integer.parseInt(edit2.getText().toString());
d1 = Double.parseDouble(edit3.getText().toString());
da1 = 100 - d1;
orderTotal1 = ((da1 * number1) * num1) / 100;
textViewSub1.setText(df.format(orderTotal1));
} else {
number1 = (int) 0.00;
num1 = 0.00;
da1 = 0.00;
}
//For the Second Row
/*if only the first and second edittext fields in Second Row is filled*/
if ((edit4.getText().toString() != "" && edit4.getText().length() > 0) && (edit5.getText().toString() != "" && edit5.getText().length() > 0)) {
num2 = Double.parseDouble((edit4.getText().toString()));
number2 = Integer.parseInt(edit5.getText().toString());
ordTot2 = num2 * number2;
textViewSub2.setText(df.format(ordTot2));
/*else if the first,secondn third (all) edittext fields in Second Row is filled*/
} else if ((edit4.getText().toString() != "" && edit4.getText().length() > 0) && (edit5.getText().toString() != "" && edit5.getText().length() > 0) && (edit6.getText().toString() != "" && edit6.getText().length() > 0)) {
num2 = Double.parseDouble((edit1.getText().toString()));
number2 = Integer.parseInt(edit2.getText().toString());
d2 = Double.parseDouble(edit3.getText().toString());
da2 = 100 - d2;
orderTotal2 = ((da2 * number2) * num2) / 100;
textViewSub2.setText(df.format(orderTotal2));
} else {
number2 = (int) 0.00;
num2 = 0.00;
da2 = 0.00;
}
//return statement
return df.format((orderTotal1) + (orderTotal2));
}
}
Hope this help.