Adding a second Activity
Hopefully, nothing new here
We create a new DetailActivity.kt
and we add a companion object for the new intents.
It's always advisable to use the wizard for creating new activities (right click anywhere on your project panel and select New>Activity>Empty Activity). If you don't create the Activity from the wizard don't forget to add it to your Manifest.
companion object {
private val INTENT_CONTACT_AVATAR = "contact_avatar"
private val INTENT_CONTACT_NAME = "contact_name"
private val INTENT_CONTACT_PHONE = "contact_phone"
fun newIntent(context: Context, contact: Contact): Intent {
val intent = Intent(context, DetailActivity::class.java)
intent.putExtra(INTENT_CONTACT_AVATAR, contact.avatarId)
intent.putExtra(INTENT_CONTACT_NAME, contact.name)
intent.putExtra(INTENT_CONTACT_PHONE, contact.phone)
return intent
}
}
and we add some views to our XML layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".DetailActivity">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_avatar"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="52dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:civ_border_width="7dp"
app:civ_border_overlay="true"
app:civ_border_color="#FFFFFF"
tools:src="@tools:sample/avatars" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:textColor="@color/text_accent"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view"
tools:text="@tools:sample/full_names" />
<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@color/colorBg"
android:elevation="-1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline_horizontal25" />
<android.support.constraint.Guideline
android:id="@+id/guideline_horizontal25"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />
</android.support.constraint.ConstraintLayout>
In order to test it on a real device we need of course to start the activity first from our MainActivity, but we're going to explain this to the next step. If you want to test it anyway, the easiest way is to set it as a launcher activity from the Manifest instead of the MainActivity.
DON'T FORGET TO CHANGE IT BACK after you finish this step!
Last updated