Here we will discuss about how to send SMS from an Android application Programmatically in a very simple way
SMS messaging has become an integral part of every mobile phone and for the past decade every mobile device be it a smartphone or a simple mobile phone has come with an sms messaging feature.
In this article, we will be having two text view one for entering number and one for text or message and one send button. On the tap of it, the message will send to number entered number.
Steps for Sending SMS in Android Programmatically
Step 1 : create a layout in the project in layout folder
<LinearLayout 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:orientation=”vertical” >
<EditText
android:id=”@+id/EditText_PhoneNumber”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Enter Number”
android:inputType=”phone” >
</EditText>
<RelativeLayout
android:layout_width=”wrap_content”
android:layout_height=”250dp” >
<EditText
android:id=”@+id/MainActivity_Message”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:ems=”10″
android:hint=”Enter message”
android:inputType=”textMultiLine” />
</RelativeLayout>
<Button
android:id=”@+id/Send_msg_Btn”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Send_Message” />
</LinearLayout>
Also Read : Best Coding Practices for Android Development
Step 2:- create a MainActivity.java
package com.example.smsdemo;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button send;
EditText phone_Number, message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.Send_msg_Btn);
phone_Number = (EditText) findViewById(R.id.EditText_PhoneNumber);
message = (EditText) findViewById(R.id.MainActivity_Message);
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String phone_Num = phone_Number.getText().toString();
String send_msg = message.getText().toString();
try {
SmsManager sms = SmsManager.getDefault(); // using android SmsManager sms.sendTextMessage(phone_Num, null, send_msg, null, null); // adding number and text
} catch (Exception e) {
Toast.makeText(this, “Sms not Send”, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
Step 3:- AndroidManifest
To send the sms in android you need to add one permission mentioned below.
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.smsdemo”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />
<uses-permission android:name=”android.permission.SEND_SMS”/>
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.smsdemo.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>