GTU Android Program 14 Android Call Intent

      Comments Off on GTU Android Program 14 Android Call Intent

GTU Android Program 14 Android Call Intent

GTU Android Program 14

In android calling is most important functionality. If you wish to directly call on some number you need to use call api. Here I have explained how you can call using android. Be sure to add permission before you run your program.


Step 1 : Create android project

Step 2 : Add following permission into AndroidManifest.xml file.

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

Step 3 : prepare code and run !

 

Location : res\layout

FileName: 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#90CAF9"
    tools:context="gtu.andriod.prog14.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Android Call Intent Example"
        android:textColor="#1565C0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="52dp"
        android:ems="10"
        android:textColor="#000000"
        android:textColorHint="#000000"
        android:hint="Enter Number to Call"
        android:inputType="phone" />

    <Button
        android:id="@+id/button1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:layout_alignBottom="@+id/editText1"
        android:layout_toRightOf="@+id/editText1"
        android:background="@drawable/call"
        android:text=""
        android:textColor="#000000" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:text="Developed By: Adarsh Patel"
        android:textColor="#1565C0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Location : src\package

FileName: ActivityMain.java

package gtu.andriod.prog14;

/*
 *  GTU Android Program 14 Android Call Intent 
 * 
 */


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

public class MainActivity extends Activity {

	EditText et1;
	Button b1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main); 
	
		b1 = (Button) findViewById(R.id.button1);
		et1 = (EditText) findViewById(R.id.editText1);
		
		b1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				//Display message to user that we are calling now...
				Toast.makeText(getApplicationContext(),"Calling to " + et1.getText().toString() + "..." , Toast.LENGTH_LONG).show();
				
				//============================================
				//	Intnet used to call on Custom Number (As per EditText1 value)
				//  First add (CALL_PHONE) permission before using following code
				//===========================================
				Intent i = new Intent(Intent.ACTION_CALL);
				i.setData(Uri.parse("tel:" + et1.getText().toString() ));
				startActivity(i);
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

 

Output 1

GTU Android Program 14

Output 2

GTU Android Program 14

Download Complete Code

Download