Skip to content

Commit a4e24d4

Browse files
committed
Screenshots for "Show the messages"
1 parent 844fa4b commit a4e24d4

File tree

8 files changed

+14
-13
lines changed

8 files changed

+14
-13
lines changed

codelabs/chat/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Next we'll send data to Firebase! In this step we'll allow the user to enter a m
195195
mFirebaseRef.push().setValue(values);
196196
textEdit.setText("");
197197
}
198-
});
198+
});
199199

200200
You will have to import the packages for some of these classes. Android Studio will tell you where to import them from.
201201

@@ -212,15 +212,15 @@ Now that we can send messages to Firebase, it is time for the next step: making
212212

213213
## Show the (existing and new) messages
214214

215-
A chat app that doesn’t show existing messages is not very useful. So in this step we’ll add a list of the existing messages to our Android app. At the end of this section we’ll have a fully functional chat app.
215+
A chat app that doesn’t show existing messages is not very useful. So in this step we’ll add a list of the existing messages to our Android app. And since we're using Firebase, new chat messages will be added to this list automatically. At the end of this section we’ll have a fully functional chat app.
216216

217-
[screenshot:chat messages in list and new message]
217+
![Chat messages Android app and new message](images/5_1.png)
218218

219219
Let's take this in chunks: first we'll create a Java class to represent each message, then we'll create an Adapter that gets each of the messages from Firebase and puts them into a ListView.
220220

221221
1. As you can see in the screenshot, each chat message has the same layout. Instead of creating a custom layout, we'll use one of the built-in layouts of Android: `android.R.layout.two_line_list_item`. This layout displays the items like this:
222222

223-
[screenshot:two_line_list_item]
223+
![two_line_list_item](images/5_2.png)
224224

225225
We'll show the user name on the first line (in bold) and the message text on the second line.
226226
2. Now create a class `ChatMessage.java` that wraps the username and text message:
@@ -248,37 +248,38 @@ Let's take this in chunks: first we'll create a Java class to represent each mes
248248

249249
As you can see, this is plain-old Java object. But it’s a POJO with some special traits. First `ChatMessage` follows a JavaBean pattern for its property names. The `getName` method is a getter for a `name` property, while `getText()` is a getter for a `text` property. And second, those property names correspond to the ones we’ve been using when we sent messages to Firebase in our `OnClickListener`.
250250

251-
[screenshot:ChatMessage.java]
251+
![ChatMessage.java](images/5_3.png)
252252

253253
Warning: if you end up making this `ChatMessage` an inner class of another class, you must make it static: `public static class ChatMessage`.
254254

255255
3. With the layout for the message specified and their structure defined in a class, we need to make a space for them in the `main_activity.xml`
256256

257-
4. Add a ListView with `android:id="@android:id/list"`` above the LinearLayout:
257+
Add a ListView with `android:id="@android:id/list"`` above the LinearLayout:
258258

259259
<ListView
260260
android:id="@android:id/list"
261261
android:layout_width="match_parent"
262262
android:layout_height="match_parent"
263263
android:layout_above="@+id/footer"/>
264264

265-
This is the container that all messages will be added to: one message_layout for each ChatMessage.
265+
This is the container that all messages will be added to: one message_layout for each ChatMessage.
266266

267-
[screenshot:activity_main.xml]
267+
![activity_main.xml with ListView](images/5_4.png)
268268

269-
The `id` value is very important here, since Android's `ListActivity` uses it to find the `ListView`. So make sure to enter it exactly as specified: ``@android:id/list`.
269+
The `id` value is very important here, since Android's `ListActivity` uses it to find the `ListView`. So make sure to enter it exactly as specified: ``@android:id/list`.
270270

271271
4. Make the `MainActivity` class descend from `ListActivity`. This is a built-in Android base-class. By deriving from this, our activity will automatically have access to the ListView we added to the layout:
272272

273273
public class MainActivity extends ListActivity {
274274

275-
[screenshot:MainActivity descends from ListActivity]
276-
277275
5. We're ready to start on our ListAdapter, which we'll base on the `FirebaseListAdapter` from the firebase-ui project we imported. `The FirebaseListAdapter` class adapts a Firebase collection so that it becomes usable in an Android `ListView`. First we'll add a member to our `MainActivity`:
278276

279277
public class MainActivity extends ListActivity {
278+
private Firebase mFirebaseRef;
280279
FirebaseListAdapter<ChatMessage> mListAdapter;
281280

281+
![MainActivity extends ListActivity](images/5_5.png)
282+
282283
6. To make everything come together, we add this to the onCreate method of our MainActivity:
283284

284285
mListAdapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class,
@@ -293,7 +294,7 @@ Let's take this in chunks: first we'll create a Java class to represent each mes
293294

294295
The FirebaseListAdapter maps the data from your Firebase database into the ListView that you added to the layout. It creates a new instance of your `two_line_list_item` for each `ChatMessage` and calls the `populateView method`. We override this method and put the name and text in the correct subviews.
295296

296-
[screenshot:MainActivity code]
297+
![MainActivity code](images/5_6.png)
297298

298299
7. Don't worry, the hardest part is behind us now. All that is left in this step is some clean-up. But before that, run your app and see that it shows all existing messages. And if you send a new message, it shows up in the emulator and in your Firebase dashboard.
299300

@@ -317,7 +318,7 @@ Let's take this in chunks: first we'll create a Java class to represent each mes
317318
mListAdapter.cleanup();
318319
}
319320

320-
[screenshot: showing OnClickListener and onDestroy]
321+
![MainActivity showing OnClickListener and onDestroy](images/5_7.png)
321322

322323
In this section we made our app show the chat messages. It was a lot of work, but in the end you can see that the Java code for our main activity still fits in a single screenshot.
323324

codelabs/chat/images/5_1.png

49.7 KB
Loading

codelabs/chat/images/5_2.png

69.9 KB
Loading

codelabs/chat/images/5_3.png

180 KB
Loading

codelabs/chat/images/5_4.png

261 KB
Loading

codelabs/chat/images/5_5.png

257 KB
Loading

codelabs/chat/images/5_6.png

264 KB
Loading

codelabs/chat/images/5_7.png

264 KB
Loading

0 commit comments

Comments
 (0)