-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
55 lines (49 loc) · 2.01 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.example.f55123084;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText edtWidth, edtHeight, edtLength;
private Button btnCalculate;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtWidth = findViewById(R.id.edt_width);
edtHeight = findViewById(R.id.edt_height);
edtLength = findViewById(R.id.edt_length);
btnCalculate = findViewById(R.id.btn_calculate);
tvResult = findViewById(R.id.tv_result);
btnCalculate.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_calculate) {
String inputLength = edtLength.getText().toString().trim();
String inputWidth = edtWidth.getText().toString().trim();
String inputHeight = edtHeight.getText().toString().trim();
boolean isEmptyFields = false;
if (TextUtils.isEmpty(inputLength)) {
isEmptyFields = true;
edtLength.setError("Field ini tidak boleh kosong");
}
if (TextUtils.isEmpty(inputWidth)) {
isEmptyFields = true;
edtWidth.setError("Field ini tidak boleh kosong");
}
if (TextUtils.isEmpty(inputHeight)) {
isEmptyFields = true;
edtHeight.setError("Field ini tidak boleh kosong");
}
if (!isEmptyFields) {
double volume = Double.parseDouble(inputLength) * Double.parseDouble(inputWidth) * Double.parseDouble(inputHeight);
tvResult.setText(String.valueOf(volume));
}
}
}
}