|
| 1 | +--- |
| 2 | +title: Add UI Code |
| 3 | +weight: 4 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## Add Layouts |
| 10 | +Currently the `activity_main.xml` layout file in your `app\src\res\layout` directory is nearly empty, containing just a "Hello World!" piece of text that we wish to remove. Instead, replace it with the following, which will create a status area at the top, a place for messages in the middle, and a place for you to type at the bottom with a button to send: |
| 11 | +```xml |
| 12 | +<?xml version="1.0" encoding="utf-8"?> |
| 13 | +<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| 14 | + xmlns:app="http://schemas.android.com/apk/res-auto" |
| 15 | + android:id="@+id/root" |
| 16 | + android:layout_width="match_parent" |
| 17 | + android:layout_height="match_parent"> |
| 18 | + |
| 19 | + <TextView |
| 20 | + android:id="@+id/status_text" |
| 21 | + android:layout_width="0dp" |
| 22 | + android:layout_height="wrap_content" |
| 23 | + android:layout_marginStart="16dp" |
| 24 | + android:layout_marginTop="16dp" |
| 25 | + android:layout_marginEnd="16dp" |
| 26 | + android:text="Loading model..." |
| 27 | + android:textAppearance="@style/TextAppearance.MaterialComponents.Body1" |
| 28 | + app:layout_constraintEnd_toEndOf="parent" |
| 29 | + app:layout_constraintStart_toStartOf="parent" |
| 30 | + app:layout_constraintTop_toTopOf="parent" /> |
| 31 | + |
| 32 | + <androidx.recyclerview.widget.RecyclerView |
| 33 | + android:id="@+id/messages" |
| 34 | + android:layout_width="0dp" |
| 35 | + android:layout_height="0dp" |
| 36 | + android:layout_marginStart="16dp" |
| 37 | + android:layout_marginTop="12dp" |
| 38 | + android:layout_marginEnd="16dp" |
| 39 | + android:clipToPadding="false" |
| 40 | + android:paddingBottom="8dp" |
| 41 | + app:layout_constraintBottom_toTopOf="@+id/input_row" |
| 42 | + app:layout_constraintEnd_toEndOf="parent" |
| 43 | + app:layout_constraintStart_toStartOf="parent" |
| 44 | + app:layout_constraintTop_toBottomOf="@+id/status_text" /> |
| 45 | + |
| 46 | + <LinearLayout |
| 47 | + android:id="@+id/input_row" |
| 48 | + android:layout_width="0dp" |
| 49 | + android:layout_height="wrap_content" |
| 50 | + android:layout_margin="16dp" |
| 51 | + android:orientation="horizontal" |
| 52 | + app:layout_constraintBottom_toBottomOf="parent" |
| 53 | + app:layout_constraintEnd_toEndOf="parent" |
| 54 | + app:layout_constraintStart_toStartOf="parent"> |
| 55 | + <EditText |
| 56 | + android:id="@+id/user_input" |
| 57 | + android:layout_width="0dp" |
| 58 | + android:layout_height="wrap_content" |
| 59 | + android:layout_weight="1" |
| 60 | + android:enabled="false" |
| 61 | + android:hint="Model is loading..." |
| 62 | + android:inputType="textMultiLine" |
| 63 | + android:maxLines="4" |
| 64 | + android:minHeight="48dp" |
| 65 | + android:padding="12dp" /> |
| 66 | + <com.google.android.material.button.MaterialButton |
| 67 | + android:id="@+id/send_button" |
| 68 | + android:layout_width="wrap_content" |
| 69 | + android:layout_height="wrap_content" |
| 70 | + android:layout_marginStart="12dp" |
| 71 | + android:enabled="false" |
| 72 | + android:text="Import model" /> |
| 73 | + </LinearLayout> |
| 74 | +</androidx.constraintlayout.widget.ConstraintLayout> |
| 75 | +``` |
| 76 | + |
| 77 | +We also need two small "helper" layouts to format the messages from the user and the AI assistant. In the `layout` folder alongside the main layout, first create `item_message_user.xml` and insert the following: |
| 78 | +```xml |
| 79 | +<?xml version="1.0" encoding="utf-8"?> |
| 80 | +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| 81 | + android:layout_width="match_parent" |
| 82 | + android:layout_height="wrap_content" |
| 83 | + android:paddingTop="6dp" |
| 84 | + android:paddingBottom="6dp"> |
| 85 | + |
| 86 | + <TextView |
| 87 | + android:id="@+id/msg_content" |
| 88 | + android:layout_width="wrap_content" |
| 89 | + android:layout_height="wrap_content" |
| 90 | + android:layout_gravity="end" |
| 91 | + android:background="#D7F0FF" |
| 92 | + android:padding="12dp" |
| 93 | + android:textAppearance="@style/TextAppearance.MaterialComponents.Body1" /> |
| 94 | +</FrameLayout> |
| 95 | +``` |
| 96 | + |
| 97 | +Then create `item_message_assistant.xml` and put the following as its contents: |
| 98 | +```xml |
| 99 | +<?xml version="1.0" encoding="utf-8"?> |
| 100 | +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| 101 | + android:layout_width="match_parent" |
| 102 | + android:layout_height="wrap_content" |
| 103 | + android:paddingTop="6dp" |
| 104 | + android:paddingBottom="6dp"> |
| 105 | + |
| 106 | + <TextView |
| 107 | + android:id="@+id/msg_content" |
| 108 | + android:layout_width="wrap_content" |
| 109 | + android:layout_height="wrap_content" |
| 110 | + android:layout_gravity="start" |
| 111 | + android:background="#EFEFEF" |
| 112 | + android:padding="12dp" |
| 113 | + android:textAppearance="@style/TextAppearance.MaterialComponents.Body1" /> |
| 114 | +</FrameLayout> |
| 115 | +``` |
| 116 | + |
| 117 | +## Add the MessageAdaptor class |
| 118 | +As a final bit of UI code, we add a `MessageAdapter.kt` code file to put our messages into the correct bit of layout. The file should sit alongside the `MainActivity` class that is auto-created with the project. |
| 119 | + |
| 120 | +The file contents of `MessageAdapter.kt` are: |
| 121 | +```kotlin |
| 122 | +package com.example.simpleaichat |
| 123 | + |
| 124 | +import android.view.LayoutInflater |
| 125 | +import android.view.View |
| 126 | +import android.view.ViewGroup |
| 127 | +import android.widget.TextView |
| 128 | +import androidx.recyclerview.widget.RecyclerView |
| 129 | + |
| 130 | +data class Message( |
| 131 | + val id: String, |
| 132 | + val content: String, |
| 133 | + val isUser: Boolean |
| 134 | +) |
| 135 | + |
| 136 | +class MessageAdapter( |
| 137 | + private val messages: List<Message> |
| 138 | +) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { |
| 139 | + |
| 140 | + override fun getItemViewType(position: Int): Int { |
| 141 | + return if (messages[position].isUser) VIEW_TYPE_USER else VIEW_TYPE_ASSISTANT |
| 142 | + } |
| 143 | + |
| 144 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { |
| 145 | + val inflater = LayoutInflater.from(parent.context) |
| 146 | + return if (viewType == VIEW_TYPE_USER) { |
| 147 | + UserMessageViewHolder(inflater.inflate(R.layout.item_message_user, parent, false)) |
| 148 | + } else { |
| 149 | + AssistantMessageViewHolder(inflater.inflate(R.layout.item_message_assistant, parent, false)) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { |
| 154 | + holder.itemView.findViewById<TextView>(R.id.msg_content).text = messages[position].content |
| 155 | + } |
| 156 | + |
| 157 | + override fun getItemCount(): Int = messages.size |
| 158 | + |
| 159 | + class UserMessageViewHolder(view: View) : RecyclerView.ViewHolder(view) |
| 160 | + class AssistantMessageViewHolder(view: View) : RecyclerView.ViewHolder(view) |
| 161 | + |
| 162 | + companion object { |
| 163 | + private const val VIEW_TYPE_USER = 1 |
| 164 | + private const val VIEW_TYPE_ASSISTANT = 2 |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +Quickly going through the important parts: |
| 170 | +- `Message` is the smallest useful chat piece: one id, the message text, and a flag saying whether it came from the user or the assistant. |
| 171 | +- `getItemViewType(...)` decides which row layout to use for each message. |
| 172 | +- `onCreateViewHolder(...)` inflates either the user bubble or the assistant bubble layout. |
| 173 | +- `onBindViewHolder(...)` writes the current message text into the row. |
| 174 | +- `getItemCount()` tells the `RecyclerView` how many chat rows it should render. |
0 commit comments