Skip to content

Commit 03b545a

Browse files
committed
Fixed button sizing and started to add functionality
1 parent 8bec14d commit 03b545a

File tree

2 files changed

+181
-86
lines changed

2 files changed

+181
-86
lines changed
Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,80 @@
1+
// Standard
12
package com.example.daymian.calculator;
2-
3-
43
import android.support.v7.app.AppCompatActivity;
54
import android.os.Bundle;
65

6+
// For the calculator
7+
import android.widget.TextView;
8+
import android.view.View;
9+
import android.widget.Button;
10+
11+
712

813
public class MainActivity extends AppCompatActivity {
914

15+
// Declaring sum things
16+
private TextView screen;
17+
private String str1, str2, result, str, sign;
18+
private double a, b;
19+
1020
@Override
1121
protected void onCreate(Bundle savedInstanceState) {
22+
// Standard
1223
super.onCreate(savedInstanceState);
1324
setContentView(R.layout.activity_main);
25+
26+
// For my calculations
27+
screen = (TextView)findViewById(R.id.textView);
28+
str = "";
29+
}
30+
31+
public void onClick(View view) {
32+
Button number = (Button)view;
33+
str = number.getText().toString();
34+
screen.setText(str);
35+
a = Double.parseDouble(str);
36+
}
37+
38+
public void onClickSigns(View view) {
39+
Button operand = (Button)view;
40+
sign = operand.getText().toString();
41+
42+
if (sign.contains("AC")) {
43+
screen.setText("0");
44+
a = 0.0;
45+
b = 0.0;
46+
} else {
47+
screen.setText(sign);
48+
str = "";
49+
}
50+
}
51+
52+
public void Calculate(View view) {
53+
//Button second = (Button)view;
54+
str2 = screen.getText().toString();
55+
b = Double.parseDouble(str2);
56+
57+
if(sign.equals("+")) {
58+
result = (a + b) + "";
59+
}
60+
else if(sign.equals("-")) {
61+
result = (a - b) + "";
62+
}
63+
else if(sign.equals("X")) {
64+
result = (a * b) + "";
65+
}
66+
else if(sign.equals("/")) {
67+
result = (a / b) + "";
68+
}
69+
else if(sign.equals("%")) {
70+
result = (a % b) + "";
71+
}
72+
else if(sign.equals("+/-")) {
73+
result = (-1 * a) + "";
74+
}
75+
else {
76+
result = "wrong";
77+
}
78+
screen.setText(result);
1479
}
1580
}

app/src/main/res/layout/activity_main.xml

Lines changed: 114 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -6,166 +6,196 @@
66
android:layout_height="match_parent"
77
android:paddingLeft="0dp"
88
android:paddingRight="0dp"
9-
android:orientation="vertical">
9+
android:orientation="vertical"
10+
android:weightSum="100">
1011

1112
<!--This is for the number view-->
1213
<LinearLayout
1314
android:layout_width="match_parent"
14-
android:layout_height="wrap_content">
15+
android:layout_height="0dp"
16+
android:layout_weight="25">
1517

1618
<TextView
19+
android:id="@+id/textView"
1720
android:layout_width="match_parent"
1821
android:layout_height="wrap_content"
1922
android:layout_gravity="end"
2023
android:text="@string/zero"
2124
android:textAlignment="viewEnd"
2225
android:textColor="@android:color/black"
23-
android:textSize="32sp" />
26+
android:textSize="60sp"/>
2427
</LinearLayout>
2528

2629
<!--This is for AC, +/-, Remainder, and division-->
2730
<LinearLayout
2831
android:layout_width="match_parent"
29-
android:layout_height="wrap_content">
32+
android:layout_height="wrap_content"
33+
android:layout_weight="15">
3034

3135
<Button
3236
android:id="@+id/AC"
33-
android:layout_width="0dp"
34-
android:layout_height="wrap_content"
35-
android:layout_weight=".25"
36-
android:text="@string/AC" />
37+
android:layout_width="wrap_content"
38+
android:layout_height="match_parent"
39+
android:text="@string/AC"
40+
android:onClick="onClickSigns"
41+
android:textSize="30sp"/>
3742
<Button
3843
android:id="@+id/flipSign"
39-
android:layout_width="0dp"
40-
android:layout_height="wrap_content"
41-
android:layout_weight=".25"
42-
android:text="@string/flipSign" />
44+
android:layout_width="wrap_content"
45+
android:layout_height="match_parent"
46+
android:text="@string/flipSign"
47+
android:onClick="onClickSigns"
48+
android:textSize="30sp"/>
4349
<Button
4450
android:id="@+id/remainder"
45-
android:layout_width="0dp"
46-
android:layout_height="wrap_content"
47-
android:layout_weight=".25"
48-
android:text="@string/remainder" />
51+
android:layout_width="wrap_content"
52+
android:layout_height="match_parent"
53+
android:text="@string/remainder"
54+
android:onClick="onClickSigns"
55+
android:textSize="30sp"/>
4956
<Button
5057
android:id="@+id/division"
51-
android:layout_width="0dp"
52-
android:layout_height="wrap_content"
53-
android:layout_weight=".25"
54-
android:text="@string/divison" /> </LinearLayout>
58+
android:layout_width="wrap_content"
59+
android:layout_height="match_parent"
60+
android:text="@string/divison"
61+
android:onClick="onClickSigns"
62+
android:textSize="30sp"/> </LinearLayout>
5563

5664
<!--This is for 7, 8, 9, and Multiplication-->
5765
<LinearLayout
5866
android:layout_width="match_parent"
59-
android:layout_height="wrap_content">
67+
android:layout_height="wrap_content"
68+
android:layout_weight="15">
6069

6170
<Button
6271
android:id="@+id/seven"
63-
android:layout_width="0dp"
64-
android:layout_height="wrap_content"
65-
android:layout_weight=".25"
66-
android:text="@string/seven" />
72+
android:layout_width="wrap_content"
73+
android:layout_height="match_parent"
74+
android:text="@string/seven"
75+
android:onClick="onClick"
76+
android:textSize="30sp"/>
6777
<Button
6878
android:id="@+id/eight"
69-
android:layout_width="0dp"
70-
android:layout_height="wrap_content"
71-
android:layout_weight=".25"
72-
android:text="@string/eight" />
79+
android:layout_width="wrap_content"
80+
android:layout_height="match_parent"
81+
android:text="@string/eight"
82+
android:onClick="onClick"
83+
android:textSize="30sp"/>
7384
<Button
7485
android:id="@+id/nine"
75-
android:layout_width="0dp"
76-
android:layout_height="wrap_content"
77-
android:layout_weight=".25"
78-
android:text="@string/nine" />
86+
android:layout_width="wrap_content"
87+
android:layout_height="match_parent"
88+
android:text="@string/nine"
89+
android:onClick="onClick"
90+
android:textSize="30sp"/>
7991
<Button
8092
android:id="@+id/multiplication"
81-
android:layout_width="0dp"
82-
android:layout_height="wrap_content"
83-
android:layout_weight=".25"
84-
android:text="@string/multiplication" /> </LinearLayout>
93+
android:layout_width="wrap_content"
94+
android:layout_height="match_parent"
95+
android:text="@string/multiplication"
96+
android:onClick="onClickSigns"
97+
android:textSize="30sp"/> </LinearLayout>
8598

8699
<!--This is for 4, 5, 6, and subtraction-->
87100
<LinearLayout
88101
android:layout_width="match_parent"
89-
android:layout_height="wrap_content">
102+
android:layout_height="wrap_content"
103+
android:layout_weight="15">
90104

91105
<Button
92106
android:id="@+id/four"
93-
android:layout_width="0dp"
94-
android:layout_height="wrap_content"
95-
android:layout_weight=".25"
96-
android:text="@string/four" />
107+
android:layout_width="wrap_content"
108+
android:layout_height="match_parent"
109+
android:text="@string/four"
110+
android:onClick="onClick"
111+
android:textSize="30sp"/>
97112
<Button
98113
android:id="@+id/five"
99-
android:layout_width="0dp"
100-
android:layout_height="wrap_content"
101-
android:layout_weight=".25"
102-
android:text="@string/five" />
114+
android:layout_width="wrap_content"
115+
android:layout_height="match_parent"
116+
android:text="@string/five"
117+
android:onClick="onClick"
118+
android:textSize="30sp"/>
103119
<Button
104120
android:id="@+id/six"
105-
android:layout_width="0dp"
106-
android:layout_height="wrap_content"
107-
android:layout_weight=".25"
108-
android:text="@string/six" />
121+
android:layout_width="wrap_content"
122+
android:layout_height="match_parent"
123+
android:text="@string/six"
124+
android:onClick="onClick"
125+
android:textSize="30sp"/>
109126
<Button
110127
android:id="@+id/subtraction"
111-
android:layout_width="0dp"
112-
android:layout_height="wrap_content"
113-
android:layout_weight=".25"
114-
android:text="@string/subtraction" /> </LinearLayout>
128+
android:layout_width="wrap_content"
129+
android:layout_height="match_parent"
130+
android:text="@string/subtraction"
131+
android:onClick="onClickSigns"
132+
android:textSize="30sp"/> </LinearLayout>
115133

116134
<!--This is for 1, 2, 3, and addition-->
117135
<LinearLayout
118136
android:layout_width="match_parent"
119-
android:layout_height="wrap_content">
137+
android:layout_height="wrap_content"
138+
android:layout_weight="15">
120139

121140
<Button
122141
android:id="@+id/one"
123-
android:layout_width="0dp"
124-
android:layout_height="wrap_content"
125-
android:layout_weight=".25"
126-
android:text="@string/one" />
142+
android:layout_width="wrap_content"
143+
android:layout_height="match_parent"
144+
android:text="@string/one"
145+
android:onClick="onClick"
146+
android:textSize="30sp"/>
127147
<Button
128148
android:id="@+id/two"
129-
android:layout_width="0dp"
130-
android:layout_height="wrap_content"
131-
android:layout_weight=".25"
132-
android:text="@string/two" />
149+
android:layout_width="wrap_content"
150+
android:layout_height="match_parent"
151+
android:text="@string/two"
152+
android:onClick="onClick"
153+
android:textSize="30sp"/>
133154
<Button
134155
android:id="@+id/three"
135-
android:layout_width="0dp"
136-
android:layout_height="wrap_content"
137-
android:layout_weight=".25"
138-
android:text="@string/three" />
156+
android:layout_width="wrap_content"
157+
android:layout_height="match_parent"
158+
android:text="@string/three"
159+
android:onClick="onClick"
160+
android:textSize="30sp"/>
161+
139162
<Button
140163
android:id="@+id/addition"
141-
android:layout_width="0dp"
142-
android:layout_height="wrap_content"
143-
android:layout_weight=".25"
144-
android:text="@string/addition" /> </LinearLayout>
164+
android:layout_width="wrap_content"
165+
android:layout_height="match_parent"
166+
android:onClick="onClickSigns"
167+
android:text="@string/addition"
168+
android:textSize="30sp"/> </LinearLayout>
145169

146170
<!--This is for 0, decimal, and equals-->
147171
<LinearLayout
148172
android:layout_width="match_parent"
149-
android:layout_height="wrap_content">
173+
android:layout_height="wrap_content"
174+
android:layout_weight="15"
175+
android:weightSum="1">
150176

151177
<Button
152178
android:id="@+id/zero"
153-
android:layout_width="0dp"
154-
android:layout_height="wrap_content"
155-
android:layout_weight=".50"
156-
android:text="@string/zero" />
179+
android:layout_width="wrap_content"
180+
android:layout_height="match_parent"
181+
android:text="@string/zero"
182+
android:onClick="onClick"
183+
android:textSize="30sp"
184+
android:layout_weight="0.59" />
157185
<Button
158186
android:id="@+id/decimal"
159-
android:layout_width="0dp"
160-
android:layout_height="wrap_content"
161-
android:layout_weight=".25"
162-
android:text="@string/decimal" />
187+
android:layout_width="wrap_content"
188+
android:layout_height="match_parent"
189+
android:text="@string/decimal"
190+
android:onClick="onClickSigns"
191+
android:textSize="30sp"/>
163192
<Button
164193
android:id="@+id/equals"
165-
android:layout_width="0dp"
166-
android:layout_height="wrap_content"
167-
android:layout_weight=".25"
168-
android:text="@string/equals" /> </LinearLayout>
194+
android:layout_width="wrap_content"
195+
android:layout_height="match_parent"
196+
android:text="@string/equals"
197+
android:onClick="Calculate"
198+
android:textSize="30sp"/> </LinearLayout>
169199

170200

171201
</LinearLayout>

0 commit comments

Comments
 (0)