Skip to content

Commit

Permalink
add menu with alert & some fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishal1297 committed Oct 20, 2020
1 parent 301cd17 commit f847edb
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
49 changes: 48 additions & 1 deletion app/src/main/java/com/app/calculator/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.app.calculator;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import org.mariuszgromada.math.mxparser.Expression;
Expand Down Expand Up @@ -135,11 +141,52 @@ public void onClick(View view) {
expTxtView.setText(result);
resultTxtView.setText(result);
} else if (R.id.dot == id) {
generateExpression(".");
if (!Double.isNaN(new Expression(exp + ".0").calculate())) generateExpression(".");
}

}

@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure ?")
.setMessage("You want to exit ?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
super.onOptionsItemSelected(item);
int menuItemId = item.getItemId();
if (menuItemId == R.id.aboutItem) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("About")
.setMessage("A Simple Calculator\nBy Vishal")
.setPositiveButton("Ok", null).show();
return true;
} else if (menuItemId == R.id.exitItem) {
onBackPressed();
return true;
} else {
return false;
}
}

private void calculate() {
String exp = expTxtView.getText().toString();
String result;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/inputs_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:id="@+id/exp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:background="@color/grey"
android:gravity="center_vertical|end"
android:padding="8dp"
android:singleLine="true"
Expand All @@ -22,7 +22,7 @@
android:id="@+id/out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:background="@color/grey"
android:gravity="center_vertical|end"
android:padding="8dp"
android:singleLine="true"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/aboutItem"
android:title="@string/about" />
<item
android:id="@+id/exitItem"
android:title="@string/exit" />
</menu>
5 changes: 3 additions & 2 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="black">#2D2D2D</color>
<color name="orange">#ED854A</color>
<color name="black">#242424</color>
<color name="grey">#E9303030</color>
<color name="orange">#EB7531</color>
<color name="white">#FFFFFF</color>

</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
<string name="zeroNum">0</string>
<string name="dotSymbol">.</string>
<string name="equalsSymbol">=</string>
<string name="about">About</string>
<string name="feedback">Feedback</string>
<string name="exit">Exit</string>

</resources>

0 comments on commit f847edb

Please sign in to comment.