0

Here's my code of calling my website to webview:

class MainActivity : AppCompatActivity() {
    var website = "https://serrajimmobilier.ma/"
    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main)
    webView.loadUrl(website)
            webView.settings.javaScriptEnabled = true;
            webView.webViewClient = CallBack()}
    class CallBack : WebViewClient (){
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest): Boolean {
                return false;}}}

The problem is when I export my apk the whatssap button doesn't work error when I click into button whatssap

Buddy Christ
  • 1,364
  • 8
  • 22
  • Does this answer your question? [Mobile website "WhatsApp" button to send message to a specific number](https://stackoverflow.com/questions/29218378/mobile-website-whatsapp-button-to-send-message-to-a-specific-number) – Mücahit Yılmaz Mar 04 '20 at 16:53
  • i wanna try to {Opening whatsapp directly from my android app as my website }. (https://serrajimmobilier.ma/ – Soufiane Nassiri Mar 04 '20 at 17:00

1 Answers1

0

Your problem is that you are using a WebView and that WebView tries to open the Whatsapp app just from a link.

You can override the shouldOverrideUrlLoading method so that if the url starts with the whatsapp schema creates an intent for whatsapp:

@Override
fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
    if (url != null && url.startsWith("whatsapp://")) {
        val txtPattern = "&text=([^&]+)".toRegex()
        val match = pattern.find(txtPattern)
        val sendIntent = Intent()
        sendIntent.setAction(Intent.ACTION_SEND)
        sendIntent.putExtra(Intent.EXTRA_TEXT, match?.value :? "Extra text")
        sendIntent.setType("text/plain")
        sendIntent.setPackage("com.whatsapp")
        startActivity(sendIntent)
        return true
    } else {
        // Whatsapp is not installed
        return false
    }
}

Update

To send a message to a specific phone number try this:

@Override
fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
    if (url != null && url.startsWith("whatsapp://")) {
        val patternPhone = "phone=([^&]+)".toRegex()
        val matchPhone = pattern.find(patternPhone)
        val uri = Uri.parse("smsto:" + matchPhone?.value)
        val sendIntent = Intent(Intent.ACTION_SENDTO, uri)
        sendIntent.setPackage("com.whatsapp")
        val shareIntent = Intent.createChooser(sendIntent, null)
        startActivity(shareIntent)
        return true
    } else {
        // Whatsapp is not installed
        return false
    }
}
jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • could u tell me where i can add the target number? in your code – Soufiane Nassiri Mar 04 '20 at 17:04
  • It is going to open the whatsapp app with the text and then you select to which contact/s (or group/s) you want to send it – jeprubio Mar 04 '20 at 17:05
  • Here there is the code to integrate whatsapp (in java): https://faq.whatsapp.com/en/android/28000012 it doesn't seem to have the phone number, sorry – jeprubio Mar 04 '20 at 17:07
  • so my purpose is to contact specific person as my website has been shown https://serrajimmobilier.ma/ you can click into whatssap button to see that – Soufiane Nassiri Mar 04 '20 at 17:10
  • OK, according to https://stackoverflow.com/questions/15462874/sending-message-through-whatsapp there is a way of sending a message to a specific number in whatsapp, I've tried to adapt it to the `shouldOverrideUrlLoading` method. See the update. The regex should get your phone number from the url but it would be great if you provide a default number in case it's not there – jeprubio Mar 04 '20 at 17:24
  • thanks sir, actually i didn't where i can put the code of whassap i used different but it did't work – Soufiane Nassiri Mar 04 '20 at 22:09
  • 'class MainActivity : AppCompatActivity() { var website = "https://serrajimmobilier.ma/" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main) webView.loadUrl(website) webView.settings.javaScriptEnabled = true; webView.webViewClient = CallBack() }class CallBack : WebViewClient (){ override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest): Boolean {return false;}}}' – Soufiane Nassiri Mar 04 '20 at 22:13
  • here's my activity.xml ' ' – Soufiane Nassiri Mar 04 '20 at 22:15