From: Martin Pitt Date: Fri, 29 May 2020 08:55:51 +0000 (+0200) Subject: Two activities connected with an intent with a string message. X-Git-Url: https://piware.de/gitweb/?a=commitdiff_plain;h=c96705bf5fa806ef00a7db674b2e524196f6729a;p=android-PittiHelloWorld.git Two activities connected with an intent with a string message. Concludes --- diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a104c54..99c7675 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> + 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 index 0000000..25ddbc6 --- /dev/null +++ b/app/src/main/java/com/example/pittihelloworld/DisplayMessageActivity.kt @@ -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(R.id.textView).apply { + text = message + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/pittihelloworld/MainActivity.kt b/app/src/main/java/com/example/pittihelloworld/MainActivity.kt index 9e808dc..540fc06 100644 --- a/app/src/main/java/com/example/pittihelloworld/MainActivity.kt +++ b/app/src/main/java/com/example/pittihelloworld/MainActivity.kt @@ -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(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 index 0000000..05dc07c --- /dev/null +++ b/app/src/main/res/layout/activity_display_message.xml @@ -0,0 +1,20 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9212deb..b9c73fe 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,13 +6,28 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - + 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"/> +