Skip to main content

Simple Calculator For Android Application






Calculator App's


Main Activity .java




package com.nm.calc;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {


Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
Button b11,b12,b13,b14,b15,b16,b17,b18,b19;
Button b21,b22,b23,b24,b25,b26,b27,b28,b29;


EditText et1;

double a,b,r,tmp,mem;
char op;

boolean dotcl=true,clear = true, first_move = true, opclick = true;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
        et1 = (EditText)findViewById(R.id.editText1);
     
        b1 = (Button)findViewById(R.id.btn_0);
        b1.setOnClickListener(this);
     
        b2 = (Button)findViewById(R.id.btn_1);
        b2.setOnClickListener(this);
     
        b3 = (Button)findViewById(R.id.btn_2);
        b3.setOnClickListener(this);
     
        b4 = (Button)findViewById(R.id.btn_3);
        b4.setOnClickListener(this);
     
        b5 = (Button)findViewById(R.id.btn_4);
        b5.setOnClickListener(this);
     
        b6 = (Button)findViewById(R.id.btn_5);
        b6.setOnClickListener(this);
     
        b7 = (Button)findViewById(R.id.btn_6);
        b7.setOnClickListener(this);
     
        b8 = (Button)findViewById(R.id.btn_add);
        b8.setOnClickListener(this);
     
        b9 = (Button)findViewById(R.id.btn_div);
        b9.setOnClickListener(this);
     
        b11 = (Button)findViewById(R.id.btn_ans);
        b11.setOnClickListener(this);
     
        b12 = (Button)findViewById(R.id.btn_sub);
        b12.setOnClickListener(this);
     
        b13 = (Button)findViewById(R.id.btn_mul);
        b13.setOnClickListener(this);
     
        b14 = (Button)findViewById(R.id.btn_dot);
        b14.setOnClickListener(this);
     
        b15 = (Button)findViewById(R.id.btn_pm);
        b15.setOnClickListener(this);
     
        b16 = (Button)findViewById(R.id.btn_byx);
        b16.setOnClickListener(this);
     
        b17 = (Button)findViewById(R.id.btn_perc);
        b17.setOnClickListener(this);
     

        b18 = (Button)findViewById(R.id.btn_mr);
        b18.setOnClickListener(this);

        b19 = (Button)findViewById(R.id.btn_ms);
        b19.setOnClickListener(this);

        b21 = (Button)findViewById(R.id.btn_mc);
        b21.setOnClickListener(this);
     

        b22 = (Button)findViewById(R.id.btn_mplus);
        b22.setOnClickListener(this);

        b23 = (Button)findViewById(R.id.btn_msub);
        b23.setOnClickListener(this);
     

        b24 = (Button)findViewById(R.id.btn_c);
        b24.setOnClickListener(this);

        b25 = (Button)findViewById(R.id.btn_ce);
        b25.setOnClickListener(this);
     
        b26 = (Button)findViewById(R.id.btn_back);
        b26.setOnClickListener(this);
       
     
     
    }


    @Override
    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;
    }


@Override
public void onClick(View v) {
// TODO Auto-generated method stub

switch(v.getId())
{

case R.id.btn_0:

String z = et1.getText().toString();

if(!z.equals("0"))
{
if(clear)
{
et1.setText("");
clear = false;
}

et1.setText(et1.getText() + "0");


}
break;



case R.id.btn_1:

if(clear)
{
et1.setText("");
clear = false;
}
opclick = true;
et1.setText(et1.getText() + "1");
break;

case R.id.btn_2:

if(clear)
{
et1.setText("");
clear = false;
}
opclick = true;
et1.setText(et1.getText() + "2");
break;

case R.id.btn_3:

if(clear)
{
et1.setText("");
clear = false;
}
opclick = true;
et1.setText(et1.getText() + "3");
break;

case R.id.btn_4:

if(clear)
{
et1.setText("");
clear = false;
}
opclick = true;
et1.setText(et1.getText() +"4");
break;

case R.id.btn_5:

if(clear)
{
et1.setText("");
clear = false;

}
opclick = true;
et1.setText(et1.getText() + "5");
break;

case R.id.btn_6:

if(clear)
{
et1.setText("");
clear = false;
}
opclick = true;
et1.setText(et1.getText() + "6");
break;

case R.id.btn_dot:

if(clear)
{
et1.setText("0");
clear = false;
}


if(dotcl)
{
et1.setText(et1.getText() + ".");
dotcl = false;
}

break;

case R.id.btn_ms:

mem = Double.parseDouble(et1.getText().toString());

break;

case R.id.btn_mc:

mem = 0.0;

break;


case R.id.btn_mr:


et1.setText(String.valueOf(mem));

break;


case R.id.btn_mplus:

mem =  mem + Double.parseDouble(et1.getText().toString());

break;


case R.id.btn_msub:

mem =  mem - Double.parseDouble(et1.getText().toString());


break;





case R.id.btn_add:

if(opclick)
{
if(first_move)
{
a = Double.parseDouble(et1.getText().toString());
first_move = false;


}

else
{
b = Double.parseDouble(et1.getText().toString());
result();
//et1.setText(String.valueOf(r));

a = r;

}

clear = true;

dotcl = true;
opclick = false;
}
op = 'a';

break;

case R.id.btn_pm:

tmp = Double.parseDouble(et1.getText().toString());
tmp = (-1) * tmp;
et1.setText(String.valueOf(tmp));


break;



case R.id.btn_byx:

tmp = Double.parseDouble(et1.getText().toString());
tmp = 1 / tmp;
et1.setText(String.valueOf(tmp));


break;



case R.id.btn_perc:

break;



case R.id.btn_c:

et1.setText("0");
clear = true;
dotcl = true;
first_move = true;



break;

case R.id.btn_ce:

et1.setText("0");
clear = true;
dotcl = true;

break;

case R.id.btn_back:

String s1 = et1.getText().toString();
if(s1.length() > 1)
{
String s2  = s1.substring(0, s1.length() - 1);
et1.setText(s2);
}
else
{
et1.setText("0");
clear = true;
}



break;


case R.id.btn_mul:

a = Double.parseDouble(et1.getText().toString());
clear = true;
op = 'm';
dotcl = true;

break;

case R.id.btn_sub:


if(opclick)
{
if(first_move)
{
a = Double.parseDouble(et1.getText().toString());
first_move = false;


}

else
{
b = Double.parseDouble(et1.getText().toString());
result();
//et1.setText(String.valueOf(r));

a = r;

}

clear = true;

dotcl = true;
opclick = false;
}

op = 's';

break;

case R.id.btn_div:

break;

case R.id.btn_ans:

b = Double.parseDouble(et1.getText().toString());
result();
//et1.setText(String.valueOf(r));
clear = true;
a = r;

break;





}




}
 
private void result()
{

switch(op)
{
case 'a':
r = a+b;

break;

case 's':
r = a-b;

break;


case 'm':

r = a*b;
break;


case 'd':
r = a/b;

break;





}

String result = String.valueOf(r);

String[] parts = result.split(".0");



if(parts.length <= 1)
{
et1.setText(parts[0]);

}
else
{
et1.setText(result);
}



}

}




Acitivty _main.xml





<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:cursorVisible="false" android:ems="10" android:gravity="right" android:text="0" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_mc" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="MC" /> <Button android:id="@+id/btn_mr" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="MR" /> <Button android:id="@+id/btn_ms" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="MS" /> <Button android:id="@+id/btn_mplus" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="M+" /> <Button android:id="@+id/btn_msub" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="M-" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_back" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="&lt;--" /> <Button android:id="@+id/Button21" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/Button24" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/btn_ce" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="CE" /> <Button android:id="@+id/btn_c" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="C" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/Button17" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/Button16" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/Button19" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/btn_div" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="/" /> <Button android:id="@+id/btn_byx" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="1/x" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_4" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="4" /> <Button android:id="@+id/btn_5" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="5" /> <Button android:id="@+id/btn_6" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="6" /> <Button android:id="@+id/btn_mul" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="*" /> <Button android:id="@+id/btn_perc" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="%" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_1" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="1" /> <Button android:id="@+id/btn_2" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="2" /> <Button android:id="@+id/btn_3" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="3" /> <Button android:id="@+id/btn_sub" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="-" /> <Button android:id="@+id/Button05" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_pm" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="+/-" /> <Button android:id="@+id/btn_0" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="0" /> <Button android:id="@+id/btn_dot" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="." /> <Button android:id="@+id/btn_add" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="+" /> <Button android:id="@+id/btn_ans" style="?android:attr/buttonStyleSmall" android:layout_width="60dp" android:layout_height="wrap_content" android:text="=" /> </LinearLayout> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>










Comments

Popular posts from this blog

Android Logging using Timber Library

  Timber     is a logger with a small, extensible API which provides utility on top of Android's normal Log class. Before  the release, we’ll cleanup the log statements by removing them manually (even though logs can be disabled in release build). This tedious process can be avoided easily by using Timber. Timber provides lots of other options as well. Let’s see how it can be used in our projects to maintain the logs better. 1. Timber Below are few debug statements printed using default Log class. int a = 100 ; Log.e( "TAG" , String.format( "Integer a value is: %d" , a));   String name = "Android Codest" ; Log.e( "TAG" , String.format( "Find source from: %s" , name)); The above same statements can be printed using Timber as below. int a = 100 ; Timber.d( "Integer a value is: %d" , a);   String name = " Android Codest " ; Timber.d( " Find source from : %s" , name); You can notice here, the TAG is not p...

Convert your apk into Android App Bundle

Android App Bundle In Google I/O 2018, a new publishing format has been introduced for Android applications called Android App Bundle. It is a new upload format that includes all your app’s compiled code and resources, but defers APK generation and signing to Google Play. Traditionally, Android apps are distributed using a special file called an Android Package(.apk). How to build an app bundle? You can easily build your app bundle using Android Studio(3.2 Canary 14+) or using command line interface. The generated app bundle will be stored at app/build/outputs/bundle/buildVariant/bundle.aab. Android Studio : Go to Build > Build Bundle(s)/APK(s) and select Build Bundle(s). Console : ./gradlew bundle Dynamic Delivery with Split APKs A fundamental component of Dynamic Delivery is the split APK mechanism available on Android 5.0 (API level 21) and higher. Split APKs are very similar to regular APKs — they include compiled DEX bytecode, resources, and an Android manifest. However, the ...

Push Notification using Firebase in Android

This is a tutorial about sending push notifications to Android through Firebase, based on the new release of Firebase this year (2016). This tutorial shows how to setup the skeleton for sending and receiving push notifications via FCM with instructions on server code. 1. Get started Add a new project or import an existing project to  Firebase console . If you choose to create a new project, you need to set the project name and country. For example, I will call my project Firebase Notification Then select "Add Firebase to your Android app". Set a package name for your app. I only set my package name and omit the SHA-1 because I don't use Firebase for my app's authentication. Click the  ADD APP  button here to download google-services.json. This is an important file and you will need to put it into your app. 2. Add google-services.json to your app folder Replace the google-services.json in your app folder. The Google services plugi...