Hi Guys, Welcome to Proto Coders Point, In this android tutorial we will integrate sending message from your android application to whatsapp using whatsapp send message api service.
Video Tutorial on Youtube on android whatsapp intent
Send Message android whatsapp intent
Step 1 : Create a new Android Project
Off-Course you need a android Project to be created in android studio or open any existing project
File > New > New Project give a name to the project as “whatsapp intent send Message” hit the finish button
Step 2 : Adding Country Code Picker library
Open build.gradle (app level) and in dependencies section add the below implementation code.
implementation 'com.hbb20:ccp:2.4.0'
then, After adding the dependencies hit the Sync Now button
Official Site to learn more about Country Code Picker
Step 3 : Custom EditText Background Design
To create a custome EditText Background Design you need to create a new Drawable Resource File to do so Right Click on drawable folder under app > res > drawable
Then, Name the Drawbale Resource file as “rounded_edittext.xml” and paste the below code in it.
<?xml version="1.0" encoding="utf-8"?> <!-- res/drawable/rounded_edittext.xml --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:padding="10dp" android:shape="rectangle" > <solid android:color="#FFFFFF" /> <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" /> </shape>
Step 4 : UI Design
For this project we have 2 editText, a Country Code Picker and a Button to send the message through whatsapp intent.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <ScrollView 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" android:background="@drawable/whatsapp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="WhatsApp Intent Send message Example" android:textColor="@android:color/white" android:gravity="center" android:textStyle="bold" android:layout_marginTop="10dp" android:textSize="20sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" > <com.hbb20.CountryCodePicker app:ccp_arrowColor="#FFF" app:ccp_contentColor="#FFF" android:id="@+id/ccp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/phonenumber" android:layout_width="match_parent" android:layout_height="match_parent" android:hint="Enter Phone Number" android:paddingLeft="10dp" android:inputType="phone" android:background="@drawable/rounded_edittext" android:layout_marginLeft="5dp" /> </LinearLayout> <EditText android:id="@+id/messages" android:layout_width="match_parent" android:layout_height="300dp" android:layout_marginTop="10dp" android:background="@drawable/rounded_edittext" android:gravity="start" android:hint="Enter Your Message" android:inputType="textMultiLine" android:paddingLeft="15dp" android:paddingTop="10dp" /> <Button android:id="@+id/sendbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#4CAF50" android:drawableRight="@drawable/messageicon" android:paddingRight="20sp" android:textColor="#FFF" android:textSize="15sp" android:layout_margin="15dp" android:text="Send Message Through WhatsApp" /> </LinearLayout> </ScrollView>
Step 5 : JAVA code
MainActivity.java
package com.example.whatsappintentsendmsg; 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; import com.hbb20.CountryCodePicker; public class MainActivity extends AppCompatActivity { EditText phoneno,message; CountryCodePicker ccpp; Button sendButton; String messageStr,phoneStr =""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); phoneno = ( EditText) findViewById(R.id.phonenumber); message =(EditText)findViewById(R.id.messages); ccpp=(CountryCodePicker)findViewById(R.id.ccp); sendButton=(Button)findViewById(R.id.sendbutton); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { messageStr = message.getText().toString().trim(); phoneStr = phoneno.getText().toString().trim(); if(phoneStr.isEmpty()) { phoneno.setError("Please Enter Phone"); } else if (messageStr.isEmpty()) { message.setError("Please Enter Message"); }else{ ccpp.registerCarrierNumberEditText(phoneno); phoneStr = ccpp.getFullNumber(); boolean isInstalled = WhatsappAvailable("com.whatsapp"); //if whatsapp is installed it will be true or else isInstalled will become false if(isInstalled) { //Whatsapp send message using Instant Intent whatsapp = new Intent(Intent.ACTION_VIEW,Uri.parse("https://api.whatsapp.com/send?phone="+phoneStr+"&text="+messageStr)); startActivity(whatsapp); phoneno.setText(""); message.setText(""); }else { Toast.makeText(MainActivity.this,"Whatsapp is not Installed on your Device",Toast.LENGTH_SHORT).show(); } } } }); } //check is whatsapp is installed on the Device private boolean WhatsappAvailable(String uri) { PackageManager packageManager = getPackageManager(); boolean isInstalled; try{ packageManager.getPackageInfo(uri,PackageManager.GET_ACTIVITIES); isInstalled = true; }catch (PackageManager.NameNotFoundException e){ isInstalled = false; } return isInstalled; } }