How to send message on WhatsApp in Android using Kotlin Last Updated : 07 Aug, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 this. This article demonstrates how an android application can send messages on WhatsApp. Whatsapp must be installed on the user’s device. In this article, we will try to create an android app that sends message on WhatsApp using Kotlin. Note: To view how to do this in Java, please refer How to send message on WhatsApp in Android using Java. Approach Step 1: Modify activity_main.xml file Open the activity_main.xml file and add the layout code. activity_main.xml contains a Linear Layout which contains a EditText to input the message and a Button to submit the message. activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <!--EditText to take message input from user--> <EditText android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:hint="Enter you message here" android:lines="8" android:inputType="textMultiLine" android:gravity="left|top" /> <!--Button to send message on Whatsapp--> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Submit" android:background="@color/colorPrimary" android:textColor="@android:color/white"/> </LinearLayout> Step 2: Working with MainActivity.kt file Take the reference of EditText and Button in Kotlin file. References are taken using the ids with the help of findViewById() method. Taking reference of EditText // Referencing the Edit Text val messageEditText = findViewById<EditText>(R.id.message) Similarly taking reference of button // Referencing the button val submit = findViewById<Button>(R.id.submit) Write a function to send message to WhatsApp. Create an intent with ACTION_SEND and specify the whatsapp package name to this so that it opens directly. com.whatsapp is the package name for official WhatsApp application. Kotlin fun sendMessage(message:String){ // Creating intent with action send val intent = Intent(Intent.ACTION_SEND) // Setting Intent type intent.type = "text/plain" // Setting whatsapp package name intent.setPackage("com.whatsapp") // Give your message here intent.putExtra(Intent.EXTRA_TEXT, message) // Checking whether whatsapp is installed or not if (intent.resolveActivity(packageManager) == null) { Toast.makeText(this, "Please install whatsapp first.", Toast.LENGTH_SHORT).show() return } // Starting Whatsapp startActivity(intent) } Set the click listener using setOnClickListener on the button to send the message. Get the text entered by the user and call the function to send message on whatsapp. Kotlin // Setting on click listener submit.setOnClickListener { val message = messageEditText.text.toString() // Calling the function sendMessage(message); } Below is the complete MainActivity.kt file MainActivity.kt package com.gfg import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Referencing the Edit Text val messageEditText = findViewById<EditText>(R.id.message) // Referencing the button val submit = findViewById<Button>(R.id.submit) // Setting on click listener submit.setOnClickListener { val message = messageEditText.text.toString() // Calling the function sendMessage(message); } } fun sendMessage(message: String) { // Creating intent with action send val intent = Intent(Intent.ACTION_SEND) // Setting Intent type intent.type = "text/plain" // Setting whatsapp package name intent.setPackage("com.whatsapp") // Give your message here intent.putExtra(Intent.EXTRA_TEXT, message) // Checking whether whatsapp is installed or not if (intent.resolveActivity(packageManager) == null) { Toast.makeText(this, "Please install whatsapp first.", Toast.LENGTH_SHORT).show() return } // Starting Whatsapp startActivity(intent) } } Output Comment More infoAdvertise with us Next Article How to Send Email in Android App Without Using Intent? A aman neekhara Follow Improve Article Tags : Android Similar Reads How to send message on WhatsApp in Android 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 t 3 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 6 min read How to Send SMS in Android using Kotlin? 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, 4 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 5 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 jav 2 min read How to Create WhatsApp Stories View in Android? Stories are now becoming one of the most seen features in many different apps such as WhatsApp, LinkedIn, Instagram, and many more. In this article, we will take a look at creating a similar type of view in our Android App. What we are going to build in this article? We will be building a simple a 7 min read Like