【androidアプリ開発 No.17】 スライドパズルを作ろう04

Pocket

今回はいよいよ作成したスライドパズルに広告を設置したいと思います。

広告はAdLantisを利用します。AdLantisについてはこちらを参照ください。

AndroidManifest.xml

AdLantisの登録が終わったらサイトより生成されたAndroid SDK用広告枠コードの「meta-data」を追加します。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="jp.ne.hatena.d.failure_engineer.SlidePuzzle"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:icon="@drawable/icon_puzzle" 
        android:label="@string/app_name" 
        android:debuggable="true">
        <activity android:name=".FirstActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SlidePuzzleActivity"
                  android:label="@string/app_name">
        </activity>
        <meta-data android:name="Adlantis_Publisher_ID" 
                   android:value="あなたのAdlantis_Publisher_ID" />
    </application>
</manifest>

※広告を表示するにはuses-permissionを設定する必要があります。

res/layout

AndroidManifestの設定が完了したので、画面に広告を設置していきます。

first.xml
<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
      <LinearLayout 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:orientation="vertical" 
        android:id="@+id/linearLayout1" 
        android:layout_alignParentBottom="true" 
        android:layout_centerHorizontal="true" 
        android:layout_marginBottom="165dp">
          <Button 
            android:id="@+id/button1" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:text="ゲーム開始"></Button>
      </LinearLayout>
      <TextView 
        android:layout_width="wrap_content" 
        android:id="@+id/textView1" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:text="3×3スライドパズル" 
        android:layout_above="@+id/linearLayout1" 
        android:layout_centerHorizontal="true" 
        android:layout_marginBottom="80dp"></TextView>
      <!-- ▼ 広告 ▼ -->
      <jp.Adlantis.Android.AdlantisView 
        android:gravity="center" 
        android:id="@+id/adView" 
        android:layout_width="fill_parent" 
        android:layout_height="50dp" 
        android:layout_alignParentBottom="true" 
        android:layout_alignParentLeft="true" />
      <!-- ▲ 広告 ▲ -->
  </RelativeLayout>

 

main.xml
<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
      <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
          <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left">
              <!-- タイムラベル -->
              <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Time : "></TextView>
              <!-- 経過した時間の表示 -->
              <Chronometer
                android:id="@+id/chronometer1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"></Chronometer>
          </LinearLayout>
          <!-- 3×3のスライドパズル -->
          <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
              <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                  <ImageButton android:id="@+id/imageButton1"
                    android:layout_width="120px"
                    android:layout_height="120px"></ImageButton>
                  <ImageButton android:id="@+id/imageButton2"
                    android:layout_width="120px"
                    android:layout_height="120px"></ImageButton>
                  <ImageButton android:id="@+id/imageButton3"
                    android:layout_width="120px"
                    android:layout_height="120px"></ImageButton>
              </TableRow>
              <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                  <ImageButton
                    android:id="@+id/imageButton4"
                    android:layout_width="120px"
                    android:layout_height="120px"></ImageButton>
                  <ImageButton
                    android:id="@+id/imageButton5"
                    android:layout_width="120px"
                    android:layout_height="120px"></ImageButton>
                  <ImageButton
                    android:id="@+id/imageButton6"
                    android:layout_width="120px"
                    android:layout_height="120px"></ImageButton>
              </TableRow>
              <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                  <ImageButton
                    android:id="@+id/imageButton7"
                    android:layout_width="120px"
                    android:layout_height="120px"></ImageButton>
                  <ImageButton
                    android:id="@+id/imageButton8"
                    android:layout_width="120px"
                    android:layout_height="120px"></ImageButton>
                  <ImageButton
                    android:id="@+id/imageButton9"
                    android:layout_width="120px"
                    android:layout_height="120px"></ImageButton>
              </TableRow>
          </TableLayout>
          <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
              <!-- スタートボタン -->
              <Button
                android:text="スタート"
                android:id="@+id/button1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"></Button>
          </LinearLayout>
          <!-- ▼ 広告 ▼ -->
          <jp.Adlantis.Android.AdlantisView
              android:id="@+id/adView"
              android:layout_width="match_parent" 
              android:layout_height="50dp"
              android:layout_weight="1"
              android:layout_centerVertical="true"
              android:layout_centerHorizontal="true" 
              android:gravity="center" />
          <!-- ▲ 広告 ▲ -->
      </LinearLayout>
  </RelativeLayout>

以上で完成です。意外と簡単に設置することが出来ました。
次回はいよいよマーケットに登録します。



Post Footer automatically generated by Add Post Footer Plugin for wordpress.

びのっち

関東圏で活動しているとてもマイペースなSEです。