MAD Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Simple Android Application for Native Calculator

BY DEVANG · MARCH 1, 2016


Aim:

To develop a Simple Android Application for Native Calculator.

Procedure:

Creating a New project:

 Open Android Stdio and then click on File -> New -> New project.

 Then type the Application name as “ex.no.3″ and click Next.


 Then select the Minimum SDK as shown below and click Next.

 Then select the Empty Activity and click Next.


 Finally click Finish.

 It will take some time to build and load the project.


 After completion it will look as given below.
Designing layout for the Android Application:

 Click on app -> res -> layout -> activity_main.xml.

 Now click on Text as shown below.


 Then delete the code which is there and type the code as given below.
Code for Activity_main.xml:
?
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:orientation="vertical"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:layout_margin="20dp">
8
9 <LinearLayout
10 android:id="@+id/linearLayout1"
11 android:layout_width="match_parent"
12 android:layout_height="wrap_content"
13 android:layout_margin="20dp">
14
15 <EditText
16 android:id="@+id/editText1"
17 android:layout_width="match_parent"
18 android:layout_height="wrap_content"
19 android:layout_weight="1"
20 android:inputType="numberDecimal"
21 android:textSize="20sp" />
22
23 <EditText
24 android:id="@+id/editText2"
25 android:layout_width="match_parent"
26 android:layout_height="wrap_content"
27 android:layout_weight="1"
28 android:inputType="numberDecimal"
29 android:textSize="20sp" />
30
31 </LinearLayout>
32
33 <LinearLayout
34 android:id="@+id/linearLayout2"
35 android:layout_width="match_parent"
36 android:layout_height="wrap_content"
37 android:layout_margin="20dp">
38
39 <Button
40 android:id="@+id/Add"
41 android:layout_width="match_parent"
42 android:layout_height="wrap_content"
43 android:layout_weight="1"
44 android:text="+"
45 android:textSize="30sp"/>
46
47 <Button
48 android:id="@+id/Sub"
49 android:layout_width="match_parent"
50 android:layout_height="wrap_content"
51 android:layout_weight="1"
52 android:text="-"
53 android:textSize="30sp"/>
54
55 <Button
56 android:id="@+id/Mul"
57 android:layout_width="match_parent"
58 android:layout_height="wrap_content"
59 android:layout_weight="1"
60 android:text="*"
61 android:textSize="30sp"/>
62
63 <Button
64 android:id="@+id/Div"
65 android:layout_width="match_parent"
66 android:layout_height="wrap_content"
67 android:layout_weight="1"
68 android:text="/"
69 android:textSize="30sp"/>
70
71 </LinearLayout>
72
73 <TextView
74 android:id="@+id/textView"
75 android:layout_width="match_parent"
76 android:layout_height="wrap_content"
77 android:layout_marginTop="50dp"
78 android:text="Answer is"
79 android:textSize="30sp"
80 android:gravity="center"/>
81
82 </LinearLayout>
 Now click on Design and your application will look as given below.

 So now the designing part is completed.


Java Coding for the Android Application:

 Click on app -> java -> com.example.exno3 -> MainActivity.

 Then delete the code which is there and type the code as given below.
Code for MainActivity.java:
?
1 package com.example.devang.exno3;
2
3 import android.os.Bundle;
4 import android.support.v7.app.AppCompatActivity;
5 import android.text.TextUtils;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.Button;
9 import android.widget.EditText;
10 import android.widget.TextView;
11
12 public class MainActivity extends AppCompatActivity implements OnClickListener
13 {
14 //Defining the Views
15 EditText Num1;
16 EditText Num2;
17 Button Add;
18 Button Sub;
19 Button Mul;
20 Button Div;
21 TextView Result;
22
23 @Override
24 public void onCreate(Bundle savedInstanceState)
25 {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.activity_main);
28
29 //Referring the Views
30 Num1 = (EditText) findViewById(R.id.editText1);
31 Num2 = (EditText) findViewById(R.id.editText2);
32 Add = (Button) findViewById(R.id.Add);
33 Sub = (Button) findViewById(R.id.Sub);
34 Mul = (Button) findViewById(R.id.Mul);
35 Div = (Button) findViewById(R.id.Div);
36 Result = (TextView) findViewById(R.id.textView);
37
38 // set a listener
39 Add.setOnClickListener(this);
40 Sub.setOnClickListener(this);
41 Mul.setOnClickListener(this);
42 Div.setOnClickListener(this);
43 }
44
45 @Override
46 public void onClick (View v)
47 {
48
49 float num1 = 0;
50 float num2 = 0;
51 float result = 0;
52 String oper = "";
53
54 // check if the fields are empty
55 if (TextUtils.isEmpty(Num1.getText().toString()) || TextUtils.isEmpty(Num2.getText().toString()))
56 return;
57
58 // read EditText and fill variables with numbers
59 num1 = Float.parseFloat(Num1.getText().toString());
60 num2 = Float.parseFloat(Num2.getText().toString());
61
62 // defines the button that has been clicked and performs the corresponding operation
63 // write operation into oper, we will use it later for output
64 switch (v.getId())
65 {
66 case R.id.Add:
67 oper = "+";
68 result = num1 + num2;
69 break;
70 case R.id.Sub:
71 oper = "-";
72 result = num1 - num2;
73 break;
74 case R.id.Mul:
75 oper = "*";
76 result = num1 * num2;
77 break;
78 case R.id.Div:
79 oper = "/";
80 result = num1 / num2;
81 break;
82 default:
83 break;
84 }
85 // form the output line
86 Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
87 }
88 }
 So now the Coding part is also completed.
 Now run the application to see the output.
Output:
Result:

Thus a Simple Android Application for Native Calculator is developed and


executed successfully.
Steps to be followed

Follow these steps to create an SMS app using Android Studio. I have attached the source code
too.

Step 1

Open Android Studio and start a new Android Studio Project.

Step 2

You can choose your application name and choose the location where your project is stored. If
you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next"
button.

Step 3

Now, select the version of Android and select the target Android devices. We need to choose
the SDK level which plays an important role in running the application.

Step 4

Now, add the activity and click the "Next" button.

Step 5

Add Activity name and click "Finish".

Step 6

Go to activity_main.xml. This XML file contains the designing code for your Android app.

The XML code is given below.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_toLeftOf="@+id/editText1"
android:text="Mobile No:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignLeft="@+id/textView1"
android:text="Message:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="34dp"
android:layout_marginTop="48dp"
android:text="Send SMS" />
</RelativeLayout>

Step 7
Go to Main Activity.java. This Java program is the backend language for Android.

The java code is given below.

1. package abu.sms;
2.
3. import android.os.Bundle;
4. import android.app.Activity;
5. import android.app.PendingIntent;
6. import android.content.Intent;
7. import android.telephony.SmsManager;
8. import android.view.Menu;
9. import android.view.View;
10. import android.view.View.OnClickListener;
11. import android.widget.Button;
12. import android.widget.EditText;
13. import android.widget.Toast;
14.
15. public class MainActivity extends Activity {
16.
17. EditText mobileno,message;
18. Button sendsms;
19. @Override
20. protected void onCreate(Bundle savedInstanceState) {
21. super.onCreate(savedInstanceState);
22. setContentView(R.layout.activity_main);
23.
24. mobileno=(EditText)findViewById(R.id.editText1);
25. message=(EditText)findViewById(R.id.editText2);
26. sendsms=(Button)findViewById(R.id.button1);
27. sendsms.setOnClickListener(new OnClickListener() {
28.
29. @Override
30. public void onClick(View arg0) {
31. String no=mobileno.getText().toString();
32. String msg=message.getText().toString();
33. Intent intent=new Intent(getApplicationContext(),MainActivity.class);
34. PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
35. SmsManager sms=SmsManager.getDefault();
36. sms.sendTextMessage(no, null, msg, pi,null);
37.
38. Toast.makeText(getApplicationContext(), "Message Sent successfully!",
39. Toast.LENGTH_LONG).show();
40. }
41. });
42. }
43.
44. @Override
45. public boolean onCreateOptionsMenu(Menu menu)
46. getMenuInflater().inflate(R.menu.activity_main, menu);
47. return true;
48. }
49.
50. }

Step 8

We need to make Sender and Receiver requests. So, add Bluetooth permissions in an
AndroidManifest.xml.

The AndroidManifest.xml code is given below.

1. <?xml version="1.0" encoding="utf-8"?>


2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3. package="abu.sms">
4. <uses-permission android:name="android.permission.SEND_SMS"/>
5. <uses-permission android:name="android.permission.RECEIVE_SMS"/>
6.
7. <application
8. android:allowBackup="true"
9. android:icon="@mipmap/ic_launcher"
10. android:label="@string/app_name"
11. android:roundIcon="@mipmap/ic_launcher_round"
12. android:supportsRtl="true"
13. android:theme="@style/AppTheme">
14. <activity android:name=".MainActivity">
15. <intent-filter>
16. <action android:name="android.intent.action.MAIN" />
17.
18. <category android:name="android.intent.category.LAUNCHER" />
19. </intent-filter>
20. </activity>
21. </application>
22.
23. </manifest>

Step 9

Now, go to the menu bar and click the "Make Project" option or press ctrl+f9 to debug the error.
Step 10

Then, click the "Run" button or press shift+f10 to run the project. And, choose the "virtual
machine" option and click OK.

Conclusion

We have successfully created an SMS Android application using the Android Studio. Here is the
output.

You might also like