Friday, 9 October 2015

Android App Returns!! Let's make a call!

In this post we will learn about adding a new Activity in our Application, changing the Launcher (Default) Activity and adding the functionality of calling in our Android App. We suggest you to read this post  first in order to continue.

=>Open an existing project (SmsProject in our case) or create a new one. Right click on app, then Select New->Activity->BlankActivity. In Activity Name text box, type PhoneCall and Click on Finish. A new Activity will be created.

 

Android App

 

 

=>Now, we want to set this PhoneCall Activity as the Default Activity of our app. Open the AndroidManifest.xml file. Notice the tags:
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>


Select the above tags, cut them and paste them after the tag:

 
<activity
android:name=".PhoneCall"
android:label="@string/title_activity_phone_call" >


After this, the file should look like:

 

Android App

 

 

=>Add the permission to call a number by adding the tag:
<uses-permission android:name="android.permission.CALL_PHONE" />


=>Now click on activity_phone_call.xml file. Delete the "hello world" TextView and drag a Number text field from the pallete into the phone's screen.

 

Android App

 

=>Double Click on the Text Box and change the id to "num". Similarly, add a Button to the Activity and change its text to "Call".

 

Android App

 

Android App

 

 

=>In the onClick event of the Button, type "callNow". This function will be defined inside the "PhoneCall.java" file, which will be called when the user clicks on this button.

 

Android App

 

=>Now open the PhoneCall.java file and add the following function inside the PhoneCall class:
public void callNow(View view)
{
EditText e1 = (EditText) findViewById(R.id.num);
Intent callIntent = new Intent(Intent.ACTION_CALL); //implicit intent

callIntent.setData((Uri.parse("tel:" + e1.getText().toString())));
startActivity(callIntent);
}


 

Android App

That's it! You can see how it works by copying the generated apk file onto your android device. Here are some screenshots of the app running on the default emulator:

 

 

Android App

 

Android App

 

 

Android App

 

Android App

 

Feel free to comment, suggest, ask or share!

No comments:

Post a Comment