Simple Chat App with Android Studio
Tags: geetweeklocal
Published: 2021-03-20 13:49:37

A basic chat app for your android devices.
Software used: Android studio and Firebase database.
The programming language used: XML, JAVA.
Introduction: The purpose of the chat application is to allow users to be able to chat with each other, like a normal chat application. The users will be able to chat with each other, most likely only from user to user, no group chatting will be developed, unless there is time to do so. The chat application will be written in java, but due to the lack of experience in java, while developing the application, practicing techniques with java and working on it as much as possible will help hone some java skills and be more ready to develop the application.
HERE IS SOME OF MY CODES USED TO DEVELOP THIS APP
1. XML MAIN ACTIVITY CODE
The main XML code for our app UI i.e. layout of our app.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#9BA8F3"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="20dp"
        android:text=" CHAT APP"
        android:textColor="#651FFF"
        android:textSize="40dp"
        android:textStyle="bold" />
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="567dp">
    </ListView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:orientation="horizontal"
        android:background="#596DDC"
        android:layout_marginTop="20dp">
        <EditText
            android:id="@+id/editText"
            android:layout_width="333dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Type message"
            android:inputType="textPersonName" />
        <ImageView
        android:id="@+id/send"
        android:layout_width="75dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_baseline_send_24"
        app:tint="#0F120F" />
    </LinearLayout>
</LinearLayout>
2. JAVA MAIN ACTIVITY CODE
The main java code to control the build and run our app.
public class MainActivity extends AppCompatActivity {
    ImageView iv;
    EditText ed;
    ListView lv;
    message msg;
    ArrayList<String> mglist;
    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.send);
        ed = (EditText) findViewById(R.id.editText);
        lv = (ListView) findViewById(R.id.listview);
        mglist = new ArrayList<>();
        adapter = new ArrayAdapter<String>(this, R.layout.list_layout, R.id.textView, mglist);
        Firebase.setAndroidContext(this);
        final Firebase ref = new Firebase("use your firebase link here");   // Here we will creat the reference of the DATABASE by writing it's URL and here ref is the Firebase
        // Now in the above line we have written chat/ so it will create chat table before execution
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Now within this we will write the code that will require to insert the data
                message mg = new message("USER", ed.getText().toString());   // now this mg is the object of message class which is an adapter class and there will be 2 parameter one is the name another is the text message
                ref.push().setValue(mg);
                ed.setText(" ");
            }
        });
        // Now here I will write the code that will display the data to both the user
        msg = new message(); // Empty constructor called
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference reference = firebaseDatabase.getReference("chat");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                mglist.clear();
                for (DataSnapshot ds : snapshot.getChildren()) {
                    msg = ds.getValue(message.class);
                    mglist.add(msg.getName() + "\n" + msg.getMessage());
                }
                lv.setAdapter(adapter);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });
    }
}
3. JAVA FOR DATABASE ENTRY
Create Any Java Class For Storing info that will be used in our chap app through Firebase, SAY “ MESSAGE.JAVA “.
package com.example.chatapp;
public class message {
    String name;
    String message;
    message(){
    }
    public message(String name, String message) {
        this.name = name;
        this.message = message;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
4. XML FOR CHAT HISTORY ENTRY
The XML code to view chat history through the list layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" " />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:textStyle="bold"
        android:textColor="#F0FF0000"
        android:text=" " />
</LinearLayout>
5. DEPENDENCIES FOR DATABASE IN FIREBASE
Add dependencies to “build.gradle(Module: Chatapp.app)”
mplementation 'com.google.firebase:firebase-firestore:21.7.1'
implementation 'com.google.firebase:firebase-database:19.5.0'
implementation 'com.google.firebase:firebase-analytics:17.6.0'
implementation 'com.google.android.gms:play-services-auth:18.1.0'
implementation 'com.firebase:firebase-client-android:2.5.2'
implementation 'com.firebaseui:firebase-ui-auth:6.0.2'
 
Hope you all enjoyed our post, you may mail me for any queries. 

at geekweeklocal #gwl  #geekweeklocal