Android WhatsApp Integration Tutorial With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.
In this tutorial, we will go through android whatsApp integration tutorial with example. We will see how to share text, image or video using whatsApp application in kotlin.

Social media always plays a vital role in application promotion. It opens connectivity to people connected through social media. So, it’s a good choice to integrate social media in your android application. In this post, we will see how to integrate whatsApp in your application to share text, image or video.

Download Source Code in Kotlin or Java

Getting Started

You may need to integrate the whatsApp in your application to share the data with other whatsApp user. The data may be text, image or video. So, in this tutorial, we will see how to integrate whatsApp to share –
(A) Text
(B) Image
(C) Video
in your android application.

Note – The tutorial will be covered in kotlin. However, you can download source code to get the sample application in java.

Now, we will start the tutorial, on android whatsApp integration, by creating a new application first. Then, going through each data type (text, image or video) one by one.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as WhatsAppIntegration. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly. If you want, visit post to get the steps in detail to create a new project

Since we have created a new project successfully, we will do some basic setup for android whatsApp integration.

2. Modify values Folder

Now, we will add constants (dimensions, strings etc.) in values folder. These constant values will be used later in the application.

So, Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">WhatsAppIntegration</string>
	<string name="hint_text_share">Enter text to share</string>
	<string name="share_text">Share Text</string>
	<string name="share_image">Share Image</string>
	<string name="share_video">Share Video</string>
	<string name="enter_text">Please enter some text</string>
	<string name="select_video">Select Video</string>
	<string name="select_picture">Select Picture</string>
	<string name="choose_media_title">Tutorialwing | Share Using</string>
	<string name="sharing_image">Tutorialwing | Sharing Image</string>
	<string name="sharing_video">Tutorialwing | Sharing Video</string>
</resources>

Now, open res/values/dimens.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<dimen name="button_margin_bottom">10dp</dimen>
	<dimen name="share_title">220dp</dimen>
	<dimen name="default_padding">10dp</dimen>
</resources>

We have completed the basic setup.

Now, we will take a next step to see how to share text on android whatsApp integration tutorial.

3.1 Share Text on WhatsApp Programmatically

You can share text on whatsApp as shown below –

val textToShare = "Hello Tutorialwing!"
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, textToShare)
sendIntent.type = "text/plain"
startActivity(sendIntent)

Here, variable textToShare contains the text that you want to share on whatsApp. You can change it as per your need. Or, you can get it using EditText from your customer in the application. Now, we will see how to achieve it in our application.

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical"
	android:padding="@dimen/default_padding">

	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:orientation="horizontal"
		android:paddingBottom="@dimen/default_padding"
		android:paddingTop="@dimen/default_padding">

		<EditText
			android:id="@+id/shareText"
			android:layout_width="@dimen/share_title"
			android:layout_height="wrap_content"
			android:hint="@string/hint_text_share"/>

		<Button
			android:id="@+id/shareIt"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="@string/share_text"/>

	</LinearLayout>

</LinearLayout>

Here, we have defined an editText, to take input from user, and a button to share entered text on whatsApp.

Now, open main/java/com.tutorialwing.whatsappintegration/MainActivity.kt file. Then, add below code into it.

package com.tutorialwing.whatsappintegration

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnShare = findViewById<Button>(R.id.shareIt)
        btnShare?.setOnClickListener {
            val editText = findViewById<EditText>(R.id.shareText)
            if (editText != null) {
                val text = editText.text.toString()
                if (!text.isEmpty()) {
                    startShareText(text)
                } else {
                    Toast.makeText(applicationContext, R.string.enter_text, Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    private fun startShareText(text: String?) {
        val sendIntent = Intent()
        sendIntent.action = Intent.ACTION_SEND
        sendIntent.putExtra(Intent.EXTRA_TEXT, text)
        sendIntent.type = "text/plain"
        startActivity(sendIntent)

        // Set package only if you do not want to show all the options by which you can share the text.
        // Setting package bypass the system picker and directly share the data on WhatsApp.
        // TODO uncomment code to show whatsapp directly
        // sendIntent.setPackage("com.whatsapp");

        startActivity(sendIntent)
    }
}

Above code shows a complete example to share any text on whatsApp programmatically in kotlin.

Now, we will take a next step to see how to share image on android whatsApp integration tutorial.

3.2 Share Image on WhatsApp Programmatically

You can share image on whatsApp programmatically as shown below –

val text = "Image Sharing via Tutorialwing!"
Uri uri = Uri.parse("uriString that references image to share");
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "image/*"
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri)
sharingIntent.putExtra(Intent.EXTRA_TEXT, text)
startActivity(Intent.createChooser(sharingIntent, getString(R.string.choose_media_title)))

Here, uri is Uri of the image which you want to share. text is the text which you want to show with image.

Whenever we want the user to share image, we also provide them a way to choose the image which he/she wants to share. We can achieve it using onActivityResult method in activity.

Now, we will show a button in mainActivity. Upon clicking on that button, a dialog will be prompted to choose the image you want to share. Then, after choosing the image, a new dialog will open to choose the contact with whom you will share the image.

SO, open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical"
	android:padding="@dimen/default_padding">

	<Button
		android:id="@+id/shareImage"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginBottom="@dimen/button_margin_bottom"
		android:text="@string/share_image"/>

</LinearLayout>

Here, we have just defined a button that will be used to share the image.

Now, open main/java/com.tutorialwing.whatsappintegration/MainActivity.kt file. Then, add below code into it.

package com.tutorialwing.whatsappintegration

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button

class MainActivity : AppCompatActivity() {

    private val pictureCode = 101

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnShareImage = findViewById<Button>(R.id.shareImage)
        btnShareImage?.setOnClickListener { selectImage() }
    }

    private fun selectImage() {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT
        startActivityForResult(Intent.createChooser(intent, getString(R.string.select_picture)), pictureCode)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (data != null && resultCode == Activity.RESULT_OK && (requestCode == pictureCode)) {
            startShareImage(getString(R.string.sharing_image), data.data)
        }
    }

    private fun startShareImage(text: String, uri: Uri?) {
        val sharingIntent = Intent(Intent.ACTION_SEND)
        sharingIntent.type = "image/*"
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri)
        sharingIntent.putExtra(Intent.EXTRA_TEXT, text)
        startActivity(Intent.createChooser(sharingIntent, getString(R.string.choose_media_title)))
    }
}

Here, we have added code to share a chosen image on whatsApp programmatically in kotlin. Upon click the share button, you need to choose the image and contact to share the image.

Now, we will take a next step to see how to share video on android whatsApp integration tutorial.

3.3 Share Video on WhatsApp Programmatically

Now, we will see how to share video on whatsApp programmatically in kotlin. You can do it as below –

Uri uri = Uri.parse("uriString to the video to share")
val text = "Sharing video via Tutorialwing!"
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "video/*"
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri)
sharingIntent.putExtra(Intent.EXTRA_TEXT, text)
startActivity(Intent.createChooser(sharingIntent, getString(R.string.choose_media_title)))

Now, we will add to code to make the user enable to select the video and user with which the selected video will be shared.

So, open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical"
	android:padding="@dimen/default_padding">

	<Button
		android:id="@+id/shareVideo"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/share_video"/>

</LinearLayout>

Here, we have defined a button that will prompts a screen to select video and contact with which you want to share the video. So, we will access this button in kotlin file and set click listener to perform required actions.

Now, open main/java/com.tutorialwing.whatsappintegration/MainActivity.kt file. Then, add below code into it.

package com.tutorialwing.whatsappintegration

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button

class MainActivity : AppCompatActivity() {

    private val videoCode = 102

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnShareVideo = findViewById<Button>(R.id.shareVideo)
        btnShareVideo?.setOnClickListener { selectVideo() }
    }

    private fun selectVideo() {
        val intent = Intent()
        intent.type = "video/*"
        intent.action = Intent.ACTION_GET_CONTENT
        startActivityForResult(Intent.createChooser(intent, getString(R.string.select_video)), videoCode)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (data != null && (resultCode == Activity.RESULT_OK) && (requestCode == videoCode)) {
           startShareVideo(getString(R.string.sharing_video), data.data)
        }
    }

    private fun startShareVideo(text: String, uri: Uri?) {
        val sharingIntent = Intent(Intent.ACTION_SEND)
        sharingIntent.type = "video/*"
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri)
        sharingIntent.putExtra(Intent.EXTRA_TEXT, text)
        startActivity(Intent.createChooser(sharingIntent, getString(R.string.choose_media_title)))
    }
}

Here, we have added code to share video on whatsApp programmatically in kotlin. When you click on button, a screen will be shown to select the video and user to share the video.

That’s end of tutorial on android whatsApp integration.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners