Home Next Tutorial

How To send simple text message (SMS) from mobile in Android


Step 1: Open Android Studio
Step 2: Select File->New->New Project
Step 3: In the Create New Project window type Application Name as "SMSDemo" and click on Next button. In the Target Android Devices select Minimum SDK and click on Next Button. In The Add an Activity to Mobile select blank activity and click on Next Button.Click on Finish button.
Step 4: Design an activity as shown below:

SMS Example
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mobile Number:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#010101" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/etMobile"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text Message:"
android:id="@+id/textView2"
android:layout_below="@+id/etMobile"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#010101" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etMessage"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS"
android:id="@+id/btnSend"

android:layout_below="@+id/etMessage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

Step 5: Open MainActivity.java file and write following code in the onCreate() method to send SMS when user click on Push Button.

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et1 = (EditText)findViewById(R.id.etMobile);
final EditText et2 = (EditText)findViewById(R.id.etMessage);
final Button b1 = (Button)findViewById(R.id.btnSend);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String Mobile = et1.getText().toString();
String Message = et2.getText().toString();
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(Mobile,null,Message,null,null);
Toast.makeText(getApplicationContext(),"SMS Sent",Toast.LENGTH_LONG).show();
}
});
}

Step 6:In order to send SMS you have to set permission in AndroidManifest.xml file as shown below:

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

Step 7:Run the application. As you click on Send SMS button it will send text message to particular mobile number

Download Projects


Download Programs