Two activities connected with an intent with a string message.
authorMartin Pitt <martin@piware.de>
Fri, 29 May 2020 08:55:51 +0000 (10:55 +0200)
committerMartin Pitt <martin@piware.de>
Fri, 29 May 2020 08:55:51 +0000 (10:55 +0200)
Concludes <https://developer.android.com/training/basics/firstapp/building-ui>

app/src/main/AndroidManifest.xml
app/src/main/java/com/example/pittihelloworld/DisplayMessageActivity.kt [new file with mode: 0644]
app/src/main/java/com/example/pittihelloworld/MainActivity.kt
app/src/main/res/layout/activity_display_message.xml [new file with mode: 0644]
app/src/main/res/layout/activity_main.xml
app/src/main/res/values/strings.xml

index a104c54..99c7675 100644 (file)
@@ -9,6 +9,7 @@
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
+        <activity android:name=".DisplayMessageActivity" android:parentActivityName=".MainActivity" />
         <activity android:name=".MainActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
diff --git a/app/src/main/java/com/example/pittihelloworld/DisplayMessageActivity.kt b/app/src/main/java/com/example/pittihelloworld/DisplayMessageActivity.kt
new file mode 100644 (file)
index 0000000..25ddbc6
--- /dev/null
@@ -0,0 +1,18 @@
+package com.example.pittihelloworld
+
+import androidx.appcompat.app.AppCompatActivity
+import android.os.Bundle
+import android.widget.TextView
+
+class DisplayMessageActivity : AppCompatActivity() {
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        setContentView(R.layout.activity_display_message)
+
+        // extract message from intent
+        val message = intent.getStringExtra(EXTRA_MESSAGE)
+        findViewById<TextView>(R.id.textView).apply {
+            text = message
+        }
+    }
+}
\ No newline at end of file
index 9e808dc..540fc06 100644 (file)
@@ -1,11 +1,25 @@
 package com.example.pittihelloworld
 
+import android.content.Intent
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
+import android.view.View
+import android.widget.EditText
+
+const val EXTRA_MESSAGE = "com.example.pittihelloworld.MESSAGE"
 
 class MainActivity : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
     }
+
+    fun sendMessage(view: View) {
+        val editText = findViewById<EditText>(R.id.messageText)
+        val message = editText.text.toString()
+        val intent = Intent(this, DisplayMessageActivity::class.java).apply {
+            putExtra(EXTRA_MESSAGE, message)
+        }
+        startActivity(intent)
+    }
 }
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_display_message.xml b/app/src/main/res/layout/activity_display_message.xml
new file mode 100644 (file)
index 0000000..05dc07c
--- /dev/null
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".DisplayMessageActivity">
+
+    <TextView
+        android:id="@+id/textView"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="16dp"
+        android:textColor="@color/colorPrimary"
+        android:textSize="14sp"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintHorizontal_bias="0.498"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
index 9212deb..b9c73fe 100644 (file)
@@ -6,13 +6,28 @@
     android:layout_height="match_parent"
     tools:context=".MainActivity">
 
-    <TextView
-        android:layout_width="wrap_content"
+    <EditText
+        android:id="@+id/messageText"
+        android:layout_width="0dp"
         android:layout_height="wrap_content"
-        android:text="Hello Android World!"
-        app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintLeft_toLeftOf="parent"
-        app:layout_constraintRight_toRightOf="parent"
-        app:layout_constraintTop_toTopOf="parent" />
+        android:ems="10"
+        android:hint="@string/edit_message"
+        android:inputType="textPersonName"
+        app:layout_constraintEnd_toStartOf="@+id/sendButton"
+        app:layout_constraintHorizontal_bias="0.5"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent"/>
 
+    <Button
+        android:id="@+id/sendButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="16dp"
+        android:layout_marginEnd="16dp"
+        android:onClick="sendMessage"
+        android:text="@string/button_send"
+        app:layout_constraintBaseline_toBaselineOf="@+id/messageText"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintHorizontal_bias="0.5"
+        app:layout_constraintStart_toEndOf="@+id/messageText" />
 </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
index 4660ee7..a284136 100644 (file)
@@ -1,3 +1,5 @@
 <resources>
     <string name="app_name">Pitti Hello World</string>
+    <string name="edit_message">Enter a message</string>
+    <string name="button_send">Send</string>
 </resources>
\ No newline at end of file