Friday, August 4, 2017

Write an android application for calculating using simple calculator.


/*******************************************************************************************
Program Name: Write an android application for calculating using simple calculator.
Author: sujith
Date: 10/9/2016
*******************************************************************************************/

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sujithbaby.calci2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>







activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sujithbaby.calci2.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/e1"
android:textSize="18dp"
android:textStyle="bold"
android:id="@+id/textView"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:inputType="numberDecimal"
android:layout_below="@+id/textView"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/e2"
android:textSize="18dp"
android:textStyle="bold"
android:id="@+id/textView2"
android:layout_below="@+id/editText"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:inputType="numberDecimal"
android:layout_below="@+id/textView2"
/>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/add" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/sub" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/multi" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/divi" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:id="@+id/output"
android:text=""
android:layout_below="@+id/linear2"
/>
</RelativeLayout>



MainActivity.java
package com.example.sujithbaby.calci2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private EditText fnum1,fnum2;
private Button add,sub,multi,div;
private TextView txtResult;
private double num1,num2,result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fnum1=(EditText)findViewById(R.id.editText);
fnum2=(EditText)findViewById(R.id.editText2);
add=(Button)findViewById(R.id.add);
sub=(Button)findViewById(R.id.sub);
multi=(Button)findViewById(R.id.multi);
div=(Button)findViewById(R.id.divi);
txtResult=(TextView)findViewById(R.id.output);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1=Double.parseDouble(fnum1.getText().toString());
num2=Double.parseDouble(fnum2.getText().toString());
result=num1+num2;
txtResult.setText(Double.toString(result));
}
});

sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1=Double.parseDouble(fnum1.getText().toString());
num2=Double.parseDouble(fnum2.getText().toString());
result=num1-num2;
txtResult.setText(Double.toString(result));
}
});

multi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1=Double.parseDouble(fnum1.getText().toString());
num2=Double.parseDouble(fnum2.getText().toString());
result=num1*num2;
txtResult.setText(Double.toString(result));
}
});

div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1=Double.parseDouble(fnum1.getText().toString());
num2=Double.parseDouble(fnum2.getText().toString());
result=num1/num2;
txtResult.setText(Double.toString(result));
}
});
}
}








Strings.xml
<resources>
<string name="app_name">simple calculator</string>
<string name="e1">Enter the first number</string>
<string name="e2">Enter the second number</string>
</resources>








OUTPUT















No comments:

Post a Comment