Facebook
From Ample Sheep, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 127
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  2.  
  3.         EditText edtWidth, edtHeight, edtLength;
  4.         Button btnCalculate;
  5.         TextView tvResult;
  6.  
  7.         @Override
  8.         protected void onCreate (Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.  
  12.         edtWidth = findViewById(R.id.edt_width);
  13.         edtHeight = findViewById(R.id.edt_height);
  14.         edtLength = findViewById(R.id.edt_length);
  15.         btnCalculate = findViewById(R.id.btn_calculate);
  16.         tvResult = findViewById(R.id.tv_result);
  17.  
  18.         btnCalculate.setOnClickListener(this);
  19.  
  20.         }
  21.  
  22.     @Override
  23.     public void onClick(View v) {
  24.         if (v.getId() == R.id.btn_calculate){
  25.             String inputLength = edtLength.getText().toString().trim();
  26.             String inputWidth = edtWidth.getText().toString().trim();
  27.             String inputHeight = edtHeight.getText().toString().trim();
  28.  
  29.             boolean isEmptyFields = false;
  30.             boolean isInvalidDouble = false;
  31.  
  32.             if (TextUtils.isEmpty(inputLength)){
  33.                 isEmptyFields = true;
  34.                 edtLength.setError("Field Ini Tidak Boleh Kosong"
  35.                 );
  36.             }
  37.             if (TextUtils.isEmpty(inputWidth)){
  38.                 isEmptyFields = true;
  39.                 edtWidth.setError("Field Ini Tidak Boleh Kosong");
  40.             }
  41.             if (TextUtils.isEmpty(inputHeight)){
  42.                 isEmptyFields = true;
  43.                 edtHeight.setError("Field Ini Tidak Boleh Kosong");
  44.             }
  45.  
  46.             Double length = toDouble (inputLength);
  47.             Double width  = toDouble (inputWidth);
  48.             Double height = toDouble (inputHeight);
  49.  
  50.             if (length == null) {
  51.                 isInvalidDouble = true;
  52.                 edtLength.setError("Field ini harus berupa nomer yang valid");
  53.             }
  54.  
  55.             if (width == null) {
  56.                 isInvalidDouble = true;
  57.                 edtWidth.setError("Field ini harus berupa nomer yang valid");
  58.             }
  59.  
  60.             if (height == null) {
  61.                 isInvalidDouble = true;
  62.                 edtHeight.setError("Field ini harus berupa nomer yang valid");
  63.             }
  64.  
  65.             if (!isEmptyFields && !isInvalidDouble) {
  66.  
  67.                 double volume = length * width * height;
  68.  
  69.                 tvResult.setText(String.valueOf(volume));
  70.             }
  71.         }
  72.     }
  73.  
  74.     private Double toDouble(String str) {
  75.         try {
  76.             return Double.valueOf(str);
  77.         } catch (NumberFormatException e) {
  78.             return null;
  79.         }
  80.     }
  81. }
  82.