- Enable my own browser to handle our own URL loading
If you run implemented and ran the AndroidBrowser in the previous article, you can note that the control will be passed to Android default Browser when any links is clicked. In order to make the new links is opened by our own browser, setWebViewClient() have to be called after the browser initiated.
myBrowser.setWebViewClient(new WebViewClient());
* Refer to Android's Hello, WebView example, there is another implementation with little bit difference.
- Remove TextView in main.xml to make more room for the browser.
- Override onCreateOptionsMenu() to add menu for various navigating functions
- Override onOptionsItemSelected(), and the corespond methods to handle the actual operation when the navigating functions selected.
- Implement a AndroidBrowserGoto.java class to handle Goto URL entry, it communicate with the main class, AndroidBrowser, by means of Intent and Bundle.
When GOTO menu item selected, control will be passed to AndroidBrowserGoto.java; after user input the URL and click OK, control and the target URL will be passed back to AndroidBrowser.java. So we start AndroidBrowserGoto.java using startActivityForResult(), instead of startActivity(). And also, we have to Override onActivityResult() to handle the returned data.
main.xml, layout used by AndroidBrowser.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="@+id/mybrowser"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
gotopanel.xml, layout used by AndroidBrowserGoto.java.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mybrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the URL:"
/>
<EditText
android:id="@+id/gotourl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://"
/>
<Button
android:id="@+id/gotoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
/>
<Button
android:id="@+id/cancelgotoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CANCEL"
/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AndroidBrowser!</string>
<string name="app_name">Android Browser</string>
<string name="str_about_message">Android Browser</string>
<string name="str_exit_message">Exit?</string>
<string name="str_ok">OK</string>
<string name="str_no">No</string>
<string name="str_URL">URL:</string>
<string name="str_Goto">Go to</string>
<string name="str_About">About</string>
<string name="str_Exit">Exit</string>
<string name="str_Backward"><<</string>
<string name="str_Reload">Reload</string>
<string name="str_Forward">>></string>
</resources>
AndroidBrowser.java
package com.exercise.AndroidBrowser;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class AndroidBrowser extends Activity {
final int MENU_GOTO = 0;
final int MENU_ABOUT = 1;
final int MENU_EXIT = 2;
final int MENU_BACKFORD = 3;
final int MENU_RELOAD = 4;
final int MENU_FORWARD = 5;
WebView myBrowser;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "http://computer-help-tips.blogspot.com";
myBrowser=(WebView)findViewById(R.id.mybrowser);
/*By default Javascript is turned off,
* it can be enabled by this line.
*/
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.setWebViewClient(new WebViewClient());
myBrowser.loadUrl(myURL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, MENU_GOTO, 0, R.string.str_Goto);
menu.add(0, MENU_ABOUT, 0, R.string.str_About);
menu.add(0, MENU_EXIT, 0, R.string.str_Exit);
menu.add(0, MENU_BACKFORD, 0, R.string.str_Backward);
menu.add(0, MENU_RELOAD, 0, R.string.str_Reload);
menu.add(0, MENU_FORWARD, 0, R.string.str_Forward);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case MENU_GOTO:
openGotoDialog();
break;
case MENU_ABOUT:
openAboutDialog();
break;
case MENU_EXIT:
openExitDialog();
break;
case MENU_BACKFORD:
if(myBrowser.canGoBack())
myBrowser.goBack();
break;
case MENU_RELOAD:
myBrowser.reload();
break;
case MENU_FORWARD:
if(myBrowser.canGoForward())
myBrowser.goForward();
break;
}
return true;
}
private void openGotoDialog()
{
Intent intent = new Intent();
intent.setClass(AndroidBrowser.this, AndroidBrowserGoto.class);
startActivityForResult(intent, 0);
}
private void openAboutDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.str_About)
.setMessage(R.string.str_about_message)
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface,
int i)
{}
})
.show();
}
private void openExitDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.str_Exit)
.setMessage(R.string.str_exit_message)
.setNegativeButton(R.string.str_no,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{}
})
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface,
int i)
{ finish();
}
})
.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==0)
{
switch (resultCode)
{ case RESULT_OK:
String gotourl = data.getStringExtra("url");
myBrowser.loadUrl(gotourl);
break;
case RESULT_CANCELED:
break;
}
}
}
}
AndroidBrowserGoto.java
package com.exercise.AndroidBrowser;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AndroidBrowserGoto extends Activity {
Button gotoButton, cancelButton;
EditText gotoURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gotopanel);
gotoURL = (EditText) findViewById(R.id.gotourl);
gotoButton = (Button) findViewById(R.id.gotoButton);
gotoButton.setOnClickListener(gotoOnClickListener);
cancelButton = (Button) findViewById(R.id.cancelgotoButton);
cancelButton.setOnClickListener(cancelgotoOnClickListener);
}
private Button.OnClickListener gotoOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
Bundle bundle = new Bundle();
//bundle.putString("url", "http://www.google.com/");
String targetURL = gotoURL.getText().toString();
bundle.putString("url", targetURL);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
};
private Button.OnClickListener cancelgotoOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setResult(RESULT_CANCELED);
finish();
}
};
}
Finally, add
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidBrowser"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidBrowser"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AndroidBrowserGoto"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
The whole project can be downloaded here.
Previous articale >> AndroidBrowser, implement a Android Browser using WebView
Next article >> AndroidBrowser, with visible/gone URL bar