Mobile App Dev 2021W: Tutorial 9
This tutorial is still in developoment.
Questions
- Where in the application is the following behaviour specified?
- The size of the red circle
- No application title on the screens.
- The layout of the application change when you switch the device to dark/night mode, beyond different colors. Why?
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>