あいつの日誌β

あいつの日誌です。

初めての Android App

Getting Start

ここ見てやった事をまとめた記事です。 Eclipse も IntellJ といった IDE を使わないで vim とコマンドラインでやってみました。

http://developer.android.com/training/index.html

Creating an Android Project

% android list targets
% android create project --target android-18 --name MyFirstApp --path ~/MyFirstApp --activity MainActivity --package com.example.myfirstapp
% cd MyFirstApp

Running Your App

実機がつながっている事を確認

% adb devices
List of devices attached 
015d4b33be40260a        device

build & install

% ant debug
% adb install bin/MyFirstApp-debug.apk

Failure [INSTALL_FAILED_ALREADY_EXISTS] となった場合は以下

% adb install -r bin/MyFirstApp-debug.apk

Building a Simple User Interface

Edit: res/layout/activity_main.xml

<LinearLayout 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:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>

And Edit: res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>

rebuild

% ant debug
% adb install -r bin/MyFirstApp-debug.apk

Starting Another Activitys

Edit: AndroidManifest.xm

--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -11,5 +11,13 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+        <activity
+            android:name="com.example.myfirstapp.DisplayMessageActivity"
+            android:label="@string/title_activity_display_message"
+            android:parentActivityName="com.example.myfirstapp.MainActivity" >
+            <meta-data
+                android:name="android.support.PARENT_ACTIVITY"
+                android:value="com.example.myfirstapp.MainActivity" />
+        </activity>
     </application>
 </manifest>

Edit: res/laytou/main.xml

--- a/res/layout/main.xml
+++ b/res/layout/main.xml
@@ -11,5 +11,6 @@
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:text="@string/button_send" />
+        android:text="@string/button_send"
+        android:onClick="sendMessage" />
 </LinearLayout>

MainActivity に sendMessage を追加

Edit: src/com/example/myfirstapp/MainActivity.java

--- a/src/com/example/myfirstapp/MainActivity.java
+++ b/src/com/example/myfirstapp/MainActivity.java
@@ -2,9 +2,14 @@ package com.example.myfirstapp;
 
 import android.app.Activity;
 import android.os.Bundle;
+import android.view.View;
+import android.content.Intent;
+import android.widget.EditText;
 
 public class MainActivity extends Activity
 {
+    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
+
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState)
@@ -12,4 +17,12 @@ public class MainActivity extends Activity
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }
+
+    public void sendMessage(View view) {
+        Intent intent = new Intent(this, DisplayMessageActivity.class);
+        EditText editText = (EditText) findViewById(R.id.edit_message);
+        String message = editText.getText().toString();
+        intent.putExtra(EXTRA_MESSAGE, message);
+        startActivity(intent);
+    }
 }

Add: src/com/example/myfirstapp/DisplayMessageActivity.java

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.widget.EditText;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);


        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }
}

build & install

% and debug
% adb install -r bin/MyFirstApp-debug.apk