Open chat page in Whatsapp for given number in Android

You can use this functionality of opening any Whatsapp number from your Android app for contact us page or in the send us feedback page. You can set your number in the code and when the user taps on the contact us button, feedback button or any other button, they will redirect to the Whatsapp app with your chat page open. So they can type and send message to you directly.

Open chat page in Whatsapp for given number
Open chat page in Whatsapp for given number

Now we will see the code for that. From now on we will use the Kotlin as our primary language.

First, we will see the layout where you can enter the phone number of the user with the country code. And then press the button to open the Whatsapp app.

<androidx.constraintlayout.widget.ConstraintLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context="com.acomputerengineer.OpenWhatsappNumberActivity">

  <EditText
    android:id="@+id/et"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/enter_phone_number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

  <Button
    android:id="@+id/btn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/open_in_whatsapp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/et" />

</androidx.constraintlayout.widget.ConstraintLayout>

Now we will see the Kotlin code for that.

btn.setOnClickListener {
  val phoneNumber = et.text.toString()
  val url = "https://api.whatsapp.com/send?phone=$phoneNumber"
  try {
    packageManager.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES)
    val i = Intent(Intent.ACTION_VIEW)
    i.data = Uri.parse(url)
    startActivity(i)
  } catch (e: PackageManager.NameNotFoundException) {
    Toast.makeText(this, "Whatsapp is not installed in your phone.", Toast.LENGTH_SHORT).show()
    e.printStackTrace()
  }
}

Github repository link: https://github.com/dakshbhatt21/a-computer-engineer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s