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.
Step 1: Modify activity_main.xml file
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.
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
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.
// Creating intent with action send
val intent = Intent(Intent.ACTION_SEND)
// Setting Intent type
// Setting whatsapp package name
// Give your message here
// Checking whether whatsapp is installed or not
"Please install whatsapp first."
// Starting Whatsapp
- 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.
// Setting on click listener
val message = messageEditText.text.toString()
// Calling the function
Below is the complete MainActivity.kt file
- How to send message on WhatsApp in Android
- Android | How to send data from one activity to second activity
- How to send an Email from your Android App?
- How to create popup message using Alerter Library in android
- Sending a Text Message Over the Phone Using SmsManager in Android
- How to create project in Android Studio using Kotlin
- Running User Interface Thread in Android using Kotlin
- How to Add RangeSeekbar in Android Using Kotlin?
- How to add a custom styled Toast in Android using Kotlin
- Why Kotlin will replace Java for Android App Development
- Android EditText in Kotlin
- Android LinearLayout in Kotlin
- Android RelativeLayout in Kotlin
- Android ListView in Kotlin
- Android FrameLayout in Kotlin
- Android TableLayout in Kotlin
- Android Toast in Kotlin
- Android Fade In/Out in Kotlin
- Android Slide Up/Down in Kotlin
- Android Animations in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.