-
Notifications
You must be signed in to change notification settings - Fork 16
Part 1: Building the Android client
The Android Chat client should be able to send and receive messages from a chat room.
- Start Android Studio and create a new project called
MobosChatwith the base packagecom.mobos.android - Create a blank activity called
ChatActivity
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:
- A
ListViewthat will contain the chat messages (hint:android:stackFromBottom="true" android:transcriptMode="alwaysScroll"may help you) - An
EditTextto write the message we want to send to the chat room - A
Buttonto send the message
Run the app and see you get the desired layout
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.
- Edit your
Fragmentand extend fromRoboFragment - Edit you
Activityand extend fromRoboFragmentActivity
In order to interact with the layout components, we'll be using the RoboGuice dependency injection
- 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;) - Add a private field of type EditText and annotate it with @InjectView to get the reference of the declared text input.
- Add a private field of type Button and annotate it with @InjectView to get the reference of the declared button.
- Use the class-level @ContentView annotation to set the layout out the
ChatActivity
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.
- Override the
onViewCreatedmethod:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}- 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);- 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!
}
});Let's now make our phone vibrate when we send a message.
- Add the Vibrate permission in your
AndroidManifest.xml<uses-permission android:name="android.permission.VIBRATE" /> - Add a private field of type
Vibratorin your fragment and annotate it with the JSR-330@Injectannotation. - Add the logic to vibrate for 1000 milliseconds when sending the message