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

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, such as open www.google.com and the app automatically puts http: // to stay link and webeview recognize the link.

follow 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());
            }
        });
    }
}
    
asked by anonymous 30.05.2018 / 05:25

1 answer

1

An extension of the 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);
    }
});
    
30.05.2018 / 15:57