This is my first Android application made in Eclipse Juno in Windows 7. It is a simple calculator with elementary mathematical operations it was designed as an application for a generic 7" tablet.
The below is the code for the layout xml file:
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 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/green" > <LinearLayout android:id="@+id/row1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".12" > <TextView android:id="@+id/displayBox" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="right" android:maxLines="1" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="0" android:textAppearance="?android:attr/textAppearanceLarge" android:textSize="70sp" /> </LinearLayout> <LinearLayout android:id="@+id/row3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".12" > <Button android:id="@+id/button_cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button_cancel" android:textSize="35sp" android:textStyle="bold" /> <Button android:id="@+id/button_point" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button_point" android:textSize="35sp" android:textStyle="bold" /> <Button android:id="@+id/button_divide" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button_divide" android:textSize="45sp" android:textStyle="bold" /> <Button android:id="@+id/button_mult" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button_mult" android:textSize="45sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:id="@+id/row4" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".12" > <Button android:id="@+id/button7" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button7" android:textSize="35sp" android:textStyle="bold" /> <Button android:id="@+id/button8" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button8" android:textSize="35sp" android:textStyle="bold" /> <Button android:id="@+id/button9" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button9" android:textSize="35sp" android:textStyle="bold" /> <Button android:id="@+id/button_sub" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button_sub" android:textSize="45sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:id="@+id/row5" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".12" > <Button android:id="@+id/button4" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button4" android:textSize="35sp" android:textStyle="bold" /> <Button android:id="@+id/button5" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button5" android:textSize="35sp" android:textStyle="bold" /> <Button android:id="@+id/button6" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button6" android:textSize="35sp" android:textStyle="bold" /> <Button android:id="@+id/button_add" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button_add" android:textSize="45sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:id="@+id/row6" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".24" android:baselineAligned="false" > <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".75" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".50" android:textSize="25sp" > <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".33" android:text="@string/button1" android:textSize="45sp" android:textStyle="bold" /> <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".33" android:text="@string/button2" android:textSize="35sp" android:textStyle="bold" /> <Button android:id="@+id/button3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".34" android:text="@string/button3" android:textSize="35sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".50" > <Button android:id="@+id/button0" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".66" android:text="@string/button0" android:textSize="35sp" android:textStyle="bold" /> </LinearLayout> </LinearLayout> <Button android:id="@+id/button_equals" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".25" android:text="@string/button_equals" android:textSize="45sp" android:textStyle="bold" /> </LinearLayout> </LinearLayout> The following is code for the Main Activity class |
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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | package com.Junaid.calculator; //this is main package name import com.Junaid.calculator.R;//we import .R file contained in main package import android.app.Activity;//resources containing Activity import android.os.Bundle;//resources containing os.Bundle import android.view.Menu;//resources containing menu import android.view.View;//resources containing View import android.view.View.OnClickListener;//resources containing onClickListener import android.widget.Button;//resources containing Button import android.widget.TextView;//resources containing TextView //MainActivity inherits from Activity main class and implements onClickListener interface public class MainActivity extends Activity implements OnClickListener { // Activity Data declaration static char previousOp = '+';//static variable only exists in one copy and belongs to class static float runningTotal = 0;//static variable only exists in one copy and belongs to class static int TRUE = 1;//static variable only exists in one copy and belongs to class static int FALSE = 0;//static variable only exists in one copy and belongs to class static int newvalue = TRUE;//static variable only exists in one copy and belongs to class //We create private variables that hold reference of button objects private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, button_point, button_add, button_cancel, button_sub, button_mult, button_divide, button_equals; private TextView displayBox;//private instance of Textview we create displayBox reference //every activity must implement abstract method onCreate //this method gets called once Activity is started //savedInstanceState is used to retrieve state from an activity before being killed @Override protected void onCreate(Bundle savedInstanceState) { CharSequence text;//Interface CharSequence represents ordered set of characters and defines the methods invoke them. super.onCreate(savedInstanceState);//overridden method is accessed even within subclass setContentView(R.layout.activity_main); //Run the function findViewById and pass it R.id.button7 as parameters button7 = (Button)findViewById(R.id.button7); button7.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button8 as parameters button8 = (Button)findViewById(R.id.button8); button8.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button9 as parameters button9 = (Button)findViewById(R.id.button9); button9.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button4 as parameters button4 = (Button)findViewById(R.id.button4); button4.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button5 as parameters button5 = (Button)findViewById(R.id.button5); button5.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button6 as parameters button6 = (Button)findViewById(R.id.button6); button6.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button0 as parameters button0 = (Button)findViewById(R.id.button0); button0.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button1 as parameters button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button2 as parameters button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button3 as parameters button3 = (Button)findViewById(R.id.button3); button3.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button_point as parameters button_point = (Button)findViewById(R.id.button_point); button_point.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button_equals as parameters button_equals = (Button)findViewById(R.id.button_equals); button_equals.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button_add as parameters button_add = (Button)findViewById(R.id.button_add); button_add.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button_sub as parameters button_sub = (Button)findViewById(R.id.button_sub); button_sub.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button_mult as parameters button_mult = (Button)findViewById(R.id.button_mult); button_mult.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button_divide as parameters button_divide = (Button)findViewById(R.id.button_divide); button_divide.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.button_cancel as parameters button_cancel = (Button)findViewById(R.id.button_cancel); button_cancel.setOnClickListener(this);//if button clicked onClickListener runs corresponding onClick() method //Run the function findViewById and pass it R.id.displayBox as parameters displayBox = (TextView)findViewById(R.id.displayBox); text = "0";//text of in display is set to 0 displayBox.setText(text);//run function getText belonging to displayBox and pass it text }//End of overridden onCreate method //onClick method gets called each time a button is pressed public void onClick(View v) { //CharSequence is an interface CharSequence text; //A CharSequence is a readable sequence of char values //Find out which button was pushed based on its ID //Switch statement checks for which button was checked and changes that text switch(v.getId()) { case R.id.button7: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("7");//run function setText belonging to displayBox and pass string of 3 newvalue = FALSE; //the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox in set it to text text = text + "7";//add 7 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and set it to text } break;//Exit the loop } case R.id.button8: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("8");//run function setText belonging to displayBox and pass string of 8 newvalue = FALSE;//the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox in set it to text text = text + "8";//add 8 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and set it to text } break;//Exit the loop } case R.id.button9: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("9");//run function setText belonging to displayBox and pass string of 9 newvalue = FALSE;//the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox in set it to text text = text + "9";//add 9 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and pass it text object } break;//Exit the loop } case R.id.button4: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("4");//run function setText belonging to displayBox and pass string of 4 newvalue = FALSE;//the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox and set it to text text = text + "4";//add 4 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and pass it text object } break;//Exit the loop } case R.id.button5: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("5");//run function setText belonging to displayBox and pass string of 5 newvalue = FALSE;//the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox and set it to text text = text + "5";//add 5 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and pass it text object } break;//Exit the loop } case R.id.button6: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("6");//run function setText belonging to displayBox and pass string of 6 newvalue = FALSE;//the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox and set it to text text = text + "6";//add 6 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and pass it text object } break;//Exit the loop } case R.id.button0: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("0");//run function setText belonging to displayBox and pass string of 0 newvalue = FALSE;//the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox and set it to text text = text + "0";//add 0 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and pass it text object } break;//Exit the loop } case R.id.button1: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("1");//run function setText belonging to displayBox and pass string of 1 newvalue = FALSE;//the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox and set it to text text = text + "1";//add 1 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and pass it text object } break;//Exit the loop } case R.id.button2: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("2");//run function setText belonging to displayBox and pass string of 2 newvalue = FALSE;//the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox and set it to text text = text + "2";//add 2 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and pass it text object } break;//Exit the loop } case R.id.button3: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("3");//run function setText belonging to displayBox and pass string of 3 newvalue = FALSE;//the integer is no longer a new value in display box } else { text = displayBox.getText();//run function getText belonging to displayBox and set it to text text = text + "3";//add 3 to existing value in text displayBox.setText(text);//run function getText belonging to displayBox and pass it text object } break;//Exit the loop } case R.id.button_point: { if (newvalue == TRUE)//any new value will be created as new entry { displayBox.setText("."); newvalue = TRUE; // if button is pressed twice it is now set again } else { text = displayBox.getText();//run function getText belonging to displayBox and set it to text text = text + "."; // add decimal to current text value displayBox.setText(text);//run function getText belonging to displayBox and pass it text object newvalue = TRUE;//prevent button from being added more than once on screen } break;//Exit the loop } case R.id.button_cancel: { text = "0";// text is set 0 displayBox.setText(text);//run function getText belonging to displayBox and pass it text object runningTotal = 0;//runnninTotal is reset to 0 previousOp='+';// set previous operator to plus newvalue = TRUE;//any new value will be created as new entry break;//Exit the loop } case R.id.button_add: { //plus button Float rez1;//Float variable called rez1 to hold current result rez1 = Float.parseFloat((String) displayBox.getText());//Parse the specified string as a float value and running method getText switch (previousOp)// switch loop to deal with operation { case '+': runningTotal = runningTotal + rez1;//running total added to result1 break;//Exit the loop case '-': runningTotal = runningTotal - rez1;//running total subtracted by result1 break;//Exit the loop case '/': runningTotal = runningTotal / rez1;//running total divided by result1 break;//Exit the loop case '*': runningTotal = runningTotal * rez1;//running total multiplied by result1 break;//Exit the loop case '=': // runningTotal = runningTotal; break;//Exit the loop } displayBox.setText(Float.toString(runningTotal));//type conversion runningTotal float to string and set to displayBox newvalue = TRUE;//any new value will be created as new entry previousOp = '+';// set previous operator to plus break;//Exit the loop } case R.id.button_sub: { //minus button Float rez1;//Float variable called rez1 to hold current result rez1 = Float.parseFloat((String) displayBox.getText());//Parse the specified string as a float value and running method getText switch (previousOp) // switch loop to deal with operation { case '+': runningTotal = runningTotal + rez1;//running total added to result1 break;//Exit the loop case '-': runningTotal = runningTotal - rez1;//running total subtracted by result1 break;//Exit the loop case '/': runningTotal = runningTotal / rez1;//running total divided by result1 break;//Exit the loop case '*': runningTotal = runningTotal * rez1;//running total multiplied by result1 break;//Exit the loop case '=': // runningTotal = runningTotal; break;//Exit the loop } displayBox.setText(Float.toString(runningTotal));//type conversion runningTotal float to string and set to displayBox newvalue = TRUE;//any new value will be created as new entry previousOp = '-';// set previous operator to minus break;//Exit the loop } case R.id.button_mult: { //multiply button Float rez1;//Float variable called rez1 to hold current result rez1 = Float.parseFloat((String) displayBox.getText());//Parse the specified string as a float value and running method getText switch (previousOp)// switch loop to deal with operation { case '+': runningTotal = runningTotal + rez1;//running total added to result1 break;//Exit the loop case '-': runningTotal = runningTotal - rez1;//running total subtracted by result1 break;//Exit the loop case '/': runningTotal = runningTotal / rez1;//running total divided by result1 break;//Exit the loop case '*': runningTotal = runningTotal * rez1;//running total multiplied by result1 break;//Exit the loop case '=': // runningTotal = runningTotal; break;//Exit the loop } displayBox.setText(Float.toString(runningTotal));//type conversion runningTotal float to string and set to displayBox newvalue = TRUE;//any new value will be created as new entry previousOp = '*';// set previous operator to multiply break; } case R.id.button_divide: { //divide button Float rez1;//Float variable called rez1 to hold current result rez1 = Float.parseFloat((String) displayBox.getText());//Parse the specified string as a float value and running method getText switch (previousOp)// switch loop to deal with operation { case '+': runningTotal = runningTotal + rez1;//running total added to result1 break;//Exit the loop case '-': runningTotal = runningTotal - rez1;//running total subtracted by result1 break;//Exit the loop case '/': runningTotal = runningTotal / rez1;//running total divided by result1 break;//Exit the loop case '*': runningTotal = runningTotal * rez1;//running total multiplied by result1 break;//Exit the loop case '=': // runningTotal = runningTotal; break;//Exit the loop } displayBox.setText(Float.toString(runningTotal));//type conversion runningTotal float to string and set to displayBox newvalue = TRUE;//any new value will be created as new entry previousOp = '/';// set previous operator to divide break;//Exit the loop } case R.id.button_equals: { //divide button Float rez1;//Float variable called rez1 to hold current result rez1 = Float.parseFloat((String) displayBox.getText());//Parse the specified string as a float value and running method getText switch (previousOp)// switch loop to deal with operation { case '+': runningTotal = runningTotal + rez1;//running total added to result1 break;//Exit the loop case '-': runningTotal = runningTotal - rez1;//running total subtracted by result1 break;//Exit the loop case '/': runningTotal = runningTotal / rez1;//running total divided by result1 break;//Exit the loop case '*': runningTotal = runningTotal * rez1;//running total multiplied by result1 break;//Exit the loop case '=': // runningTotal = runningTotal; break;//Exit the loop } displayBox.setText(Float.toString(runningTotal));//type conversion runningTotal float to string and set to displayBox newvalue = TRUE;//any new value will be created as new entry runningTotal = 0;//current total is reset to 0 previousOp = '+';//set previous operator to plus break;//Exit the loop }//End of equals method }//End of main switch case statement }//End of onClick method //Abstract method is overridden with menu parameters @Override //This means the method is overriding base class method public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;//Return boolean true }//End of method onCreateOptionsMenu }//End of main activity class |
No comments:
Post a Comment