Android Studio is the official Android Integrated Development Environment (IDE) for developing Android Applications. It allows you to create your own Android apps, even if you don't have very good programming skills. The interface is nice and user-friendly, much like the NetBeans IDE for Java Applications. If you know how to program in Java, then learning Android is a cakewalk. In this tutorial we will create a simple Android App that allows you to send SMS.
=>First of all, Install Android Studio. Download the latest version from here: https://developer.android.com/sdk/index.html
=>Open Android Studio. It may download some additional files from the internet. Let it complete and then start the IDE again.
=>Click on "Start a New Android Studio Project" option. In the "Application Name" text box, type "SmsProject" and click Next.


=>By default "Phone and Tablet" CheckBox is selected. In the "Minimum SDK" drop down menu, select "API 21: Android 5.0 (Lollipop)" and click Next.

=>Click on "Blank Activity" option and click Next.

=>In the next screen, keep the default values of Activity Name, Layout Name, Title, Menu Resource Name and click Finish. A new project will be created in the specified path. This step may take some time, depending on your machine's configurations.

=>Our new project has been created. If all went well, you will now see a screen that looks like this:

In Android, each screen or page in an app is called an "Activity". By default, a "MainActivity" is created, whose meta-data is stored in activity_main.xml and its Java code is present in MainActivity.class. You can add more Activities, change the default activity, send information from one activity to another through variables etc. The possibilities are virtually limitless.
=>Now, we will add a button to our "Activity" and if the user clicks on that button, we will call a java function that will send the message. From the pallete that appears on the left side of the phone, drag a button onto the Android Phone that is shown. Place it anywhere as shown below:

=>Double click on the button and change the text to "Send SMS". Then press Enter.

=>Click on the "Hello World" that appears on the upper left corner of the screen and press Delete to remove it. Now click on the button again. On the right hand side you will see a "Properties" section. Scroll down until you see the words "onClick". This event is used to call a function whenever the button is clicked. Double click on the empty space right next to it and type "sending" and click anywhere on the screen. This ("sending") is the name of function that we will define, and will be called when the button is clicked.

=>Now, click on the "MainActivity.java" tab. This is the file where we will define the sending function. Inside the MainActivity class, define the sending function as follows:
NOTE: While typing, the IDE will show suggestions. Just press Down arrow key and select the required suggestion. This ensures that the required libraries are automatically imported into your project, otherwise you may encounter errors.
public void sending(View v)
{
SmsManager sms=SmsManager.getDefault();
String mobNo = "9999999999"; //Change this number with the number to which you want to send the message
String message = "welcome to the world of Android Apps!";
sms.sendTextMessage(mobNo,null,message,null,null);
Toast.makeText(this, "The message has been sent!", Toast.LENGTH_LONG).show();
}
=>Now, we need to get permissions to send sms through our application. On the leftmost side, under the folder "App", there is a folder named "manifests". Click on the arrow next to it. Then select AndroidManifest.xml file by double clicking it. Now, before the <application ...> tag, type:
<uses-permission android:name="android.permission.SEND_SMS" />
Make sure you use the suggestions shown below as you type.

=>Your application is ready! You can test it either by using the Emulator provided with Android Studio, or you can build the project and then copy the "SendSms\app\build\outputs\apk\app-debug.apk" file onto your android device using a USB Cable.
Note that the emulator is not suggested for this project, as you won't be able to send messages through it. Still, if you want to see how it looks, just click on the Green Arrow on the top of the screen or press Shift+F10 to run it. By default, an Android Nexus Device API is provided. Click Ok. After some time, a screen is displayed. Unlock it by dragging the lock upwards. Your app will be shown.
Click on the "Send SMS" button. "The message has been sent" will be shown.

That's it! Congrats, you just made your first Android App! To learn more, go to: http://developer.android.com/index.html
[…] Activity and adding the functionality of calling in our Android App. We suggest you to read this post first in order to […]
ReplyDelete