前回の1.1回目では


よー様のAndroid的備忘録

のようなレイアウトの画面を作成し、BMI(肥満度)を計算するような

ツールを作ろうビックリマークというところまででした。



レイアウトの完成品はこちらです。


よー様のAndroid的備忘録

今回は、この画面レイアウトの作成について解説してきます。



この画面レイアウトを作成するにあたって、手を入れる必要のある

ソースは、『color.xml』と『main.xml』の2つになります。


『color.xml』は色の定義を行い、『main.xml』はこの画面のレイアウトを

定義します。



では、『color.xml』から解説していきましょう。
よー様のAndroid的備忘録


res\valuesを右クリックし、『新規(W)』⇒『その他(O)』から


よー様のAndroid的備忘録

『Android』の下の『Android XML File』を選択し、『次へ(N)』


よー様のAndroid的備忘録

『ファイル』に『color.xml』と入力し、『終了(F)』で『color.xml』が作成されます。



今回は、作成された『color.xml』を下記のように編集してみました。


color.xml---------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gray">#808080</color>
<color name="lightgray">#D3D3D3</color>
</resources>

-----------------------------------------------------------------


grayという変数に#808080を、

lightgrayという変数に#D3D3D3を、定義しました。それだけです。


これらの変数は、『main.xml』で使用されます。



『main.xml』は下記のように編集しました。


main.xml----------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android ">
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20.0dp"
android:text="身長"
android:gravity="left"
></TextView>
<EditText
android:id="@+id/EditText01"
android:text=""
android:layout_width="60.0dp"
android:layout_height="40.0dp"
android:textSize="20.0dp"
android:textStyle="normal"
android:typeface="normal"
android:textColorHint="@color/lightgray"
android:hint="160"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="5dp"
></EditText>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15.0dp"
android:text="."
android:gravity="left"
android:layout_marginLeft="1dp"
></TextView>
<EditText
android:id="@+id/EditText02"
android:text="0"
android:layout_width="30.0dp"
android:layout_height="35.0dp"
android:textSize="15.0dp"
android:textStyle="normal"
android:typeface="normal"
android:hint="0"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="1dp"
></EditText>
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20.0dp"
android:text="体重"
android:gravity="left"
android:layout_marginLeft="10dp"
></TextView>
<EditText
android:id="@+id/EditText03"
android:text=""
android:layout_width="60.0dp"
android:layout_height="40.0dp"
android:textSize="20.0dp"
android:textStyle="normal"
android:typeface="normal"
android:textColorHint="@color/lightgray"
android:hint="50"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="5dp"
></EditText>
<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15.0dp"
android:text="."
android:gravity="left"
android:layout_marginLeft="1dp"
></TextView>
<EditText
android:id="@+id/EditText04"
android:text="0"
android:layout_width="30.0dp"
android:layout_height="35.0dp"
android:textSize="15.0dp"
android:textStyle="normal"
android:typeface="normal"
android:hint="0"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="1dp"
></EditText>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<Button
android:text="BMI(肥満度)計算"
android:id="@+id/Button01"
android:layout_width="200.0dp"
android:layout_height="60.0dp"
android:textSize="20dp"
android:textStyle="bold"
android:typeface="normal"
android:layout_marginTop="20dp">
</Button>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:typeface="monospace"
android:textSize="20.0dp"
android:text="■あなたのBMI(肥満度)"
android:gravity="left"
android:layout_marginTop="30dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout04"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16.0dp"
android:textColor="@color/gray"
android:text=" ⇒ここに計算結果が表示されます。"
android:gravity="left"
android:layout_marginTop="10dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout05"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView51"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:typeface="monospace"
android:textSize="16.0dp"
android:text="■BMI(肥満度)から見たあなたの標準体重"
android:gravity="left"
android:layout_marginTop="25dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout06"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView61"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray"
android:textSize="16.0dp"
android:text=" ⇒ここに計算結果が表示されます。"
android:gravity="left"
android:layout_marginTop="10dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout07"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView71"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16.0dp"
android:textStyle="bold"
android:typeface="monospace"
android:text="■現在の体重-標準体重"
android:gravity="left"
android:layout_marginTop="15dp"
></TextView>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout08"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/TextView81"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/gray"
android:textSize="16.0dp"
android:text=" ⇒ここに計算結果が表示されます。"
android:gravity="left"
android:layout_marginTop="10dp"
></TextView>
</TableRow>
</TableLayout>
</LinearLayout>

-----------------------------------------------------------------


なにやら長々と書いてあって何のことかさっぱりな感じですが、

内容はたいしたことありません。


上記を図であらわしたものが、Eclipseの『アウトライン』に表示

されています。


よー様のAndroid的備忘録


緑色のT,E,Bの記号のついているものが、画面に表示される

ものです。


T(TextView)が文字の表示、

E(EditView)が文字の入力

B(Button)が何か動作をさせるボタンです。


画面に表示されているものと見比べるとわかりやすいでしょう。
よー様のAndroid的備忘録



アウトラインや画面に表示されているTextViewやEditViewやButton、

それらを囲っているLinearLayoutやTableLayoutにプロパティーを設定

することで、入力制限や文字のフォントの変更などができます。



たとえば、今回使用したLinearLayoutは、このように定義していますが、


<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android ">

『android:orientation="vertical"』は、LinearLayout内のViewなどの

コンポーネントを垂直に(縦に)並べるような指定になります。


初期値(デフォルト)は水平(横方向)への配置になります。

今回の例で言えば、TableLayout毎には縦方向、TableLayout内では

横方向の指定になっています。



次にTableLayoutを見てみましょう。

<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content
">

android:id="@+id/TableLayout01"は、このコンポーネントのID

を指定します。このTableLayoutのIDは、『TableLayout01』に

なります。


android:layout_width="wrap_content"はこのコンポーネントの

横幅のサイズについてGUI要素を表示するための最小のサイズで

画面に表示します。

android:layout_height="wrap_content">は縦幅のサイズについて

になります。

中の要素に応じてサイズを変更してくれるので、便利かもしれません。




次にTextViewを見てみましょう。

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20.0dp"
android:text="身長"
android:gravity="left"
></TextView>

android:textSize="20.0dp"
テキストのフォントサイズを指定します。dpは単位です。


android:gravity="left"はコンポーネントを左寄せにします。



あと2つです、EditTextを見てみましょう。

これは、テキストの入力に使います。

<EditText
android:id="@+id/EditText01"
android:text=""
android:layout_width="60.0dp"
android:layout_height="40.0dp"
android:textSize="20.0dp"
android:textStyle="normal"
android:typeface="normal"
android:textColorHint="@color/lightgray"
android:hint="160"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
android:gravity="left"
android:layout_marginLeft="5dp"
></EditText>

android:layout_width="60.0dp"はこのコンポーネントの横幅の

サイズを明示的に指定します。

android:layout_height="40.0dp"は縦幅です。


android:textSize="20.0dp"は中のテキストのサイズを指定します。


android:textStyle="normal"はテキストのスタイルを、
android:typeface="normal"はテキストの書体を
指定します。


何も入力されていない場合に、どのように入力するかヒントを

表示することができます。

android:textColorHint="@color/lightgray"はヒントの色を

color.xmlから指定し、

android:hint="160"はヒントに表示する文字を指定します。

今回でいえば、明るい灰色で160と表示します。


android:digits="0123456789"は、0123456789"の入力以外は

受け付けず、
android:inputType="number"は数字しか、入力の対象にならない

ようにします。
android:maxLength="3"は、最大桁数の指定です。身長や体重は

3桁まででよさそうです。
android:maxLines="1"は最大行数を指定します。


android:layout_marginLeft="5dp"は、右のコンポーネントとの間隔です。



最後にButtonです。

<Button
android:text="BMI(肥満度)計算"
android:id="@+id/Button01"
android:layout_width="200.0dp"
android:layout_height="60.0dp"
android:textSize="20dp"
android:textStyle="bold"
android:typeface="normal"
android:layout_marginTop="20dp">
</Button>


android:layout_marginTopは、上のコンポーネントとの間隔です。
他はいままででてきたプロパティーと同じです。




これで、main.xmlの内容は、ほぼ理解できるようになりました。
詳しくは下記のアドレスから調べられては如何でしょうか。

http://wikiwiki.jp/android/



今更ですが、android:textに書いている文字列なんかは、

Strings.xmlに定義すればよかったですね。

次回までに修正しておきます。



次回は、この画面で計算できるようにしていきましょう。


ではでは。


オリジナルアプリを開発していきましょう。


第1回目ということで、簡単そうなものをまずは作っていきます。

Androidでは、ボタンやプルダウン、テキストボックスのようなものを

使用することができます。

よー様のAndroid的備忘録



これらを利用してこれから、BMI(肥満度)の計算ツールを作成します。

ますは作るもののイメージをします。

なにかExcelなんかに簡単に書いてみましょう。
よー様のAndroid的備忘録
アップ書いてみました。書いてる数値は適当です。アップ


ひとまずはこんなものでいいと思います。

作りながら、色々詰めていきましょう。



Eclipseを立ち上げて、プロジェクトを作成してみましょう。

今回のアプリであれば、Androidのプロジェクトを指定すれば、

特に変わった設定はとくにありません。

サクっと作っちゃいましょう。
よー様のAndroid的備忘録
アップBMICheckというプロジェクト名にしてみました。アップ

アップここは何でもいいです。アップ



main.xmlをごそごそと弄っていきましょう。

まずは身長の整数部分が入るテキストボックスです。


このテキストボックスに、必要な機能を考えましょう。

ひとまず思いつきそうなのは、


・数値のみ入力可能

・最大3桁まで入力可能

・3桁入力されることが想定されているサイズである

・デフォルト値=170


となりましたが、こんなもので如何でしょうか。

ひとまずこれで進めていきましょう。



BMICheck\res\layout\main.xmlをEclipseから編集していきます。

こんな感じで編集してみました。


main.xml---------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="
http://schemas.android.com/apk/res/android ">
<EditText
android:id="@+id/EditText01"
android:text="170"
android:layout_width="60.0dp"
android:layout_height="40.0dp"
android:textSize="20.0dp"
android:textStyle="normal"
android:typeface="normal"
android:hint="170"
android:digits="0123456789"
android:inputType="number"
android:maxLength="3"
android:maxLines="1"
></EditText>
</LinearLayout>
-------------------------------------------------------------


各属性の意味は下記です。

--------------------------------------------------------------

android:id="@+id/EditText01"
 ⇒Javaソースに紐付けるIDを指定
android:text="170"
 ⇒初期値を設定
android:layout_width="60.0dp"
 ⇒幅のサイズを指定
android:layout_height="40.0dp"
 ⇒高さを指定
android:textSize="20.0dp"
 ⇒文字のサイズを指定
android:textStyle="normal"
 ⇒テキストのスタイルを指定
android:typeface="normal"
 ⇒テキストの書体を指定
android:hint="170"
 ⇒テキストが空の場合に表示されるヒントを指定
android:digits="0123456789"
 ⇒テキストに入力可能な文字を指定(上記は数値のみ入力可)
android:inputType="number"
 ⇒入力するデータの種別に応じて指定
  "number"は数値入力する場合に指定
android:maxLength
 ⇒最大文字数を指定
android:maxLines
 ⇒最大行数を指定

--------------------------------------------------------------


これだけ指定してれば、安全に使えるのではないでしょうか。



この状態で、一回動かして見ましょう。


『実行(R)』⇒『実行ダイアログを開く(N)』⇒

プロジェクトを指定して『実行(R)』 です。
よー様のAndroid的備忘録


指定したテキストボックスを表示することができましたビックリマーク



今日のところはいったんここまでで。

ではでは。

アプリ開発の記事がだんだん増えてきたので、もちょっとしたら

これらも整理しなきゃですねあせる


それはそれとして、

今回は画像を使ったアプリを作って動かしていきましょう。



いつものように『ファイル(F)』⇒『新規(N)』⇒『Androidプロジェクト』から

プロジェクトを作成しましょう。
よー様のAndroid的備忘録
特別な設定は特にありません。



次に、画像ファイルを配置します。

この作業はEclipseからではなく、Windowsのファイルシステムから画像を配置します。

Eclipse起動時に指定しているワークスペース\\res\drawable配下に

配置します。
よー様のAndroid的備忘録



画像はこれ
よー様のAndroid的備忘録
star.png



配置した後に、『ImageViewDemo』を選択した状態で、『プロジェクト(P)』⇒『プロジェクトのビルド(B)』

をクリックします。

※『プロジェクト(P)』⇒『自動的にビルド(M)』にチェックが入っている場合、この操作は不要ですが、

 その代わりに、『ImageViewDemo』を右クリックして、『更新(F5)』をクリックしておきましょう。


ImageViewDemo\gen\android.sample\R.javaが自動更新されます。

※このファイルは手動で更新しないよう注意しましょう。



R.java--------------------------------------------------------------------

/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/

package android.sample;

public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
public static final int star=0x7f020001;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
-------------------------------------------------------------------------

starの記述が追加されている事がわかります。




最後に、ImageViewDemo\src\android.sample\MainActivity.javaを変更しましょう。


MainActivity.java-----------------------------------------------------------

package android.sample;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends Activity {
private SampleView mView;
private Bitmap mBitmap;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.star); mView = new SampleView(this);
setContentView(mView);
}

private class SampleView extends View {
private Paint mPaint;
private float imageX = 0f;
private float imageY = 0f;

public SampleView(Context context) {
super(context);
mPaint = new Paint();
}


@Override
protected void onDraw(Canvas canvas) {
//canvasのカラー
canvas.drawColor(Color.WHITE);
//アンチエイリアス
mPaint.setAntiAlias(true);
//カラー設定(デフォルトはBLACK)
mPaint.setColor(Color.BLUE);

//テキスト表示
canvas.drawText(getString(R.string.hello), 0, 20, mPaint);

//画像表示
canvas.drawBitmap(mBitmap,
imageX - mBitmap.getWidth() / 2,
imageY - mBitmap.getHeight() / 2,
mPaint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

//触る
if(event.getAction() == MotionEvent.ACTION_DOWN){
imageX = event.getX();
imageY = event.getY();
}
//触ったままスライド
else if(event.getAction() == MotionEvent.ACTION_MOVE){
imageX = event.getX();
imageY = event.getY();
}

//離す
else if(event.getAction() == MotionEvent.ACTION_UP){
imageX = event.getX();
imageY = event.getY();
}

// 再描画の指示
invalidate();

return true;
}
}
}

------------------------------------------------------------------------- 



これで完了です。お疲れ様でした合格


次回、動かしていきましょう。ではでは。


ペタしてね

さてさて、前回のアプリをエミュレータで動かしていきましょう。


Eclipseの『実行(R)』から『実行ダイアログを開く(N)』をクリックして、今回のプロジェクトを

選択します。

HelloWorldを実行したときと同じ要領で、動かしましょう。


よー様のAndroid的備忘録
プロジェクトを選択して、『実行(R)』ボタンを押しましょう。



エミュレータが立ち上がります。

クリックドラッグで、ロックを解除しましょう。
よー様のAndroid的備忘録


メイン画面が立ち上がります。

ここは、main.xmlに書いた内容のレイアウトで表示されます。
よー様のAndroid的備忘録


右の『menu』ボタンで、メニューを表示してみましょう。

ここら辺の動きは、MainActivity.javaの内容になります。
よー様のAndroid的備忘録
menuの左側のボタンを押してみましょう。



次の画面に遷移します。

ここのレイアウトはnext.xml,動作はNextActivity.javaになります。
よー様のAndroid的備忘録


よー様のAndroid的備忘録
メイン画面に戻ります。

※内部の仕組み的には移動していますので、厳密には『戻る』ではないです。

 左の戻るボタンを押すと本当の意味で前の画面に戻りますので、メイン画面に

 menuの機能で移動した後で、左の戻るボタンを押すと、青い画面(次の画面)に

 戻ってしまいます。




よー様のAndroid的備忘録





以上で画面遷移は終わりです。

この機能をだけを使うようなアプリはあんまりないんじゃないかなーと思いますが、

アプリの設定画面が複数にまたがった時なんかに利用できそうな感じではあります。



ではでは。


ペタしてね


アップアップ前回の色違いですアップアップ

お久しぶりです。本業が忙しくてなかなかアクセスできていませんでしたあせる

ぼちぼち復活していきます。アップアップ



今回のアプリは画面遷移がテーマです。

某サイトのパクリですが、まずはコピペで作って動かしていきましょう。


Eclipseを起動してみます。



『ファイル(F)』⇒『新規(N)』⇒『Androidプロジェクト』を選択しましょう。

ScreenTransitionDemoプロジェクトを作成します。
よー様のAndroid的備忘録
アップ上記のように入力していきましょう。アップ

入力が完了したら、『終了(F)』をクリック。



入力が完了したら、ソースをコピペで作っていきましょう。

まずはScreenTransitionDemo/res/valuesにある、xmlファイルを

作っていきましょう。


Valuesを右クリックして、新規(W)からその他(O)をクリック、

『Android XML File』を選択して、『次へ(N)>』。


ファイルに『color.xml』を入力して、『終了(F)』をクリックしましょう。

これで、XMLファイルが作成されます。
よー様のAndroid的備忘録


よー様のAndroid的備忘録


valuesフォルダのcolor.xmlとstrings.xmlを下記の内容に置き換えましょう。

パッケージエクスプローラから、xmlファイルを右クリックして内容を表示させ、

表示されたタブの左下を『リソース』から『~.xml』に切り替えます。

そこで、ソースの置き換えをしましょう。


color.xml-----------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#000000</color>
<color name="pink">#FFC0CB</color>
<color name="deepskyblue">#00BFFF</color>
</resources>


-------------------------------------------------------------


strings.xml----------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ScreenTransitionDemo</string>
<string name="main_msg">Main Screen</string>
<string name="next_msg">Next Screen</string>

<string name="menu_main">メイン画面</string>
<string name="menu_next">次の画面</string>
<string name="menu_finish">Activity終了</string>
</resources>

-------------------------------------------------------------

color.xmlは色の固定値を、strings.xmlは文字列の固定値をそれぞれ

定義しています。

※ネーミング規則的にはcolor.xmlではなくcolors.xmlだとは思うのですが、

ちょっと変えて動かなくなってもヤなので、そのままにしておきます。




次layoutフォルダのmain.xmlの内容を置き換えましょう。


main.xml------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android "
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/pink"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="@string/main_msg"
/>
</LinearLayout>
--------------------------------------------------------------

@colorや@stringsはそれぞれ、color.xmlやstring.xmlを意味しています。

color.xmlやstring.xmlで定義された変数をここで使用しています。




メイン画面の画面レイアウトはこれで作成できたので、こんどはメイン画面の動きを

定義する『MainActivity.java』を編集していきましょう。

ScreenTransitionDemo/src/android.sample/MainActivityをダブルクリックして

ソースを表示させます。


MainActivity.java------------------------------------------------

package android.sample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
/** メニューID */
private final int MENU_ID1 = Menu.FIRST;
private final int MENU_ID2 = Menu.FIRST + 1;

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

/**
* メニューを作成する
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0,
MENU_ID1,
0,
getString(R.string.menu_next)).setIcon(android.R.drawable.ic_menu_crop);
menu.add(0,
MENU_ID2,
1,
getString(R.string.menu_finish)).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return true;
}
/**
* メニューボタン押下時の処理
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return true;
}

/**
* メニューを選択時の処理
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ID1:
//次の画面に遷移させる
Intent intent = new Intent();
intent.setClassName(
"android.sample",
"android.sample.NextActivity");
startActivity(intent);
return true;
case MENU_ID2:
//Activity終了
finish();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}

--------------------------------------------------------------

ひとまず、これに置き換えてみましょう。何をしているのかは、

ソース内のコメントを参考にしてみるとして、今は『こんなことができる』ということが

わかれば、それでいいのかなーと思います。




次に、遷移先画面のjavaとxmlを作成していきましょう。

xmlファイルで画面のレイアウトを、javaファイルで画面の動作を定義していきます。


ScreenTransitionDemo/res/layoutを右クリックして、新規(W)からその他(O)

をクリック、『Android XML File』を選択して、『次へ(N)>』。

こんどはファイルに『next.xml』を入力して、『終了(F)』をクリック。

同様に書き換えていきましょう。
main.xmlとほぼ同じ内容です。


next.xml------------------------------------------------------

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

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="@string/next_msg"
/>
</LinearLayout>

--------------------------------------------------------------



javaファイルは、『ScreenTransitionDemo/src/android.sample/』を右クリックして

『新規(W)』⇒『クラス』で作成します。

よー様のAndroid的備忘録
念のため、スーパークラスに『android.app.Activity』を入力しておきましょう。

※どうせ内容を置き換えるので、名前さえあっていれば、内容はどうでもいいンですが・・・


NextActivity--------------------------------------------------

package android.sample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class NextActivity extends Activity {
/** メニューID */
private final int MENU_ID1 = Menu.FIRST;
private final int MENU_ID2 = Menu.FIRST + 1;

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

/**
* メニューを作成する
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0,
MENU_ID1,
0,
getString(R.string.menu_main)).setIcon(android.R.drawable.ic_menu_crop);
menu.add(0,
MENU_ID2,
1,
getString(R.string.menu_finish)).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return true;
}
/**
* メニューボタン押下時の処理
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return true;
}

/**
* メニューを選択時の処理
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ID1:
//次の画面に遷移させる
Intent intent = new Intent();
//intent.putExtra("skin_changed", true);
intent.setClassName(
"android.sample",
"android.sample.MainActivity");
startActivity(intent);
return true;
case MENU_ID2:
//Activity終了
finish();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
--------------------------------------------------------------




最後にresフォルダ直下の『AndroidManifest.xml』を書き換えましょう。

デフォルトではNextActivityの記載がないので、追加します。

AndroidManifest.xml---------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.sample"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<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>
<activity android:name="NextActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
--------------------------------------------------------------



これで、ものづくりは終わりです。

次回、MainとNextの遷移を確認していきましょう。

ではでは。

ペタしてね


アップアップあったかくてお勧めですアップアップ

さて、今回はアプリを動かしていきましょう。


まずは、androidエミュレータの設定です。


Eclipseを起動して、『ウィンドウ(W)』から、

『Android SDK and AVD Manager』を

クリックしましょう。



開いたウィンドウの左メニューから、『Virtual Devices』を選択し、

右の『新規』ボタンをクリックします。

以前、設定したエミュレータの設定と同じですねw

しかしここからが違います。


前回はAndroid2.1用のエミュレータを設定しましたが、

今回はGoogle APIs用のエミュレータを設定します。

以下のように設定しましょう。
よー様のAndroid的備忘録

アップターゲットにGoogle APIsを選択します。アップ



設定し終われば、『Create AVD』をクリックして作成します。

これで、エミュレータの設定は完了しました。

このエミュレータを使って、アプリを動かしてみましょうひらめき電球



『実行(R)』から、『実行ダイアログを開く(N)』をクリックします。

下記のように、アプリを選択します。
よー様のAndroid的備忘録


エミュレータは自動的に選択されるので、特に何も設定は不要ですが、

この画面のターゲットタグから確認できます。



設定し終われば、『実行(R)』から実行してみましょう。




エミュレータが起動され、アプリが起動されました音譜

おめでとうございます音譜音譜
よー様のAndroid的備忘録



例のごとく、アプリを終了しても、エミュレータのメニューから

アイコンをクリックすれば実行できますひらめき電球





ではでは、これにてw




このクッション、マジすごいですアップアップ

肩こり、腰痛持ちは必須アイテムですねw買っちゃいましたwwアップ

GoogleMapのApiKeyの取得からでした、

早速、はじめましょう。


最初はEclipse関係ないので、勘違いしないでくださいねw

突然ですが、OS画面左下のWindowsボタンから、

『ファイル名を実行して起動(R)』をクリックしましょう。

『cmd』と入力して、『OK』ボタンを押しましょう。
よー様のAndroid的備忘録



黒い画面が表示されます。

この画面に、実効命令(コマンド)を入力していきましょう。



まずはAndroidのJREを解凍したドライブを入力しましょう。


たとえば、D:ドライブの場合、『D:』と入力してからEnterキー

を押してください。


>の前が『D:』になったかと思います。



次に、AndroidのJREを解凍したフォルダの中の、binフォルダ

に移動しましょう。

『cd (JRE解凍フォルダ)\bin』で移動できます。

入力し終わったら、Enterキーです。


私の場合はこうでした、

cd pleiades-all-in-one-ultimate-jre_20080303\jre\bin


これで、binの下まで移動できました。



次は、下記のコマンドを入力してみましょう。

keytool -list -keystore "%USERPROFILE%\.android\debug.keystore"


画面の右端にきてしまっても、気にしないで最後まで入力してしまいましょう。

最後は、やっぱりEnterキーです。



するとするとこんなのが、バラバラっと表示されるはずです。

パスワードを聞かれますが、無視してEnterですw



--------------------------------------------------------------------

D:\pleiades-all-in-one-ultimate-jre_20080303\jre\bin>keytool -list -keystore "%U
SERPROFILE%\.android\debug.keystore"
キーストアのパスワードを入力してください:

***************** 警告 警告 警告 *****************
* キーストアに保存された情報の完全性は検証されて *
* いません! 完全性を検証するには、キーストアの *
* パスワードを入力する必要があります。 *
***************** 警告 警告 警告 *****************

キーストアのタイプ: JKS
キーストアのプロバイダ: SUN

キーストアには 1 エントリが含まれます。

androiddebugkey, 2010/10/16, PrivateKeyEntry,
証明書のフィンガープリント (MD5): 『文字がいっぱいww』


D:\pleiades-all-in-one-ultimate-jre_20080303\jre\bin>

--------------------------------------------------------------------


証明書のフィンガープリント (MD5): 『文字がいっぱいww』

のピンクば箇所が重要です。メモ帳などに書いて保存しておきましょう。

この文字を『フィンガープリント』と呼ぶことにします。



今度はブラウザを立ち上げましょう。

フィンガープリントを使って、Apiキーを取得します。


下記のアドレスに移動しましょう。

http://code.google.com/android/maps-api-signup.html
よー様のAndroid的備忘録


英語なので、内容はさっぱりですが、同意してほしそうなので、

チェックボックスにチェックをいれて、その下のテキストボックスには

先ほど取得したフィンガープリントを入力しましょう。

入力したら、『Generate API Key』をクリックです。



画面が、変わります。

画像のは、マスクをかけましたがこれでAPIKEYが取得できます。
よー様のAndroid的備忘録


このKEYをres/values/String.xml』に記述すれば、アプリは

完成です☆

おつかれさまでした。




今日は遅いので、次の落ち着いた時間にアプリを実行してみましょう。



ではではこれにて。

間違えて、ブラウザをいったん閉じてしまい、

途中まで書いた記事が、すべて消えてしまいました。。。

しょんぼりです。

気を取り直してアプリ開発を進めていきましょう。


Eclipseを立ち上げて、

『ファイル(F)』⇒『新規(N)』⇒『Androidプロジェクト』から

下記のように入力して、プロジェクトを作成しましょう。

注意点は、『Build Target』に『Google APIs』が入力されている点と

『Create Activity』のチェックボックスのチェックをはずした点です。

『終了(F)』でプロジェクトを作成しましょう。
よー様のAndroid的備忘録


こんな感じでプロジェクトができました。
よー様のAndroid的備忘録


src配下のパッケージ(上の例であればcom.example.map.android)を右クリックし、

新規(N)⇒クラスをクリックしましょう。

下記のように入力しました。

注意点は、スーパークラスに『com.google.android.maps.MapActivity』を入力する点です。

入力し終わったら、『終了(F)』でクラスも作成しましょう。
よー様のAndroid的備忘録



あとは、各ソースを書き換えていきます。



ピンク色の箇所を追加していきましょう。

☆src/com.example.map.android/HelloMapActivity☆----------------


package com.example.map.android;
import android.os.Bundle;
import com.google.android.maps.MapActivity;

public class HelloMapActivity extends MapActivity {

@Override
protected boolean isRouteDisplayed() {
// TODO 自動生成されたメソッド・スタブ
return false;
}
@Override
// android.os.Bundleのメソッド
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}


-------------------------------------------------------------



今回はテキストを表示するわけではなく、マップを表示するので、

『<TextView ~ />』は消して、ピンク色箇所を追加しましょう。

☆res/layout/main.xml☆----------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android "
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="@string/api_key"
/>
</LinearLayout>

-------------------------------------------------------------



ここは、AndroidのApiKeyを設定したいですが、これは

事前にもらっておく必要があります。

このKeyの取得は次回説明しますので、いったんダミーを

入力しておきましょう。

☆res/values/String.xml☆----------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="api_key">★ApiKey★</string>

<string name="app_name">アンドロイドチュートリアル2</string>
</resources>

---------------------------------------------------------------



下記のピンクの箇所も追加しましょう。

インターネットやMapを使う絡みです。

☆res/AndroidManifest.xml☆--------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android "
package="com.example.map.android"
android:versionCode="1"
android:versionName="1.0">

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

<application android:icon="@drawable/icon" android:label="@string/app_name">


<uses-library android:name="com.google.android.maps"/>
<activity android:name=".HelloMapActivity"
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>
<uses-sdk android:minSdkVersion="7" />

</manifest>


---------------------------------------------------------------




これでApiKeyのところを残して、すべて変更し終えました。

忘れずに、各ファイルの保存をしておきましょう。

Eclipseの左上の方に保存ボタンがあります。




次回は、Keyを取得し、実行するところまでをやってみましょう。




おなかがすいてきました。じゅるり。

先ほど、仕事から家に帰ってきました☆

残業にも負けず、更新です♪



ではでは参りましょう。

今日はアプリでGoogleMapを使えるようにしていきましょう。



Eclipseを起動して、左上の『ファイル(F)』から『新規(N)』、

『Androidプロジェクト』をクリックします。
よー様のAndroid的備忘録


こんな画面が出てきました。

この画面の『Build Target』に注目です。


GoogleAPIsを使うにはここで選択しなければいけません。

このEclipseにはまだ入れていなかったようなので、準備しましょう。


いったんこの画面は『×』で消して、

『ウィンドウ(W)』の『Android SDK…』をクリックしましょう。
よー様のAndroid的備忘録

この画面の左にある、『Available Packages』を選択し、

左に出てきた青い丸のチェックボックスをクリックしましょう。

内容が展開されますが、そのまま、右下の

『Install Selected』ボタンをクリックします。



左のパッケージ欄に複数パッケージが表示されますが、

すべて『受諾』を選択してあげましょう。

※このパッケージの中に『Google APIs ~』っていうのがあるのを確認しましょう。



すべてのパッケージについて受諾したら、右下の『インストール』ボタンを押しましょう。

インストールが始まります。待ちましょう。
よー様のAndroid的備忘録


※コンソールにエラーメッセージが出てくるかも知れませんが、気にしなくてよさそうです。


『Done. 5 Packages installed』のメッセージがでれば、完了っぽいので

『閉じる』ボタンを押しましょう。(『5』じゃなくても気にしなくていいですw)

パッケージの選択画面も『×』で閉じちゃいましょう




さて、先ほどのところにGoogle APISは出てきてるのでしょうか???
よー様のAndroid的備忘録




出てました!!!!!



次回はこいつを使ってプロジェクトを作成していきましょう。





そろそろこんな季節がやってきました☆

たまには普通っぽいことやってみようかとw


よー様のAndroid的備忘録


コンビニで売ってました。

第一印象『まずそーww


しかし、チャレンジャーのよー様は手に取り、購入してしまいました。







実食☆






ん?意外と飲みやすい?

さわやかな酸味と甘みで、イけるかもw


まあまあ、合格点はあげされそうです♪



美味 : ★★★☆☆   

包装 : ★★★★☆

価格 : ★★★☆☆

成分 : ★★★☆☆

総合 : ★★★☆☆






メーカのHPです

http://www.meinyu.jp/product/new/milk_drink/





言わずと知れた名作。MAXCOFFEE☆ 飲み物つながりということで。