DEV Community

Heet kumar Kothadiya
Heet kumar Kothadiya

Posted on

Simple Chat App with Android Studio

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"?>
  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 mglist; ArrayAdapter 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(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"?>
  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

Top comments (0)