In this android example we see program for mathematics function like SUM or Addition of two numbers,Subtraction,Division,Multiplication just like a Calculator for android. In this android tutorial we write a very simple code to develop a simple calculator for android.let's see step by step android example
CREATE NEW ANDROID PROJECT:
Step 1: Write code into activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Maths Function" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="@+id/buttonsum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sum/Addition" />
<Button
android:id="@+id/buttonsub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtraction" />
<Button
android:id="@+id/buttondiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Division" />
<Button
android:id="@+id/buttonmul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="multiplication" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.mathsfunapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create object
Button btnsum = (Button) findViewById(R.id.buttonsum);
Button btnsub = (Button) findViewById(R.id.buttonsub);
Button btndiv = (Button) findViewById(R.id.buttondiv);
Button btnmul = (Button) findViewById(R.id.buttonmul);
final EditText etv = (EditText) findViewById(R.id.editText1);
final EditText etv2 = (EditText) findViewById(R.id.editText2);
final TextView result = (TextView) findViewById(R.id.textView1);
// Create button click event
btnsum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int x = new Integer(etv.getText().toString());
int y = new Integer(etv2.getText().toString());
int sum = x + y; //Perform Maths operation
result.setText("The ANS of " + x + " + " + y + " = " + sum);//print answer
}
});
btnsub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int x = new Integer(etv.getText().toString());
int y = new Integer(etv2.getText().toString());
int sub = x - y; //Perform Maths operation
result.setText("The ANS of " + x + " - " + y + " = " + sub);//print answer
}
});
btndiv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int x = new Integer(etv.getText().toString());
int y = new Integer(etv2.getText().toString());
int div = x / y; //Perform Maths operation
result.setText("The ANS of " + x + " / " + y + " = " + div);//print answer
}
});
btnmul.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int x = new Integer(etv.getText().toString());
int y = new Integer(etv2.getText().toString());
int mul = x * y; //Perform Maths operation
result.setText("The ANS of " + x + " * " + y + " = " + mul);//Print answer
}
});
}
}
NOTE: No need any special permission in AndroidManifest.xml
Step 3: Now Run Your Project:
Addition/Sum:
Download Project:
CREATE NEW ANDROID PROJECT:
Step 1: Write code into activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Maths Function" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="@+id/buttonsum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sum/Addition" />
<Button
android:id="@+id/buttonsub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtraction" />
<Button
android:id="@+id/buttondiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Division" />
<Button
android:id="@+id/buttonmul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="multiplication" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.mathsfunapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create object
Button btnsum = (Button) findViewById(R.id.buttonsum);
Button btnsub = (Button) findViewById(R.id.buttonsub);
Button btndiv = (Button) findViewById(R.id.buttondiv);
Button btnmul = (Button) findViewById(R.id.buttonmul);
final EditText etv = (EditText) findViewById(R.id.editText1);
final EditText etv2 = (EditText) findViewById(R.id.editText2);
final TextView result = (TextView) findViewById(R.id.textView1);
// Create button click event
btnsum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int x = new Integer(etv.getText().toString());
int y = new Integer(etv2.getText().toString());
int sum = x + y; //Perform Maths operation
result.setText("The ANS of " + x + " + " + y + " = " + sum);//print answer
}
});
btnsub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int x = new Integer(etv.getText().toString());
int y = new Integer(etv2.getText().toString());
int sub = x - y; //Perform Maths operation
result.setText("The ANS of " + x + " - " + y + " = " + sub);//print answer
}
});
btndiv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int x = new Integer(etv.getText().toString());
int y = new Integer(etv2.getText().toString());
int div = x / y; //Perform Maths operation
result.setText("The ANS of " + x + " / " + y + " = " + div);//print answer
}
});
btnmul.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int x = new Integer(etv.getText().toString());
int y = new Integer(etv2.getText().toString());
int mul = x * y; //Perform Maths operation
result.setText("The ANS of " + x + " * " + y + " = " + mul);//Print answer
}
});
}
}
NOTE: No need any special permission in AndroidManifest.xml
Step 3: Now Run Your Project:
Addition/Sum:
Subtraction:
Division:
Multiplication:
int x = new Integer(etv.getText().toString()); is this the only way to take input from the the user, and please explain why you put "new" in the begining plz...
ReplyDeleteSir there is an Error in program :-
ReplyDeleteerror: Package R does not exist
error: Package R does not exist
Why it is coming when program is Run please give Solution.....
in android studio there press alt+enter if not resolve then go to file and click invalid catch and restart then restart your android studio.
DeleteMany sites offer a variety of mortgage loan calculators for use by web surfers and visitors. There are monthly payment calculators, rent versus buy calculators, refinance calculators and on and on. I am taking aim in this series of articles to help you to use these calculators in a more effective way. hours calculator
ReplyDeleteYou can go to all the finest schools, colleges and universities; you cannot get a diploma for intelligence. You develop intelligence, however, a solid education enhances your intelligence. Intelligence is like creativity, you have it or you don't. You cannot teach creativity, you can only enhance it. Similarly, intelligence in an innate ability you have that can be enhanced with solid education. online math class
ReplyDelete