Skip to content

Commit 3f3b07e

Browse files
committed
Switched from a constraint layout to a series of parent/child linear layouts
1 parent 860ad7e commit 3f3b07e

File tree

5 files changed

+384
-307
lines changed

5 files changed

+384
-307
lines changed
Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,200 @@
11
package com.example.daymian.calculator;
22

3+
import android.databinding.DataBindingUtil;
34
import android.support.v7.app.AppCompatActivity;
45
import android.os.Bundle;
6+
import android.view.View;
7+
import com.example.daymian.calculator.databinding.ActivityMainBinding;
8+
import java.text.DecimalFormat;
59

610
public class MainActivity extends AppCompatActivity {
711

12+
private ActivityMainBinding binding;
13+
14+
private static final char ADDITION = '+';
15+
private static final char SUBTRACTION = '-';
16+
private static final char MULTIPLICATION = '*';
17+
private static final char DIVISION = '/';
18+
19+
private char CURRENT_ACTION;
20+
21+
private double valueOne = Double.NaN;
22+
private double valueTwo;
23+
24+
private DecimalFormat decimalFormat;
25+
826
@Override
927
protected void onCreate(Bundle savedInstanceState) {
1028
super.onCreate(savedInstanceState);
11-
setContentView(R.layout.activity_main);
29+
30+
decimalFormat = new DecimalFormat("#.##########");
31+
32+
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
33+
34+
binding.buttonDot.setOnClickListener(new View.OnClickListener() {
35+
@Override
36+
public void onClick(View view) {
37+
binding.editText.setText(binding.editText.getText() + ".");
38+
}
39+
});
40+
41+
binding.buttonZero.setOnClickListener(new View.OnClickListener() {
42+
@Override
43+
public void onClick(View view) {
44+
binding.editText.setText(binding.editText.getText() + "0");
45+
}
46+
});
47+
48+
binding.buttonOne.setOnClickListener(new View.OnClickListener() {
49+
@Override
50+
public void onClick(View view) {
51+
binding.editText.setText(binding.editText.getText() + "1");
52+
}
53+
});
54+
55+
binding.buttonTwo.setOnClickListener(new View.OnClickListener() {
56+
@Override
57+
public void onClick(View view) {
58+
binding.editText.setText(binding.editText.getText() + "2");
59+
}
60+
});
61+
62+
binding.buttonThree.setOnClickListener(new View.OnClickListener() {
63+
@Override
64+
public void onClick(View view) {
65+
binding.editText.setText(binding.editText.getText() + "3");
66+
}
67+
});
68+
69+
binding.buttonFour.setOnClickListener(new View.OnClickListener() {
70+
@Override
71+
public void onClick(View view) {
72+
binding.editText.setText(binding.editText.getText() + "4");
73+
}
74+
});
75+
76+
binding.buttonFive.setOnClickListener(new View.OnClickListener() {
77+
@Override
78+
public void onClick(View view) {
79+
binding.editText.setText(binding.editText.getText() + "5");
80+
}
81+
});
82+
83+
binding.buttonSix.setOnClickListener(new View.OnClickListener() {
84+
@Override
85+
public void onClick(View view) {
86+
binding.editText.setText(binding.editText.getText() + "6");
87+
}
88+
});
89+
90+
binding.buttonSeven.setOnClickListener(new View.OnClickListener() {
91+
@Override
92+
public void onClick(View view) {
93+
binding.editText.setText(binding.editText.getText() + "7");
94+
}
95+
});
96+
97+
binding.buttonEight.setOnClickListener(new View.OnClickListener() {
98+
@Override
99+
public void onClick(View view) {
100+
binding.editText.setText(binding.editText.getText() + "8");
101+
}
102+
});
103+
104+
binding.buttonNine.setOnClickListener(new View.OnClickListener() {
105+
@Override
106+
public void onClick(View view) {
107+
binding.editText.setText(binding.editText.getText() + "9");
108+
}
109+
});
110+
111+
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
112+
@Override
113+
public void onClick(View view) {
114+
computeCalculation();
115+
CURRENT_ACTION = ADDITION;
116+
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
117+
binding.editText.setText(null);
118+
}
119+
});
120+
121+
binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
122+
@Override
123+
public void onClick(View view) {
124+
computeCalculation();
125+
CURRENT_ACTION = SUBTRACTION;
126+
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
127+
binding.editText.setText(null);
128+
}
129+
});
130+
131+
binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
132+
@Override
133+
public void onClick(View view) {
134+
computeCalculation();
135+
CURRENT_ACTION = MULTIPLICATION;
136+
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
137+
binding.editText.setText(null);
138+
}
139+
});
140+
141+
binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
142+
@Override
143+
public void onClick(View view) {
144+
computeCalculation();
145+
CURRENT_ACTION = DIVISION;
146+
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
147+
binding.editText.setText(null);
148+
}
149+
});
150+
151+
binding.buttonEqual.setOnClickListener(new View.OnClickListener() {
152+
@Override
153+
public void onClick(View view) {
154+
computeCalculation();
155+
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
156+
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
157+
valueOne = Double.NaN;
158+
CURRENT_ACTION = '0';
159+
}
160+
});
161+
162+
binding.buttonClear.setOnClickListener(new View.OnClickListener() {
163+
@Override
164+
public void onClick(View view) {
165+
if(binding.editText.getText().length() > 0) {
166+
CharSequence currentText = binding.editText.getText();
167+
binding.editText.setText(currentText.subSequence(0, currentText.length()-1));
168+
}
169+
else {
170+
valueOne = Double.NaN;
171+
valueTwo = Double.NaN;
172+
binding.editText.setText("");
173+
binding.infoTextView.setText("");
174+
}
175+
}
176+
});
12177
}
13178

179+
private void computeCalculation() {
180+
if(!Double.isNaN(valueOne)) {
181+
valueTwo = Double.parseDouble(binding.editText.getText().toString());
182+
binding.editText.setText(null);
14183

184+
if(CURRENT_ACTION == ADDITION)
185+
valueOne = this.valueOne + valueTwo;
186+
else if(CURRENT_ACTION == SUBTRACTION)
187+
valueOne = this.valueOne - valueTwo;
188+
else if(CURRENT_ACTION == MULTIPLICATION)
189+
valueOne = this.valueOne * valueTwo;
190+
else if(CURRENT_ACTION == DIVISION)
191+
valueOne = this.valueOne / valueTwo;
192+
}
193+
else {
194+
try {
195+
valueOne = Double.parseDouble(binding.editText.getText().toString());
196+
}
197+
catch (Exception e){}
198+
}
199+
}
15200
}

app/src/main/res/drawable/my_button_bg.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
android:endColor="#aaa"
66
android:angle="0" />
77
<corners android:radius="3dp" />
8-
<stroke android:width="5px" android:color="darker_grey" />
8+
<stroke android:width="5px" />
99
</shape>

app/src/main/res/drawable/my_orange_button_bg.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
android:endColor="#FFA000"
66
android:angle="0" />
77
<corners android:radius="3dp" />
8-
<stroke android:width="5px" android:color="darker_grey" />
8+
<stroke android:width="5px"/>
99
</shape>

0 commit comments

Comments
 (0)