Android開発のお勉強

メニュー

E-Mail



WebView

 Androidアプリ上にWebサイトを表示することができます。
 
 ※ AndroidManifest.xmlに下記の追記を忘れずに、
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 
 

● レイアウト
 WebViewを配置します。


<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" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>




● Activity
 Webサイトでリンクなどイベントが起こった際に、
 ブラウザが立ち上がり、以降はブラウザで処理が行われてしまいます。
 
 Androidアプリ内で処理を続けたい場合は、
 setWebViewClientを設定します。




import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);


        WebView webView = (WebView)findViewById(R.id.webView1);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.google.co.jp/");


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_web_view, menu);
        return true;
    }
}





Copyright (C) Androidアプリ開発のお勉強. All Rights Reserved.