Open In App

How to Send SMS in Android using Kotlin?

Last Updated : 18 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

SMS Manager is a class in Android which is used to send the SMS to a specific contact from the android application. We can send text messages, data messages, and multimedia messages using this class. There are different methods that are provided to send different types of messages. In this article, we will take a look at How to implement SMS Manager in Android using Kotlin. A sample video is given below to get an idea about what we are going to do in this article.

Note: If you are looking to use SMS manager in android application using JAVA. Check out the following article:  Send SMS in Android using Java

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <!--on below line we are displaying a simple text view-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="10dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="10dp"
        android:padding="10dp"
        android:text="SMS Manager App"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp" />
 
    <!--on below line we are creating a
        simple edit text for phone number-->
    <EditText
        android:id="@+id/idEdtPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:hint="Enter phone number"
        android:inputType="phone" />
 
    <!--on below line we are creating a
         message edit text for message-->
    <EditText
        android:id="@+id/idEdtMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:hint="Enter Message" />
 
    <!--on below line we are creating
        a button for sending message-->
    <Button
        android:id="@+id/idBtnSendMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:padding="5dp"
        android:text="Send Message"
        android:textAllCaps="false" />
 
</LinearLayout>


Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.gtappdevelopers.kotlingfgproject
 
import android.os.Bundle
import android.telephony.SmsManager
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
 
class MainActivity : AppCompatActivity() {
 
    // on below line we are creating variable
    // for edit text phone and message and button
    lateinit var phoneEdt: EditText
    lateinit var messageEdt: EditText
    lateinit var sendMsgBtn: Button
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
         
        // initializing variables of phone edt,
        // message edt and send message btn.
        phoneEdt = findViewById(R.id.idEdtPhone)
        messageEdt = findViewById(R.id.idEdtMessage)
        sendMsgBtn = findViewById(R.id.idBtnSendMessage)
         
        // adding on click listener for send message button.
        sendMsgBtn.setOnClickListener {
             
            // on below line we are creating two
            // variables for phone and message
            val phoneNumber = phoneEdt.text.toString()
            val message = messageEdt.text.toString()
            
            // on the below line we are creating a try and catch block
            try {
               
                // on below line we are initializing sms manager.
                //as after android 10 the getDefault function no longer works
               //so we have to check that if our android version is greater
               //than or equal toandroid version 6.0 i.e SDK 23
                val smsManager:SmsManager
                if (Build.VERSION.SDK_INT>=23) {
                  //if SDK is greater that or equal to 23 then
                  //this is how we will initialize the SmsManager
                    smsManager = this.getSystemService(SmsManager::class.java)
                }
                else{
                  //if user's SDK is less than 23 then
                  //SmsManager will be initialized like this
                    smsManager = SmsManager.getDefault()
                }
                 
                // on below line we are sending text message.
                smsManager.sendTextMessage(phoneNumber, null, message, null, null)
                 
                // on below line we are displaying a toast message for message send,
                Toast.makeText(applicationContext, "Message Sent", Toast.LENGTH_LONG).show()
                 
            } catch (e: Exception) {
               
                // on catch block we are displaying toast message for error.
                Toast.makeText(applicationContext, "Please enter all the data.."+e.message.toString(), Toast.LENGTH_LONG)
                    .show()
            }
        }
    }
}


Step 4: Adding permission to send SMS in AndroidManifest.xml

Navigate to manifest > AndroidManifest.xml and add the below permissions in it. 

XML




<!--on below line we are adding sms permissions-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<dist:module dist:instant="true" />


Now run your application to see the output of it. 

Output: 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads