« Eclipse スペルチェック | メイン | Android MarketのCopy Protection動作 »

Android ViewStubを用いたapiキーの切り替え

先日の記事、「Android ApiキーをRelease版とDebug版で切り替える方法」にてコード内でリリースキーとデバックキーの切り替え方法を記載したが、ViewStubを用いることで、MapViewの生成をコード内から追い出す事が可能になる。

簡単な画面であればコード内でMapViewを生成してしまっても問題はないが、個人的にはlayoutファイル内に入れるのが好きなのでViewStubによるapiキーの切り離しを行った。

ViewStubの使用方法

Android SDKのViewStubの説明ページに総て書いてあるので特に説明をする事はないのでソースコードのみ記載する。


        ViewStub stub = (ViewStub) findViewById(R.id.mapview_stub);
        if(isDebugAble(this)){
        	Log.v("debug");
           stub.setLayoutResource(R.layout.mapview_debug);
        }else{
        	Log.v("release");
           stub.setLayoutResource(R.layout.mapview_release);
        }
        View inflated = stub.inflate();
        mMapView = (MapView)inflated;

layoutファイル


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/mainlayout"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
   >
 <ViewStub
 	android:id="@+id/mapview_stub"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
 />
    
</RelativeLayout>

上記のViewStubと入れ替わる2つのレイアウトファイルを用意する。

デバック用

<com.google.android.maps.MapView
		android:id="@+id/mapview"
		android:apiKey="hogehoge Debug api key"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:enabled="true"
		android:clickable="true"
/>
リリース用

<com.google.android.maps.MapView
		android:id="@+id/mapview"
		android:apiKey="@string/mapkey"
		android:apiKey="hogehoge Release apiKey"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:enabled="true"
		android:clickable="true"
/>

ちなみに、このViewStubは、他にもいろいろと使い道がある。

ブログ内の関連する記事

トラックバック

このエントリーのトラックバックURL:
http://www.taosoftware.co.jp/mt/mt-tb.cgi/155