Skip to content

Part 1: Building the Android client

Sergi Almar edited this page Nov 22, 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 ListView that will contain the chat messages (hint: android:stackFromBottom="true" android:transcriptMode="alwaysScroll" may help you)
  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.
  4. Use the class-level @ContentView annotation to set the layout out the ChatActivity

Adding the logic

Injection happens during the onViewCreated method in your fragment, in order to make sure the dependencies have already been injected we'll be interacting with them in that point.

  1. Override the onViewCreated method:
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
  1. In order to interact with the List element, we need to provide an adapter. Let's add a simple ArrayAdapter of Strings for now:
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1);
    mMessageList.setAdapter(adapter);
  1. When we press the button we want to add the message to the list (we'll later send it over WebSockets, but let's keep it simple for now). Add a listener:
mMessageButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // Add the text from the EditText to the adapter and clear the EditText
        // Don't forget to notify the adapter of the data change!
    }
});

Injecting a System Service

Let's now make our phone vibrate when we send a message.

  1. Add the Vibrate permission in your AndroidManifest.xml <uses-permission android:name="android.permission.VIBRATE" />
  2. Add a private field of type Vibrator in your fragment and annotate it with the JSR-330 @Inject annotation.
  3. Add the logic to vibrate for 1000 milliseconds when sending the message