Home > Software design >  Android web browser
Android web browser

Time:04-02

please predecessors, Am creating a web browser homepage in android studio and want to be able to input URL from the same editText as that of my search. have been able to take search and load google results, but URL still end up in searched instade of loading the URL website.

this is my codes.

    EditText searchEditText = rootView.findViewById (R.id.searchEditText);
    ImageButton searchBt = rootView.findViewById (R.id.searchBt);

    searchBt.setOnClickListener (new View.OnClickListener ( ) {
        @Override
        public void onClick(View v) {
            String searchQuery = searchBar.getText ().toString ();
            if(URLUtil.isValidUrl (searchQuery)){
                webView.loadUrl (searchQuery);
            }else {
                String searchURL = "https://www.google.com/search?q=" searchQuery;
                webView.loadUrl (searchURL);
            }

        }
    });

CodePudding user response:

The problem is probably because your URL doesn't have https at the beginning. Try using some other mechanism to determine if the URL is valid.

For example, use WEB_URL pattern in Patterns Class

Patterns.WEB_URL.matcher(potentialUrl).matches()
  • Related