EditText with WebView URL when the page starts loading

0

There is not much to say. I need when the loadameto of the page was started the link of the page loading in WebView go to EditText , that is: editText_url.setText(myWebView.getUrl());

I only know how to do this by clicking the button, but I need it to be ugly automatically when the page starts loading

Current code:

package br.and.browser;

import. ...

public class MainActivity extends Activity {

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

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.hide();

        final ProgressBar Pbar;
        final WebView myWebView = (WebView) findViewById(R.id.webView);
        final EditText editText_url = (EditText) findViewById(R.id.editText_url);

        Pbar = (ProgressBar) findViewById(R.id.progressBar);
        ImageButton Reload = (ImageButton) findViewById(R.id.btn_reload);
        ImageButton Next = (ImageButton) findViewById(R.id.btn_next);
        ImageButton Back = (ImageButton) findViewById(R.id.btn_back);

        WebSettings webSettings = myWebView.getSettings();
        myWebView.setWebViewClient(new WebViewClient());
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.supportZoom();
        myWebView.invokeZoomPicker();
        myWebView.loadUrl("https://www.google.com.br/");
        editText_url.setText(myWebView.getUrl());



        myWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress < 100 && Pbar.getVisibility() == ProgressBar.GONE) {
                    Pbar.setVisibility(ProgressBar.VISIBLE);
                }
                Pbar.setProgress(progress);
                if (progress == 100) {
                    Pbar.setVisibility(ProgressBar.GONE);
                }
            }
        });


        editText_url.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = true;
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    myWebView.loadUrl(editText_url.getText().toString());
                    handled = true;
                    return true;
                }
                return true;

            }
        });

        Reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myWebView.reload();
            }
        });

        Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myWebView.goForward();
            }
        });

        Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myWebView.goBack();
            }
        });


    }

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        WebView myWebView = (WebView) findViewById(R.id.webView);
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
    
asked by anonymous 08.02.2015 / 19:27

1 answer

3

You can set the URL in the onPageStarted of WebViewClient :

myWebView.setWebViewClient(new WebViewClient(){
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        editText_url.setText(url);
    }
});
    
08.02.2015 / 19:57