Hello Friends, Today we are going see an example of sending message to whatsapp number in android. WhatsApp Messenger, or simply WhatsApp, is an American freeware, cross-platform messaging and Voice over IP service owned by Facebook, Inc. Actually I have added this feature in one of my office’s android project few days ago. Using this example we can send message to specific whatsapp number or we can open whatsapp with specific message and later we can select contact in whatsapp or we can send specific message to specific whatsapp number as well. There are two buttons to trigger Whatsapp message, you can use any one you want but WHATSAPP 2 button is better because WHATSAPP 1 button doesn’t allow set specific message but can set specific number. So, lets see the code without wasting any more time…
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.zatackcoder.sendingmessagetowhatsapp; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; 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); final EditText countryCodeET = findViewById(R.id.country_code); final EditText mobileET = findViewById(R.id.mobile); final EditText messageET = findViewById(R.id.message); Button button1 = findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String countryCode = countryCodeET.getText().toString().trim(); String mobile = mobileET.getText().toString().trim(); String message = messageET.getText().toString(); try { Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + countryCode + mobile + "?body=" + message)); // I have appended message here but it doesn't work use second method to send message as well //sendIntent.putExtra(Intent.EXTRA_TEXT, message); sendIntent.setPackage("com.whatsapp"); startActivity(sendIntent); } catch (Exception e) { Toast.makeText(getApplicationContext(), "It seems whatsapp not installed on your phone!", Toast.LENGTH_LONG).show(); } } }); Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String countryCode = countryCodeET.getText().toString().trim(); String mobile = mobileET.getText().toString().trim(); String message = messageET.getText().toString(); try { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://api.whatsapp.com/send?phone=" + countryCode + mobile + "&text=" + message)); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } }); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?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" android:orientation="vertical" android:layout_gravity="center" android:gravity="center" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Message to WhatsApp" android:textSize="18dp" android:textColor="#000" android:layout_marginBottom="30dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#FAECEC"> <EditText android:id="@+id/country_code" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="5" android:text="91" android:hint="Country Code" android:singleLine="true" android:ellipsize="end"/> <EditText android:id="@+id/mobile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="Mobile" android:inputType="phone"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="91 is country code here change it accordingly\n if sending on non indian mobile number"/> <EditText android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:lines="3" android:hint="Message" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WhatsApp 1" android:layout_marginTop="20dp"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WhatsApp 2" android:layout_marginTop="20dp"/> </LinearLayout> </LinearLayout> |
Screenshot
Android Studio Project Download
Sending Message to WhatsApp
Thanks for Stoping by
If you find this helpful then please do share
Any suggestions and queries are welcome in comment section
Comments