16 July 2014

Make a calling Android Application

Here we will learn the steps / code to make a call from your android application .

What are we going to do ?
-> We will make a button and when we press the button we will be able to call on the desired number


What we need ?
  1. A Relative layout
  2. A Button
  3. The number you desire to call
Step 1 : Make a button on your xml page (activity_main.xml) in res folder as shown below .

The code for the above layout is given below :
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#FF0000" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#F0F0FF"
        android:padding="10dp"
        android:textSize="30dp"
        android:text="Make a Call to 0000" />
</RelativeLayout&gt

Step 2 : Make changes in the MainActivity.java file in the src folder .

MainActivity.java
package com.mia.makecall;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

    Button callbutton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        callbutton = (Button) findViewById(R.id.button1);
        
callbutton.setOnClickListener(new OnClickListener(){
            
            public void onClick(View arg0){
                Intent i1 = new Intent(android.content.Intent.ACTION_DIAL,Uri.parse("tel:0000"));
                startActivity(i1);
            }
        });
    }
}

You can change the number (marked in green) to the number you desire to call

Step 3 : Add CALL permission in AndroidManifest.xml file .

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mia.makecall"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
      

<uses-permission android:name="android.permission.CALL_PHONE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mia.makecall.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Step 4 : Run the project

When you click the button you see a keypad with the number we used in our code . You can call the number pressing the call button  .

NOTE :If you want to directly call the desired number without letting the keypad to be seen , just change the word ACTION_DIAL with DIAL in your java file (MainActivity.java) .

  • In the xml file (activity_main.xml) we just made a button with id button1 which when clicked performs an action of calling a particular number .
Button callbutton;
  • In the java file MainActivity.java we made an instance of a Button ,then we linked it with the button in activity_main.xml .
callbutton = (Button) findViewById(R.id.button1);
  • By call the setOnClickListener() with the instance of our button we provide an action of our choice to the button .
callbutton.setOnClickListener(new OnClickListener(){
  public void onClick(View arg0){
  • Intent is an used to perform a specialized function . A Uri is used to take input from its parameters and display it to the specific place . The Uri takes the number as the input and displays it on the dialpad using tel function .
Intent i1 = new Intent(android.content.Intent.ACTION_DIAL,Uri.parse("tel:0000"));
                startActivity(i1
  • We have to include CALL_PHONE permission in AndroidManifest.xml which gives us access to the components in the phone (call opertion inbuild in the phone)
<uses-permission android:name="android.permission.CALL_PHONE"/>

Stay tuned with Made In Android

Previous Page Next Page Home
Top