How to open WhatsApp using an Intent in your Android App

Prateek Mahesh picture Prateek Mahesh · Jul 17, 2016 · Viewed 48.7k times · Source

I want an Intent to take control you directly to WhatsApp. So the moment the user clicks on the button, the Intent is supposed to take you to WhatsApp. This is the code I wrote after following a few guide lines but it doesn't work

buttonWhatsapp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Performs action on click
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
            sendIntent.setType("text/plain");
            sendIntent.setPackage("com.whatsapp");
            startActivity(Intent.createChooser(sendIntent, ""));
            startActivity(sendIntent);
            //opens the portfolio details class
        }
    });

Answer

Pablo Cegarra picture Pablo Cegarra · Aug 27, 2018

Using the 2018 api:

String url = "https://api.whatsapp.com/send?phone="+number;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);