Android Chat Application Login Register Form

Android Chat Application Login Registration Form – In this tutorial we learn about android chat application like facebook, whatsapp and some messaging chat application. Now a days most of college students care about their career projects so some of students choose android platform to build their projects and career. In android have lots of facilities and security features available.

Here we see how to create android chat application with login and new user registration form with every users like facebook & whatsapp social networking application. We used Firebase to store the user information data (like messages, usernames etc). Actually chat application is very essential for software developers and college students.

Create New Project

Let’s start our project to make android chat application using firebase database. As usual first select one activity after that give some name for your project now the project is created successfully. Before write java class files, you have to create firebase account for store the user information and messages.

We need to must create firebase account and access the API permission for interact with java & xml file layouts. In below i give the firebase link for create new firebase project for your android chat application. For that we need separate server for storing files or go for PHP, android studio platforms.

Create Firbase Project

First we need firebase account to create android chat application. So just follow below steps to make  it, Go to on the firebase official website https://firebase.google.com/ to create new project on the Firebase database.

After create project need to integrate JSON file with java classes. When user give the information for authenticate user whether correct or not, that information stored from Firebase database. So the user get the message information if its correct.

Android Chat Application

After complete the firebase setup, open our java project and open the default file of MainActivity.java class and add the following below code,

package com.androidchatapp;
 
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
 
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class Users extends AppCompatActivity {
    ListView usersList;
    TextView noUsersText;
    ArrayList al = new ArrayList<>();
    int totalUsers = 0;
    ProgressDialog pd;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_users);
 
        usersList = (ListView)findViewById(R.id.usersList);
        noUsersText = (TextView)findViewById(R.id.noUsersText);
 
        pd = new ProgressDialog(Users.this);
        pd.setMessage("Loading...");
        pd.show();
 
        String url = "https://androidchatapp-76776.firebaseio.com/users.json";
 
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener(){
            @Override
            public void onResponse(String s) {
                doOnSuccess(s);
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("" + volleyError);
            }
        });
 
        RequestQueue rQueue = Volley.newRequestQueue(Users.this);
        rQueue.add(request);
 
        usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                UserDetails.chatWith = al.get(position);
                startActivity(new Intent(Users.this, Chat.class));
            }
        });
    }
 
    public void doOnSuccess(String s){
        try {
            JSONObject obj = new JSONObject(s);
 
            Iterator i = obj.keys();
            String key = "";
 
            while(i.hasNext()){
                key = i.next().toString();
 
                if(!key.equals(UserDetails.username)) {
                    al.add(key);
                }
 
                totalUsers++;
            }
 
        } catch (JSONException e) {
            e.printStackTrace();
        }
 
        if(totalUsers <=1){
            noUsersText.setVisibility(View.VISIBLE);
            usersList.setVisibility(View.GONE);
        }
        else{
            noUsersText.setVisibility(View.GONE);
            usersList.setVisibility(View.VISIBLE);
            usersList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, al));
        }
 
        pd.dismiss();
    }
}

After that create another java file for implement the login page from our Android Chat Application with Login and Registration form. So here just create one java file and give the name of login.java

package com.androidchatapp;
 
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
 
import org.json.JSONException;
import org.json.JSONObject;
 
public class Login extends AppCompatActivity {
    TextView register;
    EditText username, password;
    Button loginButton;
    String user, pass;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
 
        register = (TextView)findViewById(R.id.register);
        username = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);
        loginButton = (Button)findViewById(R.id.loginButton);
 
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Login.this, Register.class));
            }
        });
 
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user = username.getText().toString();
                pass = password.getText().toString();
 
                if(user.equals("")){
                    username.setError("can't be blank");
                }
                else if(pass.equals("")){
                    password.setError("can't be blank");
                }
                else{
                    String url = "https://android-chat-app-e711d.firebaseio.com/users.json";
                    final ProgressDialog pd = new ProgressDialog(Login.this);
                    pd.setMessage("Loading...");
                    pd.show();
 
                    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener(){
                        @Override
                        public void onResponse(String s) {
                            if(s.equals("null")){
                                Toast.makeText(Login.this, "user not found", Toast.LENGTH_LONG).show();
                            }
                            else{
                                try {
                                    JSONObject obj = new JSONObject(s);
 
                                    if(!obj.has(user)){
                                        Toast.makeText(Login.this, "user not found", Toast.LENGTH_LONG).show();
                                    }
                                    else if(obj.getJSONObject(user).getString("password").equals(pass)){
                                        UserDetails.username = user;
                                        UserDetails.password = pass;
                                        startActivity(new Intent(Login.this, Users.class));
                                    }
                                    else {
                                        Toast.makeText(Login.this, "incorrect password", Toast.LENGTH_LONG).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
 
                            pd.dismiss();
                        }
                    },new Response.ErrorListener(){
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            System.out.println("" + volleyError);
                            pd.dismiss();
                        }
                    });
 
                    RequestQueue rQueue = Volley.newRequestQueue(Login.this);
                    rQueue.add(request);
                }
 
            }
        });
    }
}

We need to create registration form, after complete the login page. When the new user enroll from the chat, they are must create new account. The code’s are very long so here i give the full source code for Android Chat application with Login Registration form.

After completing java codes, need to integrate XML files to pass the information for our interface. In this example i will give only for activity_main.xml file, you can get other files from download link.

XML Layouts

Now just integrate java code with xml files. so we can send the receive information through firebase. Firebase manually implemented and connected from console application. The activity_main.xml file look like,

 
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.androidchatapp.Chat">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_weight="20"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/layout1">

      </LinearLayout>
    </ScrollView>

    <include
        layout="@layout/message_area"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_marginTop="5dp"/>
</LinearLayout>

Download Source Code

Here you can download the full source code of Android Chat Application with Login Registration form using Firebase. If you are facing any issues are problem just contact us for learn more things about software technologies.

Also Read – Android Projects in free of cost

Leave a Reply