7 Ekim 2015 Çarşamba

#ANDROID - Web Browser (WebView Kullanımı)

  Hiç yorum yok
Ekranımıza WebView nesnesi ekleyerek kendimizce basit bir web browser yapacağız. Tasarım kısmında görüldüğü gibi sadece layoutlarla edittext ve buttonlarımızı konumlandırıyoruz. Burada üstünde durulması gereken bir durum yok.


Hemen browser.xml ve Browser.java dosyalarımızı oluşturuyoruz.

browser.xml dosyası:

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >

<EditText
android:id="@+id/etAdres"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="20"
android:hint="adres?" >
</EditText>

<Button
android:id="@+id/bGit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="80"
android:text="Git" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >

<Button
android:id="@+id/bGeri"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="25"
android:text="Geri" />

<Button
android:id="@+id/bIleri"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="25"
android:text="İleri" />

<Button
android:id="@+id/bYenile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="25"
android:text="Yenile" />

<Button
android:id="@+id/bGecmis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="25"
android:text="Sil" />
</LinearLayout>

<WebView
android:id="@+id/wvBrowser"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

Browser.java dosyası:

package com.hakancengiz.blogders;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class Browser extends Activity implements View.OnClickListener {

Button git, geri, ileri, gecmisiSil, yenile;
WebView browser;
EditText webAdresi;

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

browser = (WebView) findViewById(R.id.wvBrowser);
git = (Button) findViewById(R.id.bGit);
geri = (Button) findViewById(R.id.bGeri);
ileri = (Button) findViewById(R.id.bIleri);
yenile = (Button) findViewById(R.id.bYenile);
gecmisiSil = (Button) findViewById(R.id.bGecmis);
webAdresi = (EditText) findViewById(R.id.etAdres);

geri.setOnClickListener(this);
ileri.setOnClickListener(this);
gecmisiSil.setOnClickListener(this);
yenile.setOnClickListener(this);
git.setOnClickListener(this);

//javascripti açıyoruz
browser.getSettings().setJavaScriptEnabled(true);

//tüm siteyi zoom yapmadan webview içinde görmemizi sağlamak için
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);

// bağlantılar kendi webview'ımızda görüntülensin
// telefonun browserı açılmasın diye..
browser.setWebViewClient(new benimClient());

browser.loadUrl("http://www.google.com.tr");

}

@Override
public void onClick(View v) {

switch (v.getId()) {

case R.id.bGit:

String gidilecekAdres = webAdresi.getText().toString();
browser.loadUrl(gidilecekAdres);

// buton tıklandıktan sonra klavyeyi gizler
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(webAdresi.getWindowToken(), 0);

break;

case R.id.bGeri:

if (browser.canGoBack())
browser.goBack();

break;

case R.id.bIleri:
if (browser.canGoForward())
browser.goForward();
break;

case R.id.bYenile:
browser.reload();
break;

case R.id.bGecmis:
browser.clearHistory();
break;

}

}
}

Buradaki en önemli şeylerden biri benimClient.java dosyası. Browserdan bir bağlantıya tıkladığımızda otomatik olarak telefonun kendi browserı açılıyor. Bunun önüne geçmek için benimClient.java dosyasını oluşturuyoruz.

benimClient.java dosyası:

package com.hakancengiz.blogders;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class benimClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

Bu işlemi de yaptıktan sonra AndroidManifest.xml dosyamızda activitymizi tanıtıyoruz.

        <activity
android:name=".Browser"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.BROWSER" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Artık browser kullanıma hazır.

Hiç yorum yok :

Yorum Gönder