Monday 25 August 2014

How to Create Android Calling Activity

How to Create Android Calling Activity:



Project: Call From Android
>>>Create a class  CallingActivity.java
package com.sattar.callactivity;

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

public class CallActivity extends Activity {
                public EditText etNumber;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_call);
                                etNumber=(EditText) findViewById(R.id.etNumber);
                }

                public void call(View v) {
                                String number = etNumber.getText().toString();
                                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                                                                +number));
                                startActivity(intent);
            
                }
}
>>>Create an xml file name activity_call.xml and past the code
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CallActivity" >

    <EditText
        android:id="@+id/etNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="158dp"
        android:ems="10"
        android:inputType="phone" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/etCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etNumber"
        android:layout_centerHorizontal="true"
        android:onClick="call"
        android:text="Call" />

</RelativeLayout>

>>>>you have must be needed  the calling or dialing permission so update the manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sattar.callactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <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.sattar.callactivity.CallActivity"
            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>
 
Finally I hope it’s work  properly. If you face any problem comment it, I will try to solution it, thank you.



No comments:

Post a Comment