Skip to content

Part 1: Building the Android client

Sergi Almar edited this page Nov 20, 2013 · 10 revisions

Creating the project

The Android Chat client should be able to send and receive messages from a chat room.

  1. Start Android Studio and create a new project called MobosChat with the base package com.mobos.android
  2. Create a blank activity called ChatActivity

Defining the layout

The fragment will contain the layout for our chat application. Open the fragment_chat.xml (or the name you've given to out fragment layout) and define:

  1. A ViewList that will contain the chat messages
  2. An EditText to write the message we want to send to the chat room
  3. A Button to send the message

Run the app and see you get the desired layout

Adding RoboGuice

In order to use RoboGuice for the dependency injection, we need to add it as a project dependency. Open the gradle.build file and add the RoboGuice dependency under the dependencies element

compile 'org.roboguice:roboguice:3.0b-experimental'

Make sure RoboGuice appears in your External Libraries

Our Android components will need to extend RoboGuice specific classes to have DI in place.

  1. Edit your Fragment and extend from RoboFragment
  2. Edit you Activity and extend from RoboFragmentActivity

Injecting dependencies

In order to interact with the layout components, we'll be using the RoboGuice dependency injection

  1. Add a private field of type ListView and annotate it with @InjectView to get a reference of the declared list (ie: @InjectView(R.id.messageList) private ListView mMessageList;)
  2. Add a private field of type EditText and annotate it with @InjectView to get the reference of the declared text input.
  3. Add a private field of type Button and annotate it with @InjectView to get the reference of the declared button.

Clone this wiki locally