Starting Another Activity master
authorAntonio Ospite <ospite@studenti.unina.it>
Mon, 4 Mar 2013 11:16:32 +0000 (12:16 +0100)
committerAntonio Ospite <ospite@studenti.unina.it>
Mon, 4 Mar 2013 11:26:02 +0000 (12:26 +0100)
http://developer.android.com/training/basics/firstapp/starting-activity.html

AndroidManifest.xml
libs/android-support-v4.jar [new file with mode: 0644]
res/layout/main.xml
res/values/strings.xml
src/com/example/myfirstapp/DisplayMessageActivity.java [new file with mode: 0644]
src/com/example/myfirstapp/MainActivity.java

index 6977e8d..bf2dc00 100644 (file)
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
                 <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>
     </application>
 </manifest>
diff --git a/libs/android-support-v4.jar b/libs/android-support-v4.jar
new file mode 100644 (file)
index 0000000..6080877
Binary files /dev/null and b/libs/android-support-v4.jar differ
index 6125c3a..fed6a22 100644 (file)
@@ -14,6 +14,7 @@
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
     <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>
 
 </LinearLayout>
index ed4339c..d1cfceb 100644 (file)
@@ -5,4 +5,5 @@
     <string name="button_send">Send</string>
     <string name="menu_settings">Settings</string>
     <string name="title_activity_main">MainActivity</string>
     <string name="button_send">Send</string>
     <string name="menu_settings">Settings</string>
     <string name="title_activity_main">MainActivity</string>
+    <string name="title_activity_display_message">My Message</string>
 </resources>
 </resources>
diff --git a/src/com/example/myfirstapp/DisplayMessageActivity.java b/src/com/example/myfirstapp/DisplayMessageActivity.java
new file mode 100644 (file)
index 0000000..6f25ee0
--- /dev/null
@@ -0,0 +1,47 @@
+package com.example.myfirstapp;
+
+import android.annotation.SuppressLint;
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Build;
+import android.os.Bundle;
+import android.support.v4.app.NavUtils;
+import android.view.MenuItem;
+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);
+
+
+        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
+            // Show the Up button in the action bar.
+            getActionBar().setDisplayHomeAsUpEnabled(true);
+        }
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        switch (item.getItemId()) {
+        case android.R.id.home:
+            NavUtils.navigateUpFromSameTask(this);
+            return true;
+        }
+        return super.onOptionsItemSelected(item);
+    }
+}
index aea2141..2cb068e 100644 (file)
@@ -1,10 +1,15 @@
 package com.example.myfirstapp;
 
 import android.app.Activity;
 package com.example.myfirstapp;
 
 import android.app.Activity;
+import android.content.Intent;
 import android.os.Bundle;
 import android.os.Bundle;
+import android.view.View;
+import android.widget.EditText;
 
 public class MainActivity extends Activity
 {
 
 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)
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState)
@@ -12,4 +17,13 @@ public class MainActivity extends Activity
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
     }
+
+    /** Called when the user clicks the Send button */
+    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);
+    }
 }
 }