Android Chat Message App – Part 2 – How to create custom unique ID and parse it for firebase db ?

This video is continuation of the previous video which can be seen at: https://youtu.be/Hn_wfTqFbdg
In the previous video the complete steps to build a Android Chat Message App is shown.

In this video I will show advance features of chat message. Like how to create child node to store the Message texts in the Firebase Database. Further it is shows that how to parse the unique ID and do some kind of formatting before displaying the text in App’s text area.

The 3rd part of video is published. It shows how to store the chat messages in an encrypted form in database. You can watch it at: https://youtu.be/bVXT_AkHIEQ

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

Source Code:

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) {

String[] Messages = dataSnapshot.getValue().toString().split(“,”);

myText.setText(“”); // Cleaning the text Area

for (int i=0; i<Messages.length;i++){
String[] finalMsg = Messages[i].split(“=”);
myText.append(finalMsg[1] + “\n”);
}

}

@Override
public void onCancelled(DatabaseError databaseError) {
myText.setText(“CANCELLED”);

}
});
}

public void sendMessage(View view){
EditText myEditText = findViewById(R.id.editText);

myDatabase.child(Long.toString(System.currentTimeMillis())).setValue(myEditText.getText().toString());
myEditText.setText(“”);
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
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&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
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>

Leave a Reply

%d bloggers like this: