[Android]Cara Menampilkan Halaman Web Sederhana dengan WebView Tanpa Menampilkan Address Bar

Gimana cara menampilkan halan web tanpa memperlihatkan alamat web??

Pertama bikin Project Androidnya dulu gan, Kasih Nama Projectnya “web”, setelah project dibuat edit activity_main.xml, biar seperti ini:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="144dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="adi-hidayat.com" />

</RelativeLayout>

Setelah itu edit MainActivity.java nya, seperti ini

package com.example.web;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	Button go;

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

		go = (Button) findViewById(R.id.button1);

		go.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(MainActivity.this, Web.class);
				startActivity(i);
			}
		});
	}

}

kalo udah, bikin Activity baru kasih nama aja Web. lalu edit activity_web.xml seperti ini

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

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <WebView
        android:id="@+id/webview01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </WebView>

</LinearLayout>

abis itu edit juga web.java nya seperti ini,

package com.example.web;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class Web extends Activity {

	WebView web;
	ProgressBar progressBar;

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

		web = (WebView) findViewById(R.id.webview01);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);

		web.setWebViewClient(new myWebClient());
		web.getSettings().setJavaScriptEnabled(true);
		web.loadUrl("http://www.adi-hidayat.com");

	}

	public class myWebClient extends WebViewClient {
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub

			view.loadUrl(url);
			return true;

		}

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			super.onPageFinished(view, url);

			progressBar.setVisibility(View.GONE);
		}

		@Override
		public void onReceivedError(WebView view, int errorCod,
				String description, String failingUrl) {
			Toast.makeText(Web.this, "Ente Ga Punya Koneksi Gan..!! ",
					Toast.LENGTH_LONG).show();
			finish();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
			web.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}

buat folder raw,di dalam foler res

custom raw

buatlah file html yang  isinya  seperti berikut

<html>
<body>
<h1>
ga bisa load gan!!
</h1>
</body>
</html>

file ini bertujuan mengcustom jika gagal meload halaman web yang biasanya tampillanya seperti ini

device-2014-04-06-030131

kalo udah tambahkan permission internet di android manifesnya

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

Running projectnya, kalo bener, pasti hasilnya gini

buttonlink

kalo ada koneksi internet/berhasil, maka hasilnya

Untitled

nah kalo gagal maka hasilnya

device-2014-04-06-030744

 

download projeknya di sini

 

One thought on “[Android]Cara Menampilkan Halaman Web Sederhana dengan WebView Tanpa Menampilkan Address Bar

  1. Pingback: Stephen

Leave a Reply

Your email address will not be published. Required fields are marked *


3 + 6 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>