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 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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"
    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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
        }
    });

chevron_right


Below is the complete MainActivity.java file:

MainActivity.java

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


Output:

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.