Commit 34b9351a976cb84cab81edfd2fff396be69ed1a9

Authored by Cecilie Høvring
1 parent fc4bae18

Added toolbar

src/main/AndroidManifest.xml
... ... @@ -8,7 +8,7 @@
8 8 android:label="@string/app_name"
9 9 android:roundIcon="@mipmap/ic_launcher_round"
10 10 android:supportsRtl="true"
11   - android:theme="@style/AppTheme">
  11 + android:theme="@style/Theme.AppCompat.Light.NoActionBar">
12 12 <activity android:name=".MainActivity">
13 13 <intent-filter>
14 14 <action android:name="android.intent.action.MAIN" />
... ...
src/main/java/com/example/calculator/MainActivity.java
... ... @@ -4,6 +4,10 @@ import android.content.DialogInterface;
4 4 import android.support.v7.app.AlertDialog;
5 5 import android.support.v7.app.AppCompatActivity;
6 6 import android.os.Bundle;
  7 +import android.support.v7.widget.Toolbar;
  8 +import android.view.Menu;
  9 +import android.view.MenuInflater;
  10 +import android.view.MenuItem;
7 11 import android.view.View;
8 12 import android.widget.Button;
9 13 import android.widget.EditText;
... ... @@ -11,9 +15,6 @@ import android.widget.TextView;
11 15  
12 16 import org.matheclipse.parser.client.eval.DoubleEvaluator;
13 17  
14   -import java.util.regex.Matcher;
15   -import java.util.regex.Pattern;
16   -
17 18 public class MainActivity extends AppCompatActivity {
18 19  
19 20 public boolean empty = true;
... ... @@ -22,8 +23,14 @@ public class MainActivity extends AppCompatActivity {
22 23 protected void onCreate(Bundle savedInstanceState) {
23 24 super.onCreate(savedInstanceState);
24 25 setContentView(R.layout.activity_main);
  26 + Toolbar myToolbar = findViewById(R.id.tbMenu);
  27 + setSupportActionBar(myToolbar);
  28 + }
25 29  
26   -
  30 + public boolean onCreateOptionsMenu(Menu menu) {
  31 + MenuInflater inflater = getMenuInflater();
  32 + inflater.inflate(R.menu.menu, menu);
  33 + return true;
27 34 }
28 35  
29 36 /** Called when the user taps a button */
... ... @@ -41,8 +48,9 @@ public class MainActivity extends AppCompatActivity {
41 48 case R.id.btnSubtract:
42 49 case R.id.btnMultiply:
43 50 case R.id.btnDivide:
  51 + if(t.length() == 0) break;
44 52 Character c = t.charAt(t.length()-1);
45   - if (!(c.equals('+') || c.equals('-') || c.equals('*') || c.equals('/')) && !empty) {
  53 + if (Character.isDigit(c) && !empty) {
46 54 etDisplayField.setText(t + buttonText);
47 55 };
48 56 break;
... ... @@ -50,9 +58,13 @@ public class MainActivity extends AppCompatActivity {
50 58 etDisplayField.setText("");
51 59 break;
52 60 case R.id.btnEquals:
53   - double d = engine.evaluate(t.toString());
54   - etDisplayField.setText(Double.toString(d));
55   - empty = true;
  61 + if(t.length() == 0) break;
  62 + Character i = t.charAt(t.length()-1);
  63 + if (Character.isDigit(i) && !empty) {
  64 + double d = engine.evaluate(t.toString());
  65 + etDisplayField.setText(Double.toString(d));
  66 + empty = true;
  67 + }
56 68 break;
57 69 case R.id.btnHelp:
58 70 help();
... ... @@ -71,10 +83,80 @@ public class MainActivity extends AppCompatActivity {
71 83 }
72 84 }
73 85  
  86 + @Override
  87 + public boolean onOptionsItemSelected(MenuItem item) {
  88 + switch (item.getItemId()) {
  89 + case R.id.action_normal:
  90 + normal();
  91 + return true;
  92 +
  93 + case R.id.action_scientific:
  94 + scientific();
  95 + return true;
  96 +
  97 + case R.id.action_units:
  98 + units();
  99 + return true;
  100 +
  101 + case R.id.action_help:
  102 + help();
  103 + return true;
  104 +
  105 + case R.id.action_exit:
  106 + this.finish();
  107 + return true;
  108 +
  109 + default:
  110 + // If we got here, the user's action was not recognized.
  111 + // Invoke the superclass to handle it.
  112 + return super.onOptionsItemSelected(item);
  113 +
  114 + }
  115 + }
  116 +
  117 + public void normal() {
  118 + AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
  119 + alertDialog.setTitle("Normal");
  120 + alertDialog.setMessage("Du trykket på 'normal'.");
  121 + alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
  122 + new DialogInterface.OnClickListener() {
  123 + public void onClick(DialogInterface dialog, int which) {
  124 + dialog.dismiss();
  125 + }
  126 + });
  127 + alertDialog.show();
  128 + }
  129 +
  130 + public void scientific() {
  131 + AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
  132 + alertDialog.setTitle("Vitenskapelig");
  133 + alertDialog.setMessage("Du trykket på 'vitenskapelig'.");
  134 + alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
  135 + new DialogInterface.OnClickListener() {
  136 + public void onClick(DialogInterface dialog, int which) {
  137 + dialog.dismiss();
  138 + }
  139 + });
  140 + alertDialog.show();
  141 + }
  142 +
  143 + public void units() {
  144 + AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
  145 + alertDialog.setTitle("Enhetsomregning");
  146 + alertDialog.setMessage("Du trykket på 'enhetsomregning'.");
  147 + alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
  148 + new DialogInterface.OnClickListener() {
  149 + public void onClick(DialogInterface dialog, int which) {
  150 + dialog.dismiss();
  151 + }
  152 + });
  153 + alertDialog.show();
  154 + }
  155 +
74 156 public void help() {
75 157 AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
76 158 alertDialog.setTitle("Hjelp");
77   - alertDialog.setMessage("Info info info");
  159 + alertDialog.setMessage("Du trykket på 'hjelp'.");
78 160 alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
79 161 new DialogInterface.OnClickListener() {
80 162 public void onClick(DialogInterface dialog, int which) {
... ...
src/main/res/drawable/ic_normal.png 0 → 100644

4.38 KB

src/main/res/drawable/ic_scientific.png 0 → 100644

8.44 KB

src/main/res/drawable/ic_unit.png 0 → 100644

10.9 KB

src/main/res/layout/activity_main.xml
1 1 <?xml version="1.0" encoding="utf-8"?>
  2 +
2 3 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 4 xmlns:app="http://schemas.android.com/apk/res-auto"
4 5 xmlns:tools="http://schemas.android.com/tools"
... ... @@ -6,9 +7,18 @@
6 7 android:layout_height="match_parent"
7 8 tools:context=".MainActivity">
8 9  
  10 + <android.support.v7.widget.Toolbar
  11 + android:id="@+id/tbMenu"
  12 + android:layout_width="match_parent"
  13 + android:layout_height="?attr/actionBarSize"
  14 + android:background="@color/buttonBorder"
  15 + android:elevation="4dp"
  16 + android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
  17 + app:layout_constraintTop_toTopOf="parent"
  18 + app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
9 19  
10 20 <android.support.constraint.ConstraintLayout
11   - android:layout_width="0dp"
  21 + android:layout_width="352dp"
12 22 android:layout_height="0dp"
13 23 android:layout_marginStart="16dp"
14 24 android:layout_marginLeft="16dp"
... ... @@ -187,6 +197,7 @@
187 197 app:layout_constraintBottom_toTopOf="@+id/btnHelp"
188 198 app:layout_constraintEnd_toStartOf="@+id/btnClear"
189 199 app:layout_constraintHorizontal_bias="0.5"
  200 + app:layout_constraintHorizontal_chainStyle="spread_inside"
190 201 app:layout_constraintStart_toStartOf="parent"
191 202 app:layout_constraintTop_toBottomOf="@+id/btnThree" />
192 203  
... ... @@ -272,5 +283,6 @@
272 283 android:inputType="textPersonName"
273 284 app:layout_constraintEnd_toEndOf="parent"
274 285 app:layout_constraintStart_toStartOf="parent"
275   - app:layout_constraintTop_toTopOf="parent" />
  286 + app:layout_constraintTop_toBottomOf="@+id/tbMenu" />
  287 +
276 288 </android.support.constraint.ConstraintLayout>
277 289 \ No newline at end of file
... ...
src/main/res/menu/menu.xml 0 → 100644
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<menu xmlns:android="http://schemas.android.com/apk/res/android"
  3 + xmlns:app="http://schemas.android.com/apk/res-auto">
  4 + <item
  5 + android:id="@+id/action_normal"
  6 + android:icon="@drawable/ic_normal"
  7 + android:title="@string/menuNormalText"
  8 + app:showAsAction="ifRoom"/>
  9 +
  10 + <item
  11 + android:id="@+id/action_scientific"
  12 + android:icon="@drawable/ic_scientific"
  13 + android:title="@string/menuScientificText"
  14 + app:showAsAction="ifRoom"/>
  15 +
  16 + <item
  17 + android:id="@+id/action_units"
  18 + android:icon="@drawable/ic_unit"
  19 + android:title="@string/menuUnitsText"
  20 + app:showAsAction="ifRoom"/>
  21 +
  22 + <item
  23 + android:id="@+id/action_help"
  24 + android:title="@string/menuHelpText"
  25 + app:showAsAction="never"/>
  26 +
  27 + <item
  28 + android:id="@+id/action_exit"
  29 + android:title="@string/menuExitText"
  30 + app:showAsAction="never"/>
  31 +</menu>
0 32 \ No newline at end of file
... ...
src/main/res/values/strings.xml
1 1 <resources>
2   - <string name="app_name">Calculator</string>
  2 + <string name="app_name">Kalkulator</string>
3 3 <string name="btnNineText">9</string>
4 4 <string name="btnEightText">8</string>
5 5 <string name="btnSevenText">7</string>
... ... @@ -18,4 +18,10 @@
18 18 <string name="btnClearText">C</string>
19 19 <string name="btnHelpText">HJELP</string>
20 20 <string name="btnExitText">AVSLUTT</string>
  21 +
  22 + <string name="menuNormalText">Normal</string>
  23 + <string name="menuScientificText">Vitenskapelig</string>
  24 + <string name="menuUnitsText">Enhetsomregning</string>
  25 + <string name="menuHelpText">Hjelp</string>
  26 + <string name="menuExitText">Avslutt</string>
21 27 </resources>
... ...
src/test/java/com/example/calculator/ExampleUnitTest.java
... ... @@ -5,7 +5,7 @@ import org.junit.Test;
5 5 import static org.junit.Assert.*;
6 6  
7 7 /**
8   - * Example local unit test, which will execute on the development machine (host).
  8 + * Example local ic_unit test, which will execute on the development machine (host).
9 9 *
10 10 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
11 11 */
... ...