I have an app where I have to calculate 2 sums with a discounted rate. eg:200 50 - 10% the result should be shown automatically on the other textView without clicking any submit button I have done the addition part by textWatcher method. But I am not able to do the percentage part This is My code.
My Xml Code:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/productPriceEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Product Price"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/deliveryChargesEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Delivery Charges"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/discountEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Discount"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textview.MaterialTextView
android:id="@ id/prbtTv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:background="@drawable/textview_border"
android:gravity="center_vertical"
android:padding="15dp"
android:text="Price Before Taxes"
android:textSize="16sp">
</com.google.android.material.textview.MaterialTextView>
My Java Code :
TextWatcher textWatcher = 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) {
if (!productPriceEt.getText().toString().equals("") &&
!deliveryChargeEt.getText().toString().equals("") &&
!discountEt.getText().toString().equals("")){
int temp1 = Integer.parseInt(productPriceEt.getText().toString());
int temp2 = Integer.parseInt(deliveryChargeEt.getText().toString());
int temp3 = Integer.parseInt(discountEt.getText().toString());
prbTv.setText(String.valueOf(temp1 temp2 - temp3));
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
productPriceEt.addTextChangedListener(textWatcher);
deliveryChargeEt.addTextChangedListener(textWatcher);
discountEt.addTextChangedListener(textWatcher);
CodePudding user response:
just save the result of the sum in a variable first like this:
private val sum = temp1 temp2
then calculate the off like this:
private val off = (temp3/100) * sum
and finally set text on text view like this:
prbTv.setText(String.valueOf(sum - off ));
CodePudding user response:
Try this
Xml File
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/productPriceEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Product Price"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/deliveryChargesEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Delivery Charges"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/discountEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Discount"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textview.MaterialTextView
android:id="@ id/prbtTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="3dp"
android:gravity="center_vertical"
android:padding="15dp"
android:text="Price Before Taxes"
android:textSize="16sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
Code
public class MainActivity extends AppCompatActivity {
TextInputEditText productPriceEt, deliveryChargesEt, discountEt;
MaterialTextView prbtTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
productPriceEt = findViewById(R.id.productPriceEt);
deliveryChargesEt = findViewById(R.id.deliveryChargesEt);
discountEt = findViewById(R.id.discountEt);
prbtTv = findViewById(R.id.prbtTv);
productPriceEt.addTextChangedListener(textWatcher);
deliveryChargesEt.addTextChangedListener(textWatcher);
discountEt.addTextChangedListener(textWatcher);
}
private final TextWatcher textWatcher = 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) {
if (!productPriceEt.getText().toString().equals("") &&
!deliveryChargesEt.getText().toString().equals("") &&
!discountEt.getText().toString().equals("")) {
int amounts = Integer.parseInt(productPriceEt.getText().toString().trim())
Integer.parseInt(deliveryChargesEt.getText().toString().trim());
double amount = Double.parseDouble(String.valueOf(amounts));
double res = (amount / 100.0f) * Double.parseDouble(discountEt.getText().toString());
res = amount - res;
prbtTv.setText("Price Before Taxes- " amount "\nPrice After Discount - " res);
Toast.makeText(getApplicationContext(), "" res, Toast.LENGTH_SHORT).show();
} else {
prbtTv.setText("Price Before Taxes- " 0.00 "\nPrice After Discount - " 0.00);
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
}