The Wayback Machine - https://web.archive.org/web/20240612065228/https://www.geeksforgeeks.org/how-to-send-sms-in-android-using-kotlin/
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: 



Similar Reads

SMS Verification in Android using SMS User Consent API
Nowadays, SMS verification is that the best manner that's getting used by mobile applications for login purposes. All you would like to try and do is simply enter your mobile number, get a One-Time arcanum and eventually enter that OTP in your application and verify. However, the matter that arises here is that to own the OTP for your application,
5 min read
How to send message on WhatsApp in Android using Kotlin
Whatsapp is the one of most popular messaging App. Many android applications need the functionality to share some messages directly from their app to WhatsApp. For example, if a user wants to share the app or share a message from the app then this functionality comes in use. Either user can send a text or a predefined text can also be sent through
3 min read
How to Send Data Back to MainActivity in Android using Kotlin?
As there are many methods to send the data, but in this article, we will use startActivityForResult() method. Here we need to launch a child activity using startActivityForResult() method. Then from child activity, we can easily send data back to Main Activity. Example: Note: To implement it in java refer to this article: How to send data from one
2 min read
Send Multiple Data From One Activity to Another in Android using Kotlin
There are multiple ways for sending multiple data from one Activity to Another in Android, but in this article, we will do this using Bundle. Bundle in android is used to pass data from one activity to another, it takes data in key and value pairs. To understand this concept we will create a simple project in android studio using Kotlin. Step by St
3 min read
Creating a Anagram Checker Android App in Android Studio with Kotlin
An anagram of a word is another word that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other. In this article, we will be building an Anagram Checker android app in Android Studio using Kotlin and XML. The app will check whether the entered words are anagrams of e
3 min read
Send Email in an Android Application using Jetpack Compose
Many times while building an android application. We have to send a mail to any specific address from our android application. Inside this mail, we have to define the email address, subject, and body to be sent over Gmail or any other mail application. In this article, we will be building a simple application in which we will be able to send mail t
7 min read
Send Message on WhatsApp using Android Jetpack Compose
Many times in android applications we want to provide functionality through which we should be able to send the message directly from our mobile application on WhatsApp. In this article, we will take a look at How to send message from an android application to WhatsApp using Jetpack Compose. Step by Step Implementation Step 1: Create a New Project
6 min read
How to Send Email in Android App Without Using Intent?
It is crucial to establish an email connection in the Android application. This enables us to send important messages to users. For example, when the user joins the app first time sending a welcome email is a common practice. Additionally in some applications if a user signs in from a new location a notification is sent to inform them of this activ
5 min read
Mail and SMS in Flutter
The world works in text. From advertisements to conversations, text is everywhere. The most popular modes of official textual communication are Mails followed by SMS. Companies use these modes to communicate not only with their employees but also with their customers. This had led app developers to include mailing and SMS services in their apps. Fl
6 min read
How to Send Data From Activity to Fragment in Android?
Prerequisites: Introduction to Activities in AndroidIntroduction to Fragments in Android In Android, a fragment is a portion of the user interface that can be used again and again. Fragment manages its own layout and has its own life cycle. Since fragment is a small portion of the bigger user interface, it can only be initialized inside an activity
5 min read
Article Tags :