Steps and code to build a chat message using the Firebase databse in Android is shown below:
package com.example.yourname.yourapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;public class MainActivity extends AppCompatActivity {
private DatabaseReference myDatabase;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);myDatabase = FirebaseDatabase.getInstance().getReference(“Message”);
final TextView myText = findViewById(R.id.textbox);
myDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {myText.setText(dataSnapshot.getValue().toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
myText.setText(“CANCELLED”);}
});
}public void sendMessage(View view){
EditText myEditText = findViewById(R.id.editText);myDatabase.push().setValue(myEditText.getText().toString());
myEditText.setText(“”);
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.yourname.yourapplication”><application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application></manifest>
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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”
tools:context=”com.example.yourname.yourapplication.MainActivity”><TextView
android:id=”@+id/textbox”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_above=”@+id/editText”
android:layout_alignParentEnd=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentRight=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true”
android:gravity=”bottom”
android:hint=”@string/chat_message_will_appear_here”
android:textAlignment=”gravity”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” /><EditText
android:id=”@+id/editText”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_toLeftOf=”@+id/sendButton”
android:layout_toStartOf=”@+id/sendButton”
android:ems=”10″
android:hint=”@string/type_here”
android:inputType=”textPersonName” /><Button
android:id=”@+id/sendButton”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBottom=”@+id/editText”
android:layout_alignParentEnd=”true”
android:layout_alignParentRight=”true”
android:onClick=”sendMessage”
android:text=”@string/send” /></RelativeLayout>
Some posts/ Comments from YoutTube channel:
How to add Encryption feature in the chat app?
Adding encryption are advanced concepts which is not the scope of this tutorial.
However, to explain in very simple terms. Let’s assume you want to add end-to-end encryption. Then in that case you will have to devise some simple encryption algorithm. (NOTE: Encryption in itself is vast field of study and mostly done by advanced mathematics students).
Once that algorithm is decided encrypt the message at the source before posting it in the firebase database using that algorithm. Then de-crypt the algorithm at destination after receiving the message from Firebase database.
One simple example could be, say in your encryption algorithm you duplicate each letter and then increase the first by one character and second by two character. Now, if your message is “GOD IS GREAT”. Then the encrypted message will be: “HIPQEF JKTU HISTFGBCUV”. Just push this message in the firebase database. Then on the receiving end convert it back to the original message as you know the algorithm to convert. “HI” will become “G”. “PQ” will be “O” … and so on. This will ensure if anyone has access to your messages in the database, then also he can’t decode the actual message unless he knows your encryption algorithm.
Great article neatly and well done presentation . thanks for sharing such a wondefull article. recently i have published same article related to same topichere i have shared source url this will help your readers https://blog.mirrorfly.com/build-chat-app-for-android/
Hey,
Yes, your page also looks great. Thanks for posting the URL here.
Thanks …. good going!!!
Cheers
Programmer World
–