Testing your Code
At this point, your MainActivity.java file should look something like this (with a few adjustments):
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private Session session;
private SignalMessageAdapter messageHistory;
private EditText messageEditTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageEditTextView = findViewById(R.id.message_edit_text);
ListView messageHistoryListView = findViewById(R.id.message_history_list_view);
// Attach data source to message history
messageHistory = new SignalMessageAdapter(this);
messageHistoryListView.setAdapter(messageHistory);
messageEditTextView.setEnabled(false);
session = new Session.Builder(this, VonageVideoSDKConfig.APP_ID, VonageVideoSDKConfig.SESSION_ID).build();
session.setSessionListener(sessionListener);
session.setSignalListener(signalListener);
session.connect(VonageVideoSDKConfig.TOKEN);
messageEditTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager inputMethodManager = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
sendMessage();
return true;
}
return false;
}
});
}
private void sendMessage() {
Log.d(TAG, "Send Message");
String data = messageEditTextView.getText().toString();
session.sendSignal("msg", data);
SignalMessage message = new SignalMessage(data);
messageHistory.add(message);
messageEditTextView.setText("");
}
private final Session.SessionListener sessionListener = new Session.SessionListener() {
@Override
public void onConnected(Session session) {
messageEditTextView.setEnabled(true);
}
@Override
public void onDisconnected(Session session) {
messageEditTextView.setEnabled(false);
}
@Override
public void onError(Session session, OpentokError opentokError) {
Log.i(TAG,"Session error: " + opentokError.getMessage());
}
@Override
public void onStreamReceived(Session session, Stream stream) {}
@Override
public void onStreamDropped(Session session, Stream stream) {}
};
private final Session.SignalListener signalListener = new Session.SignalListener() {
@Override
public void onSignalReceived(Session session, String type, String data, Connection connection) {
if (!connection.equals(session.getConnection()) && (type != null && type.equals("msg"))) {
SignalMessage message = new SignalMessage(data, true);
messageHistory.add(message);
}
}
};
}
In your completed code, you should have hard coded values to replace YOUR_APP_ID, YOUR_SESSION_ID and YOUR_TOKEN in the VonageVideoSDKConfig class — if you haven't done this, see Setting up authentication above.
- Build and run the app in Android Studio.
- Enter some text into the text field and send it.
- You should see the message get added to the message history.
Next, we can test what it looks like if someone else sends a message. We can simulate that by running the app again on a different emulator/device:
- Build and run the app on a different emulator.
- Enter some text into the textfield and send it on both emulators.
- You should see a both messages get added to the message history.
Troubleshooting tips: If you cant connect to the session, check the Android Studio Logcat window for errors. The most likely issue is that your API key, session ID, or token is not set up properly. Since you hard coded your credentials, it's also possible that your token has expired. If you get a build error related to the compile version, you can change it in the app level build.grade file.
Basic text chat
Follow this tutorial to build basic text chat from scratch using the Vonage Video API. It is the quickest way to build a proof of concept for this functionality on the video platform.