Android Real Time Chat Application Using Firebase Tutorial

In this tutorial you will learn to build an android real time chat application using firebase database.

Before reading this tutorial I hope you are already familiar with basic read and write operations on firebase. If you don’t know then I would recommend you to follow below link to get basic overview about firebase.

Read: https://firebase.google.com/docs/database/android/start/

I have developed a very basic and simple chat app in which user can login and register in the system and can do one to one chat with other users.

Android Real Time Chat Application Using Firebase Tutorial 1

Video Demo

Watch below video to see how the app works.

Android Real Time Chat Application Using Firebase Tutorial

Firebase

Go to firebase console and create a new project.

Link: https://console.firebase.google.com/

Select Database option in left sidebar. The database structure used in this project looks as shown in below image.

Android Real Time Chat Application Using Firebase Tutorial 2

In my case the database base url is https://android-chat-app-e711d.firebaseio.com/. It will be different in your case so make sure to change the url wherever used in the code.

You can access the child in form of json data by just adding .json at the end of parent url. For example in this app I have accessed users data by url https://android-chat-app-e711d.firebaseio.com/users.json.

Read more about it here https://firebase.google.com/docs/reference/rest/database/

Security Rules

By default only authenticated user can access the database. When you will go to Rules tab in firebase console project then it may look like this.

When you try to run the app you may get authentication failure error.

So change rules to as shown below.

Note: Above security rules will allow anyone to read, write and edit data. That means any user can edit data of any other user. This must not be allowed. So use this security rule only for testing purpose. Don’t use it if you are making the app live in market. To learn more about firebase security rules visit below link.

https://firebase.google.com/docs/database/security/

Android Studio

Create a new android studio project with package name com.androidchatapp

Now add dependency for firebase database and volley in build.gradle (Module: app) file. Add following lines under dependency section and sync the project.

Add internet access permission in AndroidManifest.xml file.

The android studio project has following structure.

Android Real Time Chat Application Using Firebase Tutorial 3

Add following code in respective files.

AndroidManifest.xml

rounded_corner1.xml

rounded_corner2.xml

activity_chat.xml

activity_login.xml

activity_register.xml

activity_users.xml

message_area.xml

Chat.java

Login.java

Register.java

UserDetails.java

Users.java

 

Screenshots

Android Real Time Chat Application Using Firebase Tutorial 4 Android Real Time Chat Application Using Firebase Tutorial 5

Android Real Time Chat Application Using Firebase Tutorial 6 Android Real Time Chat Application Using Firebase Tutorial 7

You can download the apk and source code from below link.

Apk: http://www.mediafire.com/file/jwurkc69pag2u6w/Android+Chat+App_V1.0.apk

Source Code: http://www.mediafire.com/file/djl5s58sbw5kpma/AndroidChatApp.rar 

Feel free to ask your queries by commenting below.

31 thoughts on “Android Real Time Chat Application Using Firebase Tutorial

  1. nisha

    I want your help for my final year projetc…and the question is how to fetch active process in android phone to database

    Reply
  2. Devesh

    registered but cant log in….. user not found error

    W/EGL_emulation: eglSurfaceAttrib not implemented
    W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9e453dc0, error=EGL_SUCCESS
    E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa1932470
    W/EGL_emulation: eglSurfaceAttrib not implemented
    W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9e453dc0, error=EGL_SUCCESS
    E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa19309c0
    V/FA: Activity paused, time: 1284458
    V/FA: onActivityCreated
    V/FA: Activity resumed, time: 1284528
    W/EGL_emulation: eglSurfaceAttrib not implemented
    W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9e453f20, error=EGL_SUCCESS
    E/Surface: getSlotFromBufferLocked: unknown buffer: 0x9fdf8070
    D/InputEventConsistencyVerifier: KeyEvent: ACTION_UP but key was not down.
    in android.support.v7.widget.AppCompatEditText{26e9ba7 VFED..CL. .F….ID 42,746-1038,864 #7f0b005e app:id/password}
    0: sent at 1291504000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=1291504, downTime=1291504, deviceId=0, source=0x101 }
    W/EGL_emulation: eglSurfaceAttrib not implemented
    W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9ec14de0, error=EGL_SUCCESS
    E/Surface: getSlotFromBufferLocked: unknown buffer: 0x9fdf8230
    W/EGL_emulation: eglSurfaceAttrib not implemented
    W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9ec14de0, error=EGL_SUCCESS
    E/Surface: getSlotFromBufferLocked: unknown buffer: 0x9fdf8230

    Reply
  3. mahmoud

    hello,
    My application database in firebase does not look like yours i have only one column with the app name because of this am not able to signin, how to change it to look like users with messages and users column?

    Reply
    1. Admin Post author

      You can easily add more columns under it. Just make all the columns that I have shown in the image.

      Reply
  4. Arjun

    Could you please solve this error while running the project. The error is shown below

    Error:Execution failed for task ‘:app:transformResourcesWithMergeJavaResForDebug’.
    > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
    File1: C:\Users\Arjun\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.2.2\3c8f6018eaa72d43b261181e801e6f8676c16ef6\jackson-databind-2.2.2.jar
    File2: C:\Users\Arjun\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.2.2\285cb9c666f0f0f3dd8a1be04e1f457eb7b15113\jackson-annotations-2.2.2.jar
    File3: C:\Users\Arjun\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.2.2\d20be6a5ddd6f8cfd36ebf6dea329873a1c41f1b\jackson-core-2.2.2.jar

    Reply
  5. Arjun

    Am facing the error like

    “Error:Failed to capture snapshot of output files for task ‘transformClassesWithDexForDebug’ during up-to-date check.”

    Reply
  6. Adnan Quraishi

    hello neeraj Your code works great , I appreciate you for this nice tutorial , it working perfect with me , but I need your help , I am trying to upload camera pictures to firebase database in base64 string format and other user can get that string and decode it to get to its bitmap ,

    My question is how we will add the image thumbnail in the messagebox ? and only add the image thumbnail when user selects the image and confirms to upload .

    I tried some codes but either empty imageview is sent with every message , or not shown.
    can you provide any simple method which we provide image bitmap and it adds the image view in messagebox and starts uploading , and other peer can receive the thumbnail in messagebox and downloads it . any help would be much appreciated , thanks

    Reply
    1. Admin Post author

      1. First capture image using camera, follow this tutorial http://www.viralandroid.com/2015/12/how-to-capture-image-from-android-camera-and-display-it-programmatically.html

      2. Now show a dialog, if user confirms then add the image dynamically to the chat box, follow this tutorial http://stackoverflow.com/questions/14002996/add-imageview-dynamically-to-view

      3. Convert the bitmap image to base64 and send it to firebase to save it, for converting bitmap to image follow this tutorial http://www.thecrazyprogrammer.com/2016/10/android-convert-image-base64-string-base64-string-image.html

      4. Follow the reverse process to get image at receiver end.

      I hope this will help.

      Reply
      1. Adnan Quraishi

        This works thanks neeraj i have succesfully achieved it . your tutorial is really helpful for learning firebase . Thanks a lot.

        Reply
  7. Adnan Quraishi

    How to add ImageView in messagebox for uploading and other peer for downloading ? while using base64 to encode image and storing to firebase databse?

    Reply
  8. Adnan Quraishi

    Neeraj if want to block certain user , how can we achieve that . i think a flag should be put in firebase database and on the basis of flag we can verify the user is blocked or not and then display other to list . or is there any other better solution for this in your mind . kindly do share here . Thanks

    Reply
  9. Geethanjali

    I would like to know how to delete a particular message in a chat. And also how to clear or delete chat.

    Reply
  10. soumya

    nice tutorial.I have two questions.
    1> how do i create my own user.json file
    2> how do i push that message

    Reply
  11. bayuna

    that the string url “String url = “https://android-chat-app-e711d.firebaseio.com/users.json”;” etc, use my firebase url right??

    Reply
  12. Atul kumar

    String url = “https://chatapplication-28729.firebaseio.com/users.json”;
    I use above url for my project but i get this IO Exception

    In Adroid version 18
    com.android.volley.NoConnectionError: java.io.IOException: No authentication challenges found .

    In Adroid version 22
    E/Volley: [5512] BasicNetwork.performRequest: Unexpected response code 401 for https://chatapplication-28729.firebaseio.com/users.json

    System.out: com.android.volley.AuthFailureError

    Reply
    1. Admin Post author

      By default authentication is required to use firebase database. When you will go to Rules section in your firebase console project, it may look as shown below.
      {
      "rules": {
      ".read": "auth != null",
      ".write": "auth != null"
      }
      }

      Just change it to.

      {
      "rules": {
      ".read": true,
      ".write": true
      }
      }

      This rule is for development purpose only. If you keep these rules when your chat is live then anyone can read, write and edit data, and that is not good. A user should be allowed to edit its own chat messages only. Learn more about rules at below link.
      https://firebase.google.com/docs/database/security/

      Reply
  13. Saalem

    Can u plz help me with the below errors

    Error:(30, 17) No resource found that matches the given name (at ‘layout’ with value ‘@layout/message_area’).
    Error:(30, 17) No resource found that matches the given name (at ‘layout’ with value ‘@layout/message_area’).
    C:\Users\Acer\AndroidStudioProjects\AndroidChatApp\app\src\main\res\layout\activity_chat.xml
    C:\Users\Acer\AndroidStudioProjects\AndroidChatApp\app\build\intermediates\res\merged\debug\layout\activity_chat.xml
    Error:Execution failed for task ‘:app:processDebugResources’.
    > com.android.ide.common.process.ProcessException: Failed to execute aapt

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *