H ow to load a URL to webview in android? public class MainActivity extends AppCompatActivity{ private Button button ; //button ...

Android WebView

How to load a URL to webview in android?

public class MainActivity extends AppCompatActivity{
    private Button button; //button
    private EditText editText; //Edittext
    private WebView webView; //webview

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

        button = (Button)findViewById(R.id.button);
        editText = (EditText)findViewById(R.id.editText);
        webView = (WebView)findViewById(R.id.url);
        webView.setWebViewClient(new MyBrowser());

        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){

                String string = editText.getText().toString().trim();
                webView.getSettings().setLoadsImagesAutomatically(true);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                webView.loadUrl(string);
            }
        });
    }

    private class MyBrowser extends WebViewClient {
        @Override        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}



Go to AndroidManifest.xml File and add this statement 

<uses-permission android:name="android.permission.INTERNET" />






0 comments :