Mobile App Dev 2021W: Tutorial 9
This tutorial is due on April 8, 2021 via cuLearn. Please use this template for your solutions. Be sure to download the code for IntentDemo.
Questions
- Where in the application is the following behaviour specified?
- The size of the red circle
- The colour of the red circle.
- No application title on the screens.
- The application should start with showing the splash screen.
- The splash screen layout in splashscreen.xml is associated with the code in SplashScreen.kt
- Clicking on continue on the splash screen should cause MainActivity to be run.
- The layout of the application changes when you switch the device to dark/night mode, beyond different colors. Why?
- Where does the application send an explicit intent? Where does it receive an explicit intent?
- Where does the application send an implicit intent? Where does it receive an implicit intent?
- What touch events does MainActivity respond to? What code implements the response for them?
- Why are the constants added to circle.x and circle.y in TrackCircle's onTouch handler (lines 40 and 41)? How does the behavior of the app change if these constants are removed? Why?
- TrackCircle's onTouch method does the same thing in response to an ACTION_MOVE and an ACTION_UP event. Are both of these needed? What happens if either is removed?
Code
MainActivity.kt
package carleton.comp1601.intentdemo
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import android.content.Intent
import android.net.Uri
import android.view.MotionEvent
val appName = "IntentDemo"
class MainActivity : AppCompatActivity() {
private lateinit var circle: View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
circle = findViewById(R.id.circle)
circle.setOnTouchListener(trackCircle)
Log.d(appName, "Main screen created")
}
fun openPage(v: View) {
Log.d(appName, "Opening URL")
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://homeostasis.scs.carleton.ca/wiki"))
startActivity(intent)
Log.d(appName, "URL intent sent")
}
val trackCircle = object: View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
if (event == null || v == null) {
return false
}
val action = event.getAction()
val x = event.rawX - 128F
val y = event.rawY - 128F
if (action == MotionEvent.ACTION_DOWN) {
Log.d(appName, "Circle touched at ${x}, ${y}")
} else if (action == MotionEvent.ACTION_UP) {
circle.x = x
circle.y = y
Log.d(appName, "Circle released at ${x}, ${y}")
} else if (action == MotionEvent.ACTION_MOVE) {
circle.x = x
circle.y = y
Log.d(appName, "Circle repositioned to ${x}, ${y}")
}
return true;
}
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/circle"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/circle"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openPage"
android:text="Open Page"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.944" />
</androidx.constraintlayout.widget.ConstraintLayout>
SplashScreen.kt
package carleton.comp1601.intentdemo
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class SplashScreen : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.splashscreen)
Log.d(appName, "Splash screen created")
}
fun startMain(v: View) {
Log.d(appName, "Starting Main button pressed")
intent = Intent(this, MainActivity::class.java)
startActivity(intent)
Log.d(appName, "Starting Main intent sent")
}
}
splashscreen.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:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="startMain"
tools:context=".SplashScreen">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Intent & Drag Demo"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Display3"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COMP 1601"
android:textAppearance="@style/TextAppearance.AppCompat.Display2"
app:layout_constraintBottom_toTopOf="@+id/continueButton"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/continueButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:onClick="startMain"
android:text="Continue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="carleton.comp1601.intentdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.IntentDemo">
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" android:exported="true">
</activity>
</application>
</manifest>
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000"/>
</shape>
theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.IntentDemo" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>