Skip to content
Snippets Groups Projects
Unverified Commit b7910cd7 authored by AC5636's avatar AC5636 :ghost:
Browse files

Add functionality for button click

parent 14b082d3
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
}
<?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
<?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
<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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment