How To Open Whatsapp In Android Programmatically?

how to open whatsapp in android programmatically

Android is the leading platform not because of its availability but because of its programmability. This ability to integrate any app with another is so great that WhatsApp, a universal messaging app, is not free from it. But how to open WhatsApp in Android programmatically?

After declaring two EditTexts and one Button in your XML file, you can use the Android Studio to link your app with WhatsApp.

This will send the messages you want to send to the phone numbers you want to send them. But if you simply want to just link your app with WhatsApp, you can use Intent. This way, you will have fewer lines of code to work with.

No matter what caliber of Android developer you are, you must first know the reasons for your code. Blindly coding will never help you write the program that suits your app.

Android Programmatically, What Is It?

Android phones are the most sought-after device in the world. And this popularity made the Android device a center of attention for all developers. That’s why they constantly try to create programs with the Intent to integrate their unique nature.

The most benefit of using the android studio to make a program for Android devices is the ability to link apps. An android developer can take any Android app and connect it with another app with ease. The same is true for the WhatsApp API that you can get from the Google Play Store.

You can even link WhatsApp to send a message to a particular number. But to do that, you need to know how to open WhatsApp using Intent in your Android app. The task is not that hard as long as you know the process.

Add WhatsApp to Your App

If you already have an Android app in mind, you must have already developed the code using Android Studio. It is the best way to make an Android app and link it to other existing ones. So, if you want to send a message to a specific contact through WhatsApp from another app, follow these steps.

Step 01: Open Android Studio.

Step 02: Create a new project using your preferred layout.

Step 03: Once the project is ready, go to the res folder on the left panel of the Android Studio.

Step 04: Under res, you will find the layout folder and click on it.

Step 05: Select the activity_main.xml.

how to open whatsapp in android programmatically


Step 06: Within the activity_main.xml, create two EditText.

Step 06-a: The first EditText is for the contact that you want to send the message to. The code would be similar to the following.

<EditText

//This is the text box for the number you want to text to

android:id=”@+id/edit_mobile_no”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:layout_margin=”10dp”

android:hint=”Enter Mobile Number”

android:inputType=”number”

app:layout_constraintBottom_toBottomOf=”parent”

app:layout_constraintTop_toTopOf=”parent”

tools:ignore=”MissingConstraints”

tools:layout_editor_absoluteX=”16dp”>

</EditText>

Step 06-b: For the second EditText, the intent is the message that you want to send. Use a similar code as below.

<EditText

//This is the text box for the text or message that you want to send

android:id=”@+id/edit_message”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:layout_margin=”10dp”

android:layout_marginTop=”76dp”

android:hint=”Enter Message”

android:inputType=”text”

app:layout_constraintTop_toBottomOf=”@+id/edit_mobile_no”

tools:ignore=”MissingConstraints”

tools:layout_editor_absoluteX=”16dp”>

</EditText>

Step 07: Now create a Button right under the second EditText. This is the button that will confirm your action within the app so the code should be similar to the following.

<Button

//This button is the confirmation for your message and contact

android:id=”@+id/button_send”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”Send”

app:layout_constraintBottom_toBottomOf=”parent”

app:layout_constraintEnd_toEndOf=”parent”

app:layout_constraintStart_toStartOf=”parent”

app:layout_constraintTop_toBottomOf=”@+id/edit_message”

tools:ignore=”MissingConstraints”>

</Button>

how to open whatsapp in android programmatically

Step 08: After the Button is done, go back to the Java file.

Step 09: Select the MainActivity.

Step 10: Within the MainActivity, create an object for both EditText and Button as follows.

Button button = findViewById(R.id.button_send);

EditText editText_mobile = findViewById(R.id.edit_mobile_no);

EditText editText_msg = findViewById(R.id.edit_message);

Step 11: Now using the Calling Listener on the Button you can WhatsApp call android programmatically.

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//This is so you can get the EditText value

String mobileNumber = editText_mobile.getText().toString();

String message = editText_msg.getText().toString();

}

}

Step 12: After this, you will have to add the program that will check if the number you inserted has WhatsApp messenger or not.

Step 12-a: First, add the following boolean under the button calling listener.

boolean installed = appInstalledOrNot(“com.whatsapp”);

Step 12-b: And outside the button check, add the main private boolean to complete the check.

private boolean appInstalledOrNot(String url){

//Create method appInstalledOrNot

PackageManager packageManager =getPackageManager();

boolean app_installed;

try {

packageManager.getPackageInfo(url,PackageManager.GET_ACTIVITIES);

app_installed = true;

}catch (PackageManager.NameNotFoundException e){

app_installed = false;

}

return app_installed;

}

Step 13: Once the boolean is set, make the logic that will check the returned value from the boolean. This part of the code should be under theboolean from Step 12-a.

if (installed){

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setData(Uri.parse(“http://api.whatsapp.com/send?phone=”+”91″+mobileNumber + “&text=”+message));

startActivity(intent);

}

else {

Toast.makeText(MainActivity.this, “Whats app not installed on your device”, Toast.LENGTH_SHORT).show();

}

Step 14: With this, the coding part is done. Now you will have to debug and run the code.

how to open whatsapp in android programmatically

This process is if you want to send a WhatsApp message programmatically Android without Intent. But there is a process to do the task with Intent if you wish. Nevertheless, if you follow the process correctly, you will end up with the following code structure.

how to open whatsapp in android programmatically
how to open whatsapp in android programmatically

Link With Intent

After the last part, if you are still confused about WhatsApp API, you might need to do the task with Intent. The answer to the question “how to share text on WhatsApp in android programmatically” is not one. Here are all the things you need to know to complete the task with Intent.

Send A Particular Automated Message

If you want to send a particular message using WhatsApp in the android studio, you must use the following steps.

Step 01: First, create the public void for your app.

public void openWhatsApp(View view){

}

Step 02: Within the public void, declare the package manager.

PackageManager pm=getPackageManager();

Step 03: After this add the part that will hold your message. For this, use the try command.

try {

Intent waIntent = new Intent(Intent.ACTION_SEND);

waIntent.setType(“text/plain”);

String text = “Hello, how you doinn”;

//You can replace this part with the message you wish to send

}

Step 04: Now, under the try command, add PackageInfo. This part of the code checks whether the contact entered has WhatsApp activated or not. Thus, the connection between your app and WhatsApp web is established.

PackageInfo info=pm.getPackageInfo(“com.whatsapp”, PackageManager.GET_META_DATA);

//This part will check if the number inserted has WhatApp or not by connecting with the WhatsApp web

waIntent.setPackage(“com.whatsapp”);

waIntent.putExtra(Intent.EXTRA_TEXT, text);

startActivity(Intent.createChooser(waIntent, “Share with”));

Step 05: For the part that returns a not installed value, use the catch command. This part will check the value that the PackageInfo returns and display a negative message if the target doesn’t have WhatsApp.

catch (PackageManager.NameNotFoundException e) {

Toast.makeText(this, “This user doesn’t have WhatsApp not Installed”, Toast.LENGTH_SHORT)

//This is the message which will be displayed for non-existent WhatsApp numbers

.show();

}

catch(Exception e){

e.printStacktrace();

}

If all steps were followed properly, the final code will be somewhat similar to this.

public void openWhatsApp(View view){

PackageManager pm=getPackageManager();

try {

Intent waIntent = new Intent(Intent.ACTION_SEND);

waIntent.setType(“text/plain”);

String text = “Hello, how you doinn”;

PackageInfo info=pm.getPackageInfo(“com.whatsapp”, PackageManager.GET_META_DATA);

waIntent.setPackage(“com.whatsapp”);

waIntent.putExtra(Intent.EXTRA_TEXT, text);

startActivity(Intent.createChooser(waIntent, “Share with”));

}

catch (PackageManager.NameNotFoundException e) {

Toast.makeText(this, “This user doesn’t have WhatsApp not Installed”, Toast.LENGTH_SHORT)

.show();

}

catch(Exception e){

e.printStacktrace();

}

}

Sent To Different Phone Numbers

Now, if your Intent is to use Android to send WhatsApp messages to a specific number, then the last code will not work. Follow these steps to open a particular contact on the WhatsApp app using your Android app.

Step 01: Create the public void and package manager same as before.

public void openWhatsApp(View view){

PackageManager pm=getPackageManager();

}

Step 02: Using the try command, write the code that will input the number you want to open.

try {

String toNumber = “abcdefghi”;

//The “abcdefghi” here is a placeholder. You can change the value to the contact you wish to search

//Make sure to enter the contact that you want to search without the country or region code

}

Step 03: Under the same try command, insert the code that will connect with WhatsApp web and try to open the contact on the app.

Intent sendIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse(“smsto:” + “” + toNumber /*+ “?body=” + “”));

sendIntent.setPackage(“com.whatsapp”);

startActivity(sendIntent);

Step 04: Finally, add the catch command that will declare the message which will be displayed if the entered contact is unavailable in WhatsApp web.

catch (Exception e){

e.printStackTrace();

Toast.makeText(MainActivity.this,”Seems like the contact you are looking for doesn’t use WhatsApp”,Toast.LENGTH_LONG).show();

}

This code is perfect for searching for a contact you entered in your app and opening it using the WhatsApp app. Here is the final code if you are confused by the steps.

public void openWhatsApp(View view){

PackageManager pm=getPackageManager();

try {

String toNumber = “abcdefghi”;

Intent sendIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse(“smsto:” + “” + toNumber /*+ “?body=” + “”));

sendIntent.setPackage(“com.whatsapp”);

startActivity(sendIntent);

}

catch (Exception e){

e.printStackTrace();

Toast.makeText(MainActivity.this,”Seems like the contact you are looking for doesn’t use WhatsApp”,Toast.LENGTH_LONG).show();

}

}

Specify Both, Number and Message

We already talked about sending a particular message to a specific user. But there is another way to call the WhatsApp plugin for Android using Intent. Follow these steps, and you will be all good.

Step 01: Declare the public void but not the package manager.

public void openWhatsApp(View view){

}

Step 02: Use the same try command code to input the contact you want to send the message to.

try {

String toNumber = “abcdefghi”;

//The “abcdefghi” here is a placeholder. You can change the value to the contact you wish to search

//Make sure to enter the contact that you want to search without the country or region code

}

Step 02: Now, within the same command, add the string for the text you wish to send.

String text = “The cake is a LIE”;

//Feel free to change this part with the message you wish to send

Step 03: Within the same try command, add a check program.

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setData(Uri.parse(“http://api.whatsapp.com/send?phone=”+toNumber +”&text=”+text));

startActivity(intent);

Step 04: Finally add the catch for the return message.

catch (Exception e){

e.printStackTrace();

Toast.makeText(MainActivity.this,”Seems like the contact you are looking for doesn’t use WhatsApp”,Toast.LENGTH_LONG).show();

}

With these steps, the final code should be somewhat as the following.

public void openWhatsApp(View view){

try {

String toNumber = “abcdefghi”;

String text = “The cake is a LIE”;

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setData(Uri.parse(“http://api.whatsapp.com/send?phone=”+toNumber +”&text=”+text));

startActivity(intent);

}

catch (Exception e){

e.printStackTrace();

Toast.makeText(MainActivity.this,”Seems like the contact you are looking for doesn’t use WhatsApp”,Toast.LENGTH_LONG).show();

}

}

By Imran Chowdhury

Imran is a tech-savvy guy, love to share the hacks of technology related tips and tricks, and a certified learner from Android developer platform He has been in the industry since 2017. With his vast knowledge and hundreds of blog publications, he can solve any tech-related problem flawlessly. The main areas of expertise are android and ios app-related problems, tech-tutorial, and operating systems. He explains rocket-science-type difficult things easily so you can follow instructions and solve problems. He is currently working as the founder of Writing hack and some other blogs. His love for writing and solving others' problems sets him apart from other writers.

Leave a comment

Your email address will not be published. Required fields are marked *

Index