i wanna ask how do i set if my payment method is QR Payment i wanna show a QR code at the Enter cash field and and all the thing below payment method until the keypad will be hidden if it is QR Payment method. If it is Cash payment method it will show enter cash , CHANGE and the keypad.
https://imgur.com/a/nEUkgd2 This is the result i want , this picture is edited with paint not coded
PaymentActivity.java
package com.tankarjian.it212n.a2910assingment;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.tankarjian.it212n.a2910assingment.roomDb.database.MyDb;
import com.tankarjian.it212n.a2910assingment.roomDb.database.TransactionRecord;
import java.util.List;
public class PaymentActivity extends AppCompatActivity {
double totalPrice = 0;
double totalTax = 0;
private List<Product> productList;;
Button button0, button1, button2, button3, button4, button5, button6,
button7, button8, button9, buttondecimal, buttonclear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_activty);
TextView et_cash = (TextView) findViewById(R.id.et_cash);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttondecimal = (Button) findViewById(R.id.buttondecimal);
buttonclear = (Button) findViewById(R.id.buttonclear);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "9");
}
});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() "0");
}
});
buttonclear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText("");
}
});
buttondecimal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et_cash.setText(et_cash.getText() ".");
}
});
if (getIntent() != null) {
totalPrice = getIntent().getDoubleExtra("TOTAL_PRICE", 0);
totalTax = getIntent().getDoubleExtra("TOTAL_TAX", 0);
productList = (List<Product>) getIntent().getSerializableExtra("LIST");
}
TextView tvTotalAmount = findViewById(R.id.tv_total_amount);
tvTotalAmount.setText(String.valueOf(totalPrice totalTax));
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(PaymentActivity.this,
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.spinner));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(myAdapter);
TextView etCash = findViewById(R.id.et_cash);
findViewById(R.id.btn_calculate).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String cashString = etCash.getText().toString();
if (cashString != null && !cashString.isEmpty()) {
double cash = Double.parseDouble(cashString);
if (cash >= totalPrice) {
double payableAmount = cash - totalPrice - totalTax;
TextView tvPayable = findViewById(R.id.tv_return_amount);
tvPayable.setText(String.valueOf(payableAmount));
saveTransactionToDatabase();
} else {
Toast.makeText(PaymentActivity.this, "Please enter the cash more than amount", Toast.LENGTH_SHORT).show();
}
} else
Toast.makeText(PaymentActivity.this, "Please enter an amount", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btn_history).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(PaymentActivity.this, HistoryActivity.class));
}
});
}
private void saveTransactionToDatabase() {
long date = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
if (productList != null)
for (int i = 0; i < productList.size(); i ) {
sb.append(productList.get(i).name);
sb.append(" ,");
}
TransactionRecord transactionRecord = new TransactionRecord(sb.toString(), date, String.valueOf(totalPrice));
MyDb myDb = MyDb.getInstance(PaymentActivity.this);
myDb.myDao().addRecord(transactionRecord);
}
}
CodePudding user response:
Try This Answer :
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("qr payment method"))
{
// Keybord hide
} else {
// keybord show
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});