2013年5月7日火曜日

最初に起動する Activity の指定

Android でアプリを起動したときに最初に起動する Activity は AndroidManifest.xml で指定します。

アプリのアイコンをタッチすると <intent-filter> タグで actionMAIN
 categoryLAUNCHER が指定されている Activity の onCreate() が呼び出されます。
<activity> タグの name 属性にパッケージ名も含めた Activity のクラス名を書けば OK です。

<activity android:name="com.example.hoge.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>

詳しくはこちら
Starting on Activity - Android Developers
http://developer.android.com/training/basics/activity-lifecycle/starting.html#launching-activity

2013年5月6日月曜日

Intent とは

Intent とは Activity 間の通信や、アプリ間での通信を行う機能のこと。
別の画面を表示したり、他のアプリを呼び出したりするときに使う。
イベントやメッセージのようなもの。

全体的な説明はこちら
Intent and Intent Filter - Android Developers
http://developer.android.com/guide/components/intents-filters.html


Intent を作るときには Intent クラスをインスタンス化して使う。
Intent class - Android Developers
http://developer.android.com/reference/android/content/Intent.html


別のアクティビティを起動するときはこんな感じ。
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
第一引数が Context 、第二引数が送り先のクラスです。
他にも色々とコンストラクタがあるので必要に応じて使い分けることになります。

Intent には HTTP の GET や POST のようにデータをくっつけることができます。
他のページを初期化するときや、特定のアクションを実行したいときなどに使えそうです。
また、今回のように相手を直接指定するのではなく、
条件に合う相手を探して送る機能もあるそうです。
それらについてはまた別の記事で触れます。

Intent についてはこちらがわかりやすいです。
intent - Androidのド肝
http://blog.haw.co.jp/android/?p=54

2013年5月5日日曜日

Activity とは

Activity とはアプリを構成する画面1枚を指す言葉です。
画面を作るときには Activity クラスのサブクラスを作って
onCreate() や onPause() を実装するとのこと。
Android システムが自動的に状態遷移を行なってくれて、
その都度 onCreate() などのメソッドが呼ばれます。
Activity クラスは android.app パッケージで定義されています。

Activity - Android Developers

Activity class - Android Developers


Activity についてはこちらがわかりやすいです。

Activity とは何か? - @IT
http://www.atmarkit.co.jp/fsmart/articles/android02/android02_1.html



別の Activity が呼び出されると前の Activity をスタックに保存しておいてくれたり、
マニフェストファイルで最初に起動する Activity を指定できたりするなど
色々あるようなので、別の記事でそれぞれ触れていこうと思います。

2013年5月4日土曜日

Android アプリ:イベントのバインドとキャッチ

Android でイベントを処理する方法です。

ADT で Blank Activity を選ぶと Hello world! と表示されるアプリが作られます。
Hello world! をタッチしたら Hello Android world! に変えましょう。

イベントについてはこちらに解説があります
Android Developer - Input Events
http://developer.android.com/guide/topics/ui/ui-events.html

手順
  1. 文字を大きくする
  2. TextView に id をつける
  3. id を使って TextView を取得する
  4. OnClickListener を実装する
  5. OnClickListener を View へセットする
  6. onClick() 内で文字列を切り替える
  7. 動作確認

文字を大きくする


デフォルトのレイアウトはこんな感じ
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>


このままではテキストが小さいので大きくします。
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textSize="30pt"
        android:text="@string/hello_world" />

タップできるくらいの大きさにする

テキストの指定についてはこちら
Android Developers - R.attr : textSize http://developer.android.com/reference/android/R.attr.html#textSize

TextView に id をつける


Java から TextView を取得するために xml で id を設定します。
これで R.id.hello_world でこの TextView を参照できるようになります。
    <TextView
        android:id="@+id/helloworld"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30pt"
        android:text="@string/hello_world" />



Java のソースを開きます。
package com.example.eventlistener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

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

}

id を使って TextView を取得する


TextView を保存する変数を作るために android.widget.TextView をインポートします。
import android.widget.TextView;

Activity に onStart() メソッドを追加して TextView を取得します。
public class MainActivity extends Activity {
 ...
 protected void onStart() {
  super.onStart();
  TextView textView = (TextView)findViewById(R.id.helloworld);
 }
 ...
}

OnClickListener を実装する


Click イベントをキャプチャするために、android.view パッケージで提供されている OnClickListener インタフェースを使います。

Android Developers - View.OnClickListener
http://developer.android.com/reference/android/view/View.OnClickListener.html

OnClickListener インタフェースは onClick() メソッドを宣言していて、クリックされた時に呼び出されます。

まず、OnClickListener インタフェースをインポートします。
import android.view.View.OnClickListener;

OnClickListener インタフェースを MainActivity クラスへ実装します。
public class MainActivity extends Activity implements OnClickListener {
 ...
}

インタフェースのメソッドを実装します。
public class MainActivity extends Activity implements OnClickListener {
 ...
 public void onClick(View v) { 
 }
}

OnClickListener を View へセットする


次に、TextView がクリックされた時に、このメソッドが呼ばれるようにしましょう。
View の setOnClickListener() メソッドを使います。

Android Developers - setOnClickListener
http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)

先ほど取得した TextView にリスナを追加します。
 protected void onStart() {
  super.onStart();
  TextView textView = (TextView)findViewById(R.id.helloworld);
  textView.setOnClickListener(this);
 }

引数には OnClickListener インタフェースを実装した(onClick()メソッドを持っている)クラスを渡します。
今回は MainActivity 自身にもたせているので、 this で MainActivity 自身を渡しています。
これで TextView がタップされた時に MainActivity の onClick() メソッドが呼ばれることになりました。

onClick() 内で文字列を切り替える


最後に onClick() メソッド内で TextView のテキストを変えます。
まず、変更する文字列を定義しておきます。
/res/values/strings.xml へ文字列 hello_android_world を追加します。
ファイルを保存すると、自動的に R.java へ参照が追加されます。
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">EventListener</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="hello_android_world">Hello Android world!</string>

</resources>

では、文字列を変えましょう。
onClick() メソッドの引数としてクリックされた View が渡されます。
これを TextView へキャストして、setText() メソッドで参照する文字列を変えます。
 public void onClick(View v) {
  ((TextView)v).setText(R.string.hello_android_world);
 }

動作確認


Android 端末で実行します。
テキストをタップすると文字列が変わることが確認できます。

タップしたら文字列が変わりました

2013年5月3日金曜日

Android アプリ: View の幅と高さを設定

XML で View を書くときに幅と高さを設定しなくてはいけません。
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
android:layout_width
android:layout_height

幅と高さは、値を直接指定するか、キーワードによる指定ができます。

直接指定する場合は、px や mm などの単位を使って数値を指定します。

Android Developer - More Resource Type - Dimention
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension


指定できるキーワード

match_parent
    親に合わせる。fill_parent に変わって API Level 8 (Android 2.2) から登場。

fill_parent 
    親に合わせる。何が違うんだろうか・・・

wrap_content 
    要素に合わせた大きさになる

Android Developer - Layout Resource - Layout Values
http://developer.android.com/guide/topics/resources/layout-resource.html#layoutvalues


mach_parent と fill_parent の違いがなにか調べてみると、どうもどちらも同じもののようです。Android 2.2 以降では match_parent が推奨されているらしいので、 match_parent を使うことにします。

Androidアプリ開発 - wrap_contentとfill_parentとmatch_parentの違い
http://android49.blog.fc2.com/blog-entry-20.html


2013年5月2日木曜日

GitHub の公式ロゴをリンク画像に使う

GitHub のロゴが公式から落とせます

GitHub - GitHub Logos and Usage
https://github.com/logos

自分の GitHub ページヘリンクする画像として使えます。
アプリに組み込んだりはできないので注意が必要です。

2013年5月1日水曜日

Layout とは何者か

ADT で Blank layout を選ぶと、res/layout/activity_main.xml はこんなかんじになってます。
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

一番上は ******Layout じゃないとダメなんだろうか?

そもそも Layout とは何なのか・・・。

Android Developers - Layouts
http://developer.android.com/guide/topics/ui/declaring-layout.html


簡単にまとめてみると
  • レイアウトはアプリのUIやウィジットのUIを決めるもの
  • XMLで書ける
  • XMLの要素名が View のクラス名に、属性名がメソッド名に一致する(例外有り)
  • 画面の向き、デバイスのサイズ、言語別にレイアウトを作成できる
  • 必ず1つのルート要素を持つ

更にリンクを辿っていくと、疑問に思っていたことを発見。

Android Developers - Layout Resource
http://developer.android.com/guide/topics/resources/layout-resource.html

Note: The root element can be either a ViewGroup, a View, or a <merge> element, but there must be only one root element and it must contain the xmlns:android attribute with the android namespace as shown.

一番上に来るのは、ViewGroup か View を継承しているクラスなら何でもいいみたいです。ただし、ルート要素で名前空間を指定する必要あり。

じゃあ Layout は ViewGroup のサブクラスなのかと思ったらその通りでした。

Android Developers - ViewGroup
http://developer.android.com/reference/android/view/ViewGroup.html


で、その ViewGroup は View のサブクラスでした。

Android Developers -View
http://developer.android.com/reference/android/view/View.html

View が一番上にいるようです。

ということで Layout 以外でも一番上に来ていいことがわかりました。



ちなみに、各 Layout についてはこちらがわかりやすかったです

@IT - Androidアプリの使いやすさを左右する5つのレイアウト
http://www.atmarkit.co.jp/fsmart/articles/android05/android05_1.html