WebViewDemo

In this demo, we get to view a web address in a totally different way by sidestepping the default browser and creating our own custom webviewer.

The key to creating the custom webviewer is the MyWebViewClient class that extends the WebViewClient class. With its key method shouldOverrideUrlLoading(view,url) that gives control to application when a new url is loaded in the provided view.


WebViewDemo.java
Code:

package org.example.webviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class WebViewDemo extends Activity {
private WebView webView;
private EditText urlField;
private Button goButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Create reference to UI elements
webView = (WebView) findViewById(R.id.webview_compontent);
urlField = (EditText)findViewById(R.id.url);
goButton = (Button)findViewById(R.id.go_button);

// workaround so that the default browser doesn't take over
webView.setWebViewClient(new MyWebViewClient());

// Setup click listener
goButton.setOnClickListener( new OnClickListener() {
public void onClick(View view) {
openURL();
}
});

// Setup key listener
urlField.setOnKeyListener( new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_ENTER) {
openURL();
return true;
} else {
return false;
}
}
});

}

/** Opens the URL in a browser */
private void openURL() {
webView.loadUrl(urlField.getText().toString());
//webView.requestFocus();
}

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



main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/url"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:lines="1"
android:layout_weight="1.0" android:hint="https://"/>

<Button
android:id="@+id/go_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/go_button"
/>

</LinearLayout>

<WebView
android:id="@+id/webview_compontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
/>
</LinearLayout>

Do not forget
1. To give the AndroidManifestfile.xml the necessary internet permission:
<uses-permission android:name="android.permission.INTERNET" />

2. And for this setup in the strings.xml file to add <string name="go_button">Go</string> in between <resources> tag


Output

Source
https://www.protechtraining.com/static/tutorials/WebViewDemo.zip

Copyright © 2018 ProTech. All Rights Reserved.

Sign In Create Account

Navigation

Social Media