open links mo webview without having http://or https://

Asked

Viewed 84 times

0

I’m creating a browser for android, but I’m having trouble running the url without the need for the protocol on the link like opening www.google.com and the app automatically put http:// to stay http://www.google.com and the webeview recognize the link.

follows below my mainactivity

public class MainActivity extends AppCompatActivity {
    //definir na programação ---
    private android.widget.Button botaoNav;

    private android.widget.EditText urlNav;

    private android.webkit.WebView navegadorNav;
    //---

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // achar oque cada variavel é, nome do elemento  no FindViewById
        botaoNav = (android.widget.Button) findViewById(R.id.botaoPesquisar);
        urlNav = (android.widget.EditText)  findViewById(R.id.urlPesquisar);
        navegadorNav = (android.webkit.WebView) findViewById(R.id.navegador);

        //definir java script
        navegadorNav.getSettings().setJavaScriptEnabled(true);

        //definir app como navegador
        navegadorNav.setWebViewClient(new android.webkit.WebViewClient());

        //Ação pesquisar

        botaoNav.setOnClickListener(new android.view.View.OnClickListener(){
            @Override
            //carregar url
            public void onClick(android.view.View v){
                navegadorNav.loadUrl(urlNav.getText().toString());
            }
        });
    }
}
  • 1

    As I understand it, the user type the link and the webview goes to him, is that it? If it is, just concatenate the string, boot navegadorNav.loadUrl("https://" + urlNav.getText().toString());

1 answer

1


An extension of Woton Sampaio logic

botaoNav.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Remover possíveis espaços em branco no início e no fim da string
        String url = urlNav.getText().toString().trim();

        if (!url.startsWith("http://"))
            url = "http://" + url;

        navegadorNav.loadUrl(url);
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.