Android SMS - How to Send Sms in Android Studio using Kotlin.

Last updated Dec 16, 2021

In this android example, we will see how to send message from app in Android Studio by using Kotlin Language.

Implementation:
 

 Create a new Project in android studio.

 Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish

Go to activity_message.xml file and add the following code

 

<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:gravity="center"
    android:padding="20dp"
    android:orientation="vertical"
    tools:context=".MessageActivity">

    <EditText
        android:id="@+id/etPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter mobile number" />
    <EditText
        android:id="@+id/etMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Enter your message" />

    <Button
        android:id="@+id/sendSMS"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Send Message" />

</LinearLayout>

Add Send SMS permission in AndroidManifest.xml file

 

Open MessageActivity.kt file and add the following code below setContentView().

val btn = findViewById(R.id.sendSMS)
        val phoneNo = findViewById(R.id.etPhone)
        val message = findViewById(R.id.etMessage)
        val permissionRequest = 101

 

        fun myMessage() {
            val myNumber: String = phoneNo.text.toString().trim()
            val myMsg: String = message.text.toString().trim()
            if (myNumber == "" || myMsg == "") {
                Toast.makeText(this, "Field cannot be empty", Toast.LENGTH_SHORT).show()
            } else {
                if (TextUtils.isDigitsOnly(myNumber)) {
                    val smsManager: SmsManager = SmsManager.getDefault()
                    smsManager.sendTextMessage("+91 " + myNumber, null, myMsg, null, null)
                    Toast.makeText(this, "Message Sent", Toast.LENGTH_SHORT).show()
                    phoneNo.text.clear()
                    message.text.clear()
                } else {
                    Toast.makeText(this, "Please enter the correct number", Toast.LENGTH_SHORT)
                        .show()
                }
            }
        }

        btn.setOnClickListener {

            val permissionCheck =
                ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
            if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
                myMessage()
            } else {
                ActivityCompat.requestPermissions(
                    this, arrayOf(Manifest.permission.SEND_SMS),
                    permissionRequest
                )
            }

        }

 

Run the app on emulator or real device, you will get the output as following in video .

 

Complete Source Code of Send SMS Example :
 

activity_message.xml file

<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
    android:padding="20dp"
    android:orientation="vertical"
    tools:context=".MessageActivity">

    <EditText
        android:id="@+id/etPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter mobile number" />
    <EditText
        android:id="@+id/etMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Enter your message" />

    <Button
        android:id="@+id/sendSMS"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Send Message" />

</LinearLayout>

MessageActivity.kt file

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telephony.SmsManager
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat


class MessageActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_message)
        val btn = findViewById(R.id.sendSMS)
        val phoneNo = findViewById(R.id.etPhone)
        val message = findViewById(R.id.etMessage)
        val permissionRequest = 101

 

        fun myMessage() {
            val myNumber: String = phoneNo.text.toString().trim()
            val myMsg: String = message.text.toString().trim()
            if (myNumber == "" || myMsg == "") {
                Toast.makeText(this, "Field cannot be empty", Toast.LENGTH_SHORT).show()
            } else {
                if (TextUtils.isDigitsOnly(myNumber)) {
                    val smsManager: SmsManager = SmsManager.getDefault()
                    smsManager.sendTextMessage("+91 " + myNumber, null, myMsg, null, null)
                    Toast.makeText(this, "Message Sent", Toast.LENGTH_SHORT).show()
                    phoneNo.text.clear()
                    message.text.clear()
                } else {
                    Toast.makeText(this, "Please enter the correct number", Toast.LENGTH_SHORT)
                        .show()
                }
            }
        }

        btn.setOnClickListener {

            val permissionCheck =
                ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
            if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
                myMessage()
            } else {
                ActivityCompat.requestPermissions(
                    this, arrayOf(Manifest.permission.SEND_SMS),
                    permissionRequest
                )
            }

        }
    }

}

 

 

Conclusion: In this article we have covered how to send message from app in Android Studio by using Kotlin Language.

<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
    android:padding="20dp"
    android:orientation="vertical"
    tools:context=".MessageActivity">

    <EditText
        android:id="@+id/etPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter mobile number" />
    <EditText
        android:id="@+id/etMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Enter your message" />

    <Button
        android:id="@+id/sendSMS"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Send Message" />

</LinearLayout>

MessageActivity.kt file

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telephony.SmsManager
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat


class MessageActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_message)
        val btn = findViewById(R.id.sendSMS)
        val phoneNo = findViewById(R.id.etPhone)
        val message = findViewById(R.id.etMessage)
        val permissionRequest = 101

 

        fun myMessage() {
            val myNumber: String = phoneNo.text.toString().trim()
            val myMsg: String = message.text.toString().trim()
            if (myNumber == "" || myMsg == "") {
                Toast.makeText(this, "Field cannot be empty", Toast.LENGTH_SHORT).show()
            } else {
                if (TextUtils.isDigitsOnly(myNumber)) {
                    val smsManager: SmsManager = SmsManager.getDefault()
                    smsManager.sendTextMessage("+91 " + myNumber, null, myMsg, null, null)
                    Toast.makeText(this, "Message Sent", Toast.LENGTH_SHORT).show()
                    phoneNo.text.clear()
                    message.text.clear()
                } else {
                    Toast.makeText(this, "Please enter the correct number", Toast.LENGTH_SHORT)
                        .show()
                }
            }
        }

        btn.setOnClickListener {

            val permissionCheck =
                ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
            if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
                myMessage()
            } else {
                ActivityCompat.requestPermissions(
                    this, arrayOf(Manifest.permission.SEND_SMS),
                    permissionRequest
                )
            }

        }
    }

}

 

 

Conclusion: In this article we have covered how to send message from app in Android Studio by using Kotlin Language.

-->
Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

2232 Views