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