In this tutorial we will develop an application to send a SMS.

Create a new project -> SMS.

Create two text fields (one to allow the user to enter Phone Number
and the other one for the user to enter a Message) and one send button.

 

<EditText android:id=”@+id/number”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/><EditText android:id=”@+id/message”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>

 

We have to provide permission for our Application to send a sms.
Add, the following line to your AndroidManifest.xml file.

 

<uses-permission android:name=”android.permission.SEND_SMS”></uses-permission>

 

We have to create a listener for the send button.
In SMS.java file add the following code.

 

// send variable will have the instance of the send button(in the UI)
Button send =(Button)findViewById(R.id.send);
send.setOnClickListener(onSave); // Setting up a listener

 

Listener implementation is as follows.

 

EditText number=(EditText)findViewById(R.id.number);
EditText message=(EditText)findViewById(R.id.message);

message.getText().toString();
sendMessage(number.getText().toString(), message.getText().toString());

 

We will define a method – sendMessage that actually implements the SMS functionality.

 

void sendMessage(String number,String msg)
{
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(number, null, msg, null, null);

}

 

The program can now be tested.

 

 

 

Complete code is as given below.

 

AndroidManifest.xml

 

<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.sms”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”15″ />

<application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.SMS”
android:label=”@string/title_activity_sms” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

<uses-permission android:name=”android.permission.SEND_SMS”></uses-permission>

</manifest>

 

activity_sms.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<LinearLayout
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Number:”
/>
<EditText android:id=”@+id/number”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>
</LinearLayout>
<LinearLayout
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Message”
/>
<EditText android:id=”@+id/message”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>
</LinearLayout>
<Button android:id=”@+id/send”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Send SMS”
/>
</LinearLayout>

 

SMS.java

 

package com.example.sms;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMS extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);

Button send =(Button)findViewById(R.id.send);
send.setOnClickListener(onSave);

}

private View.OnClickListener onSave=new View.OnClickListener() {

public void onClick(View v) {

EditText number=(EditText)findViewById(R.id.number);
EditText message=(EditText)findViewById(R.id.message);

message.getText().toString();

sendMessage(number.getText().toString(), message.getText().toString());

}
};

void sendMessage(String number,String msg)
{
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(number, null, msg, null, null);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_sms, menu);
return true;
}
}