Tuesday, September 22, 2009

AndroidBrowser, with visible/gone URL bar

Personally, I don't like the UI in the previous exercise, which start another activity to enter URL, and passed back to open the URL. Here, I implement a URL bar, using TableLayout with visibility. It can be set between visible and gone(hide) by click on the "URL bar" option on the Menu. Such that user can enter URL on the same screen of the browser, and hide it if more room for display is wanted.



main.xml
<?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"
>
<TableLayout
android:id="@+id/UrlEntry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0"
android:visibility="gone"
>
<TableRow>
<EditText
android:id="@+id/goUrl"
android:text="http://"
/>
<Button
android:id="@+id/goUrlButton"
android:text="GO"
/>
</TableRow>
</TableLayout>
<WebView android:id="@+id/mybrowser"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

main.xml can be downloaded here.

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_URLbar">URL bar</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>

strings.xml can be downloaded here.

package com.exercise.AndroidBrowser;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;

public class AndroidBrowser extends Activity {

final int MENU_VISIBILITY = 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;
TableLayout myUrlBar;
EditText gotoUrl;
Button myUrlButton;

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

String myURL = "http://www.google.com";
myBrowser=(WebView)findViewById(R.id.mybrowser);

myUrlBar = (TableLayout)findViewById(R.id.UrlEntry);
gotoUrl = (EditText)findViewById(R.id.goUrl);
myUrlButton = (Button)findViewById(R.id.goUrlButton);
myUrlButton.setOnClickListener(myUrlButtonOnClickListener);

/*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_VISIBILITY, 0, R.string.str_URLbar);
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_VISIBILITY:
ToggleGotoVisibility();
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;
}

void ToggleGotoVisibility()
{
if (myUrlBar.getVisibility() == View.GONE)
{
myUrlBar.setVisibility(View.VISIBLE);
}
else
{
myUrlBar.setVisibility(View.GONE);
}
}

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();
}

private Button.OnClickListener myUrlButtonOnClickListener
= new Button.OnClickListener()
{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myBrowser.loadUrl(gotoUrl.getText().toString());
}

};

}

AndroidBrowser.java can be downloaded here.

Remember to delete the files gotopanel.xml and AndroidBrowserGoto.java. And also remove "activity android:name=".AndroidBrowserGoto" from AndroidManifest.xml to resume original.

The whole project can be downloaded here.

Previous articale >> AndroidBrowser, with simple navigating functions

No comments:

Post a Comment