From b7910cd757df2b6a86dbf1658a290aa66c109e24 Mon Sep 17 00:00:00 2001 From: Iftakhar Husan <AC5636@student.jamk.fi> Date: Wed, 13 Sep 2023 22:27:41 +0300 Subject: [PATCH] Add functionality for button click --- .../e01firstapplication/MainActivity.kt | 12 ++++++++++- .../app/src/main/res/layout/activity_main.xml | 20 +++++++++++++++---- .../app/src/main/res/values-fi/strings.xml | 6 ++++++ .../app/src/main/res/values/strings.xml | 4 +++- 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 e01-first-application/app/src/main/res/values-fi/strings.xml diff --git a/e01-first-application/app/src/main/java/com/example/e01firstapplication/MainActivity.kt b/e01-first-application/app/src/main/java/com/example/e01firstapplication/MainActivity.kt index 2c083fa..d854e43 100644 --- a/e01-first-application/app/src/main/java/com/example/e01firstapplication/MainActivity.kt +++ b/e01-first-application/app/src/main/java/com/example/e01firstapplication/MainActivity.kt @@ -2,10 +2,20 @@ package com.example.e01firstapplication import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.view.View +import android.widget.TextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } -} \ No newline at end of file + + // View class need to be used in parameter, even it is not used + fun buttonClicked(view: View) { + // activity_main layout has textView id in TextView element + // find it and change text + val textView = findViewById<TextView>(R.id.textView) + textView.text = resources.getString(R.string.button_clicked_txt) + } +} diff --git a/e01-first-application/app/src/main/res/layout/activity_main.xml b/e01-first-application/app/src/main/res/layout/activity_main.xml index 79ae699..e036337 100644 --- a/e01-first-application/app/src/main/res/layout/activity_main.xml +++ b/e01-first-application/app/src/main/res/layout/activity_main.xml @@ -1,6 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> -<androidx.constraintlayout.widget.ConstraintLayout - xmlns:android="http://schemas.android.com/apk/res/android" +<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" @@ -8,12 +7,25 @@ tools:context=".MainActivity"> <TextView + android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintLeft_toLeftOf="parent" - app:layout_constraintRight_toRightOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + <Button + android:id="@+id/button2" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginBottom="40dp" + android:onClick="buttonClicked" + android:text="@string/button_txt" + app:layout_constraintBottom_toTopOf="@+id/textView" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + tools:text="@string/button_txt" /> + </androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file diff --git a/e01-first-application/app/src/main/res/values-fi/strings.xml b/e01-first-application/app/src/main/res/values-fi/strings.xml new file mode 100644 index 0000000..f00a079 --- /dev/null +++ b/e01-first-application/app/src/main/res/values-fi/strings.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="app_name">First Application(fi)</string> + <string name="button_txt">Click me!(fi)</string> + <string name="button_clicked_txt">Button clicked!(fi)</string> +</resources> \ No newline at end of file diff --git a/e01-first-application/app/src/main/res/values/strings.xml b/e01-first-application/app/src/main/res/values/strings.xml index 458284c..81bec1e 100644 --- a/e01-first-application/app/src/main/res/values/strings.xml +++ b/e01-first-application/app/src/main/res/values/strings.xml @@ -1,3 +1,5 @@ <resources> - <string name="app_name">E01 First Application</string> + <string name="app_name">First Application</string> + <string name="button_txt">Click Me!</string> + <string name="button_clicked_txt">Button was clicked!</string> </resources> \ No newline at end of file -- GitLab