MAD Lab
MAD Lab
MAD Lab
Procedure:
Open Android Stdio and then click on File -> New -> New project.
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:
Follow these steps to create an SMS app using Android Studio. I have attached the source code
too.
Step 1
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
Step 5
Step 6
Go to activity_main.xml. This XML file contains the designing code for your Android app.
Step 7
Go to Main Activity.java. This Java program is the backend language for Android.
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.
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.