m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

androidでwebviewを使用してページを表示

RSSリーダーを作成する場合は

内部のwebviewを使用するがアプリケーションでのwebブラウザを使用するかのどちらかになりますが今回はwebviewを使用する

画面

アクティビティにwebviewを配置
f:id:m_shige1979:20150105225723p:plain

実装

package jp.mshige1979.app.sampleappweb1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;


public class MainActivity extends ActionBarActivity {

    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 取得
        webview = (WebView)findViewById(R.id.webView);
        
        // URLを指定してロードを行う
        webview.loadUrl("http://m-shige1979.hatenablog.com/");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

permission設定

忘れやすいので一応

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jp.mshige1979.app.sampleappweb1" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

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

</manifest>

結果

f:id:m_shige1979:20150105230504p:plain

所感

シミュレータが速いのか?すぐに表示してくれます。内部ブラウザの場合はこれで対応可能なよう
そろそろRSSリーダーでも作成してもいいかもしれない