In Android App whats app can be open through intent filter directly. Only thing that need to use package name ‘whatsapp’ to open it in intent filter. (This is due to the fact that ‘whatsapp’ allow third party to open whats app, through it you can pass message and number just like share data)

Source Code:

1. activity_main.xml file

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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”>
 
    <EditText
        android:id=”@+id/et”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignParentStart=”true”
        android:layout_alignParentTop=”true”
        android:layout_marginStart=”85dp”
        android:layout_marginTop=”143dp”
        android:ems=”10″
        android:hint=”Name”
        android:inputType=”textPersonName” />
 
    <Button
        android:id=”@+id/bt”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignParentStart=”true”
        android:layout_alignParentTop=”true”
        android:layout_marginStart=”139dp”
        android:layout_marginTop=”292dp”
        android:text=”Button” />
 
</RelativeLayout>

2. MainActivity.java file

package com.example.whatsappint;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends AppCompatActivity {
 
    EditText e;
    Button b;
    String s;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        e = (EditText)findViewById(R.id.et);
        b = (Button)findViewById(R.id.bt);
 
            b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    s = e.getText().toString();
                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.setType(“text/plain”);
                    i.setPackage(“com.whatsapp”);
                    i.putExtra(Intent.EXTRA_TEXT, s);
                    startActivity(Intent.createChooser(i,”Share”));
                }
            });
 
    }
}

3. AndroidManifest.xml file

 <uses-permission android:name=”android.permission.INTERNET”/>

Output:

Connect with us: