-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.Java
395 lines (322 loc) · 14.3 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package com.coreymalbright.mycalculator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
//Additional imports
import android.view.View; //Whenever you need to use widgets
import android.widget.Button; //Using Buttons
import android.widget.TextView; //Using Numbers view
public class MainActivity extends AppCompatActivity {
public String sign = ""; //This method has to return a String at some point,
// otherwise you will receive an error (StackTrace will be asked for) " 8:26
public String total = "";
public Double tempDouble, tempDouble2; //holds the first number, and the second number after the sign is pushed
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Buttons 1 - 0
Button button1 = (Button)findViewById(R.id.button1);
//reference our buttons using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically
Button button2 = (Button)findViewById(R.id.button2); //reference our buttons
Button button3 = (Button)findViewById(R.id.button3); //reference our buttons
Button button4 = (Button)findViewById(R.id.button4); //reference our buttons
Button button5 = (Button)findViewById(R.id.button5); //reference our buttons
Button button6 = (Button)findViewById(R.id.button6); //reference our buttons
Button button7 = (Button)findViewById(R.id.button7); //reference our buttons
Button button8 = (Button)findViewById(R.id.button8); //reference our buttons
Button button9 = (Button)findViewById(R.id.button9); //reference our buttons
Button button10 = (Button)findViewById(R.id.button10); //reference our buttons
//Clear and Close Buttons
Button button11 = (Button)findViewById(R.id.button11); //reference our buttons
Button button12 = (Button)findViewById(R.id.button12); //reference our buttons
//operators (add, subtract, multiply, and divide)
Button button13 = (Button)findViewById(R.id.button13); //reference our buttons
Button button14 = (Button)findViewById(R.id.button14); //reference our buttons
Button button15 = (Button)findViewById(R.id.button15); //reference our buttons
Button button16 = (Button)findViewById(R.id.button16); //reference our buttons
//equals
Button button17 = (Button)findViewById(R.id.button17); //reference our buttons
// Button 1 event handler "the one button"
button1.setOnClickListener( //this makes it listen to the click for it to open
//Button 1 Interface
new Button.OnClickListener() //It is basically a passive data structure holding an abstract description of an action to be performed.
{
//button 1 call back method
public void onClick(View v) //The view that was click (A button, A TextView, etc.)
{
TextView output = (TextView)findViewById(R.id.editText); //using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
output.append("1");
}
}
);
// Button 2 event handler
button2.setOnClickListener(
//Button 2 Interface
new Button.OnClickListener()
{
//button 1 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("2");
}
}
);
// Button 3 event handler
button3.setOnClickListener(
//Button 3 Interface
new Button.OnClickListener()
{
//button 3 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("3");
}
}
);
// Button 4 event handler
button4.setOnClickListener(
//Button 4 Interface
new Button.OnClickListener()
{
//button 4 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("4");
}
}
);
// Button 5 event handler
button5.setOnClickListener(
//Button 1 Interface
new Button.OnClickListener()
{
//button 5 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("5");
}
}
);
// Button 6 event handler
button6.setOnClickListener(
//Button 6 Interface
new Button.OnClickListener()
{
//button 6 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("6");
}
}
);
// Button 7 event handler
button7.setOnClickListener(
//Button 7 Interface
new Button.OnClickListener()
{
//button 7 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("7");
}
}
);
// Button 8 event handler
button8.setOnClickListener(
//Button 8 Interface
new Button.OnClickListener()
{
//button 8 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("8");
}
}
);
// Button 9 event handler
button9.setOnClickListener(
//Button 9 Interface
new Button.OnClickListener()
{
//button 9 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("9");
}
}
);
// Button 10 "the zero button" event handler
button10.setOnClickListener(
//Button 10 Interface
new Button.OnClickListener()
{
//button 10 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("0");
}
}
);
// Button 11 "the clear button" event handler
button11.setOnClickListener(
//Button 11 Interface
new Button.OnClickListener()
{
//button 11 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.setText(""); //used to turn everything null and not appending anymore
}
}
);
// Button 13 "the (+) add button" event handler
button13.setOnClickListener(
//Button 13 Interface
new Button.OnClickListener()
{
//button 13 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
tempDouble = Double.parseDouble(output.getText().toString()); //we need to change to a string first and store
//it before we add the second number
output.setText(""); //used to turn everything null and not appending anymore
sign = "+";
}
}
);
// Button 14 "the (-) subtract button" event handler
button14.setOnClickListener(
//Button 14 Interface
new Button.OnClickListener()
{
//button 14 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
tempDouble = Double.parseDouble(output.getText().toString()); //we need to change to a string first
output.setText(""); //used to turn everything null and not appending anymore
sign = "-";
}
}
);
// Button 15 "the (/) divide button" event handler
button15.setOnClickListener(
//Button 15 Interface
new Button.OnClickListener()
{
//button 15 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
tempDouble = Double.parseDouble(output.getText().toString()); //we need to change to a string first
output.setText(""); //used to turn everything null and not appending anymore
sign = "/";
}
}
);
// Button 16 "the (*) multiply button" event handler
button16.setOnClickListener(
//Button 16 Interface
new Button.OnClickListener()
{
//button 16 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
tempDouble = Double.parseDouble(output.getText().toString()); //we need to change to a string first
output.setText(""); //used to turn everything null and not appending anymore
sign = "*";
}
}
);
// Button 17 "the (=) = button" event handler 12:59
button17.setOnClickListener(
//Button 17 Interface
new Button.OnClickListener()
{
//button 17 call back method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText); //whatever is in the text box will
//now become the tempDouble2 this is known as casting
tempDouble2 = Double.parseDouble(output.getText().toString()); //we need to change to a string first
//The Logic operators for conditions are below
if (sign == "+")
{
output.setText(Double.toString(tempDouble + tempDouble2));
}
else if (sign == "-")
{
output.setText(Double.toString(tempDouble - tempDouble2));
}
else if (sign == "*")
{
output.setText(Double.toString(tempDouble * tempDouble2));
}
//divide requires some logic to be thought out 11:55
else if (sign == "/")
{
if (tempDouble2 == 0)
{
//Cannot Divide here
output.setText("Can not divide by Zero!!!!! ");
}
else
{
output.setText(Double.toString(tempDouble / tempDouble2)); //takes tempDouble and divides
//by the tempDouble2 and then turns it into a string and
//makes it into the output text
}
}
//Reset The Sign Variable
sign = "";
}
}
); //This is a paraenthsis not a curly bracket
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}