Mobile App Development 2021W Lecture 15: Difference between revisions

From Soma-notes
Created page with "==Video== Video for the lecture given on March 10, 2021 [https://homeostasis.scs.carleton.ca/~soma/mad-2021w/lectures/comp1601-2021w-lec15-20210310.m4v is now available]."
 
No edit summary
 
Line 2: Line 2:


Video for the lecture given on March 10, 2021 [https://homeostasis.scs.carleton.ca/~soma/mad-2021w/lectures/comp1601-2021w-lec15-20210310.m4v is now available].
Video for the lecture given on March 10, 2021 [https://homeostasis.scs.carleton.ca/~soma/mad-2021w/lectures/comp1601-2021w-lec15-20210310.m4v is now available].
==Code==
[https://homeostasis.scs.carleton.ca/~soma/mad-2021w/code/Mar10.zip Mar10.zip]
===MainActivity,kt===
<syntaxhighlight lang="kotlin" line>
package carleton.comp1601.mar10
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    private lateinit var myMessage: TextView
    private var count = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        myMessage = findViewById(R.id.myMessage)
    }
    fun myButtonPressed(v: View) {
        count++
        myMessage.text = "You clicked $count times."
    }
}
</syntaxhighlight>
===activity_main.xml===
<syntaxhighlight lang="xml" line>
<?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:id="@+id/Mar10"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="myButtonPressed"
        android:text="Tap me!"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintBottom_toTopOf="@+id/myMessage"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/myMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You haven't tapped yet."
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</syntaxhighlight>

Latest revision as of 23:48, 15 March 2021

Video

Video for the lecture given on March 10, 2021 is now available.

Code

Mar10.zip

MainActivity,kt

package carleton.comp1601.mar10

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    private lateinit var myMessage: TextView
    private var count = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        myMessage = findViewById(R.id.myMessage)
    }

    fun myButtonPressed(v: View) {
        count++
        myMessage.text = "You clicked $count times."
    }
}

activity_main.xml

<?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:id="@+id/Mar10"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="myButtonPressed"
        android:text="Tap me!"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintBottom_toTopOf="@+id/myMessage"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/myMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You haven't tapped yet."
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>