Skip to content

Commit

Permalink
Merge pull request aadityamp01#83 from akashdnb/Akash-branch
Browse files Browse the repository at this point in the history
Enhanced Calculator App
  • Loading branch information
aadityamp01 committed Oct 9, 2022
2 parents 1368584 + 2fdf148 commit 155619b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -23,11 +28,12 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
data=findViewById(R.id.data);
output=findViewById(R.id.output);
oset.add('+'); oset.add('-'); oset.add('*'); oset.add('/'); oset.add('%');
data.setMovementMethod(new ScrollingMovementMethod());
}

public void numberEvent(View view) {
if(data.getText().toString().equals("0"))
data.setText("");
Expand Down Expand Up @@ -166,14 +172,23 @@ public static Double applyOp(char op, Double b, Double a) {
}

public void equalEvent(View view) {
String newNumber=data.getText().toString();
double result=0.0;
result = Double.parseDouble(String.valueOf(evaluate(newNumber)));
output.setText(result+"");
try {
String newNumber=data.getText().toString();
double result=0.0;
result = Double.parseDouble(String.valueOf(evaluate(newNumber)));
if((int)result==result){
output.setText((int)result+"");
}else{
output.setText(result+"");
}
}catch (Exception e){
Toast.makeText(this, "Invalid Operation!!", Toast.LENGTH_SHORT).show();
}
}

public void clearEvent(View view) {
data.setText("0");
output.setText("0");
newOp= true;
}

Expand All @@ -193,4 +208,14 @@ public void percentEvent(View view) {
data.setText(curr);
newOp=true;
}

public void backspaceEvent(View view) {
String number= data.getText().toString();
if(number.length()>0){
number=number.substring(0,number.length()-1);
}
data.setText(number);
if(number.equals(""))
data.setText("0");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M22,3L7,3c-0.69,0 -1.23,0.35 -1.59,0.88L0,12l5.41,8.11c0.36,0.53 0.9,0.89 1.59,0.89h15c1.1,0 2,-0.9 2,-2L24,5c0,-1.1 -0.9,-2 -2,-2zM19,15.59L17.59,17 14,13.41 10.41,17 9,15.59 12.59,12 9,8.41 10.41,7 14,10.59 17.59,7 19,8.41 15.41,12 19,15.59z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#573f3f">
<item>
<shape android:shape="oval">
<solid android:color="#1D1D1D"/>
<size
android:width="48dp"
android:height="40dp"/>
</shape>
</item>
</ripple>
63 changes: 45 additions & 18 deletions Calculator App/Calculator/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1D1D1D"
tools:context=".MainActivity">

<LinearLayout
Expand All @@ -13,30 +14,22 @@
android:weightSum="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/linearLayout">

<TextView
android:id="@+id/data"
android:layout_width="match_parent"
android:layout_height="124dp"
android:layout_weight="1"
android:background="#1D1D1D"
android:gravity="end|bottom"
android:text="@string/zero"
android:textColor="@color/colorWhite"
android:textSize="40sp" />

<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="292dp"
android:layout_weight="1"
android:layout_height="0dp"
android:minHeight="128dp"
android:layout_weight="2"
android:scrollbars = "vertical"
android:background="#1D1D1D"
android:gravity="end|center"
android:text="@string/zero"
android:paddingHorizontal="15dp"
android:textColor="@color/colorWhite"
android:textSize="40sp" />

</LinearLayout>

<LinearLayout
Expand All @@ -48,6 +41,40 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginVertical="20dp"
android:layout_weight="1"
android:background="#1D1D1D"
android:gravity="end"
android:paddingHorizontal="15dp"
android:text="@string/zero"
android:textColor="@color/colorWhite"
android:textSize="40sp" />


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">

<ImageView
android:id="@+id/backspace"
android:layout_width="0dp"
android:layout_height="44dp"
android:padding="10dp"
android:background="@drawable/ripple_effect_backspace"
android:clickable="true"
android:layout_weight="1"
android:src="@drawable/ic_backspace"
android:onClick="backspaceEvent"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -58,7 +85,7 @@
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="@drawable/ripple_effect_numbers"
android:background="@drawable/ripple_effect_operations"
android:text="@string/add_subtract"
android:textColor="@color/colorWhite"
android:textSize="30sp"
Expand All @@ -69,7 +96,7 @@
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="@drawable/ripple_effect_numbers"
android:background="@drawable/ripple_effect_operations"
android:text="@string/parenthesis"
android:textColor="@color/colorWhite"
android:textSize="30sp"
Expand All @@ -80,7 +107,7 @@
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="@drawable/ripple_effect_numbers"
android:background="@drawable/ripple_effect_operations"
android:text="@string/modulus"
android:textColor="@color/colorWhite"
android:textSize="30sp"
Expand Down
4 changes: 2 additions & 2 deletions Calculator App/Calculator/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorPrimary">#808080</color>
<color name="colorPrimaryDark">#808080</color>
<color name="colorAccent">#03DAC5</color>
<color name="colorBlack">#000000</color>
<color name="colorWhite">#ffffff</color>
Expand Down

0 comments on commit 155619b

Please sign in to comment.