Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to send message on WhatsApp in Android
Next article icon

How to send message on WhatsApp in Android

Last Updated : 12 Sep, 2023
Comments
Improve
Suggest changes
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.

Approach

Step 1:Open the activity_main.xml file and add the layout code. A message input container as EditText and a Button to send this message is added.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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: Take the reference of EditText and Button in Java file. References are taken using the ids with the help of findViewById() method.

  • Taking reference to EditText
    EditText messageEditText = findViewById(R.id.message);
  • Taking reference to Button
    Button submit = findViewById(R.id.submit);

Step 3: Write function to send message to whatsapp. Create an intent with ACTION_SEND and specify the whatsapp package name to this so that it opens whatsapp directly.

com.whatsapp is the package name for official whatsapp application.
Java
private void sendMessage(String message) {      // Creating new intent     Intent intent         = new Intent(Intent.ACTION_SEND);      intent.setType("text/plain");     intent.setPackage("com.whatsapp");      // Give your message here     intent.putExtra(         Intent.EXTRA_TEXT,         message);      // Checking whether Whatsapp     // is installed or not     if (intent             .resolveActivity(                 getPackageManager())         == null) {         Toast.makeText(                  this,                  "Please install whatsapp first.",                  Toast.LENGTH_SHORT)             .show();         return;     }      // Starting Whatsapp     startActivity(intent); } 

Step 4: Set onClickListener to the button. It takes the text entered by the user and calls the function sendMessage in which the text message is sent as a parameter.

Java
submit.setOnClickListener(     new View.OnClickListener() {         @Override         public void onClick(View view)         {              // Getting the text             // from edit text             String message                 = messageEditText                       .getText()                       .toString();              // Calling the function             // to send message             sendMessage(message);         }     }); 
Below is the complete MainActivity.java file: MainActivity.java
package com.gfg;  import androidx.appcompat     .app.AppCompatActivity;  import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;  public class MainActivity     extends AppCompatActivity {      @Override     protected void onCreate(         Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Taking reference of Edit Text         final EditText messageEditText             = findViewById(R.id.message);          // Taking reference to button         Button submit             = findViewById(R.id.submit);          submit.setOnClickListener(             new View.OnClickListener() {                 @Override                 public void onClick(View view)                 {                      // Getting the text                     // from edit text                     String message                         = messageEditText                               .getText()                               .toString();                      // Calling the function                     // to send message                     sendMessage(message);                 }             });     }      private void sendMessage(String message)     {          // Creating new intent         Intent intent             = new Intent(                 Intent.ACTION_SEND);          intent.setType("text/plain");         intent.setPackage("com.whatsapp");          // Give your message here         intent.putExtra(             Intent.EXTRA_TEXT,             message);          // Checking whether Whatsapp         // is installed or not         if (intent                 .resolveActivity(                     getPackageManager())             == null) {             Toast.makeText(                      this,                      "Please install whatsapp first.",                      Toast.LENGTH_SHORT)                 .show();             return;         }          // Starting Whatsapp         startActivity(intent);     } } 

Output:


Next Article
How to send message on WhatsApp in Android

A

aman neekhara
Improve
Article Tags :
  • Java
  • Android
Practice Tags :
  • Java

Similar Reads

    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 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 Add Images Directly to WhatsApp in Android?
    An image is a UI widget used to show images or upload images in an Android app. It is the sub-class of the View class. We can use it to add many images, like Bitmaps, and Drawable files. To use it a person can add ImageView to their XML files. Passing its id to the resource file. A sample video is g
    3 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
    How to Block Spam Text Messages on Android & iPhone
    Today, spam text messages are more than just annoying; they can also be dangerous. People using both Android and iPhone get lots of unwanted messages. These messages can be ads or even tricks to steal your information. Millions of smartphone users deal with spam texts every day. This can be very ups
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences