1

I'm 16 and trying to learn Kotlin, I'm very new. I want to create a search bar which searches both Google and can open any URL you type in. I was trying to use an if else statement, for example:

  • if the first three letters were "www." then use the string url which is equal to "https://",
  • else use the string start_url which is equal to "google.com/search?q=";.

I just do not know how to do that and I have tried looking for help across the internet I just couldn't.

The URLUtil.isValidUrl(url) will not work because it still only loads Google Search

I am open to any comments to try and help me further learn and improve my code, even if it is not related to my question. Point out any errors or things that could be improved in my code, I know it's not perfect, thank you!

package com.example.corie.quicklinks.mainpages

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.asynclayoutinflater.R.id.text
import android.webkit.WebChromeClient
import android.webkit.WebViewClient
import com.example.corie.quicklinks.R
import com.example.corie.quicklinks.R.string.start_url
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        //------------------WEBVIEW-----------------//
        webViewOne.webChromeClient = WebChromeClient()
        webViewOne.isVerticalScrollBarEnabled = false
        webViewOne.run{
            webViewOne.loadUrl("https://" + getString(start_url))
        }


        goBtn.setOnClickListener{
            webViewOne.loadUrl("https://www.google.com/search?q=" + editText.text.toString())

        }
        backBtn.setOnClickListener {
            if (webViewOne.canGoBack())
                webViewOne.goBack()
        }
        nextBtn.setOnClickListener {
            if (webViewOne.canGoForward())
                webViewOne.goForward()
        }
        //------------------WEBVIEW-----------------//


    }
}
  • I was trying to use an if else statement, like if the first three letters were "www." then use the string url which is equal to "https://", else use the string start_url which is equal to "https://www.google.com/search?q=". I just do not know how to do that and I have tried looking for help across the internet I just couldn't –  Aug 17 '18 at 10:46
  • So basically you need to verify whether a string is a valid url? Take a look at this https://stackoverflow.com/questions/4905075/how-to-check-if-url-is-valid-in-android/13931980 – StarterPack Aug 17 '18 at 10:53
  • One thing you could do is to match the content of the search bar against a pattern e.g. [like in this question](https://stackoverflow.com/questions/163360/regular-expression-to-match-urls-in-java). If it matches, load the url, if not perform the query. Unfortunately I can't be much help code-wise, but that should be good starting point – Michael Dodd Aug 17 '18 at 10:53
  • Please see [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – glennsl Aug 17 '18 at 10:53
  • 1
    Possible duplicate of [How to check if URL is valid in Android](https://stackoverflow.com/questions/4905075/how-to-check-if-url-is-valid-in-android) – StarterPack Aug 17 '18 at 10:54
  • thank you everyone for the replies that was quick, and I'm gonna look into each. @glennsl This is the first time I have ever asked a question on here sorry if I did it wrong lmao. But thank you to michael and starter pack –  Aug 17 '18 at 11:02
  • @StarterPack that isn't working, I tried it and the only thing that will load in is google search, not URL's. –  Aug 17 '18 at 11:27
  • Could you edit your question to show what code exactly did not work? – StarterPack Aug 17 '18 at 11:51

1 Answers1

2

Simple with built-in regex pattern:

import android.util.Patterns


val isAddress = Patterns.WEB_URL.matcher(address).matches()

if (isAddress) {
    this@WebpageFragment.mBinding.webpageWebView
        .loadUrl(address)
} else {
    this@WebpageFragment.mBinding.webpageWebView.loadUrl(
        "https://www.google.com/search?q=$address"
    )
}

Explain: First you need to check if the string is an URL. If it's an URL, you'll use the webview to load that URL; Otherwise, you will need to use that value as google search query.

dphans
  • 1,543
  • 19
  • 20