Android Open Source - passby Main Activity

From Project

Back to project page passby.


The source code is released under:

Copyright 2014 Firebase, Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ?Software??...

If you think the Android project passby listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//#CSIT6000B#  Rio He       20227977
//#CSIT6000B#  Cedric Liang 20220395
//#CSIT6000B#  Kevin Wang   20225577 
package com.firebase.androidchat;
/*from  w  w w  .  j a va  2  s. c o m*/
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.*;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.ValueEventListener;

public class MainActivity extends ListActivity {

    // database URL
    private static final String FIREBASE_URL = "";

    private String username, chatRoom;
    private Firebase ref;
    private ValueEventListener connectedListener;
    private ChatListAdapter chatListAdapter;

    protected void onCreate(Bundle savedInstanceState) {

        this.username = getIntent().getStringExtra("nickname");
        this.chatRoom = getIntent().getStringExtra("chatRoom");

        setTitle("Chatting as " + this.username);

        // Setup our database ref
        ref = new Firebase(FIREBASE_URL).child(this.chatRoom);

        // Setup our input methods. Enter key on the keyboard or pushing the send button
        EditText inputText = (EditText)findViewById(;
        inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_NULL && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                return true;

        findViewById( View.OnClickListener() {
            public void onClick(View view) {


    public void onStart() {
        // Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
        final ListView listView = getListView();
        chatListAdapter = new ChatListAdapter(ref.limit(20), this, R.layout.chat_message, username);
        chatListAdapter.registerDataSetObserver(new DataSetObserver() {
            public void onChanged() {
                listView.setSelection(chatListAdapter.getCount() - 1);

        // A little indication of connection status
        connectedListener = ref.getRoot().child(".info/connected").addValueEventListener(new ValueEventListener() {
            public void onDataChange(DataSnapshot dataSnapshot) {
                boolean connected = (Boolean)dataSnapshot.getValue();
                if (connected) {
                    Toast.makeText(MainActivity.this, "Connected to Firebase", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Disconnected from Firebase", Toast.LENGTH_SHORT).show();

            public void onCancelled() {
                // No-op

    public void onStop() {

    private void sendMessage() {
        EditText inputText = (EditText)findViewById(;
        String input = inputText.getText().toString();
        if (!input.equals("")) {
            // Create our 'model', a Chat object
            Chat chat = new Chat(input, username);
            // Create a new, auto-generated child of that chat location, and save our chat data there

Java Source Code List