I'm trying to create a group chat app but when clicking on the send button, the message does not send. Please can anyone tell me what is wrong with my group chat app?
Here is the mainactivity xml file :
<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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="45dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:background="#147A18"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Enter message"
android:inputType="textPersonName"
android:textColor="#FF5722" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_weight="1"
android:background="#A5DD64"
app:srcCompat="@android:drawable/ic_menu_send" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
it is item message xml file :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:orientation="horizontal"
android:id="@+id/l1Message"
>
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:id="@+id/tvTitle"
android:text=" Random text"
android:padding="5dp"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/delete"
android:background="@color/colorPrimary"
android:src="@android:drawable/ic_delete"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
here mainActivity java class :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
FirebaseAuth fAuth;
DatabaseReference messagedb;
FirebaseDatabase database;
MessageAdapter messageAdapter;
List<Message> messages;
RecyclerView rvMessage;
EditText enterMess;
ImageButton imagebtn;
User u;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
// fAuth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();
enterMess = findViewById(R.id.message);
rvMessage = findViewById(R.id.rvmessage);
imagebtn = findViewById(R.id.imageButton);
imagebtn.setOnClickListener(this);
messages =new ArrayList<>();
}
@Override
public void onClick(View v) {
}
// Initialize Firebase Auth
@Override
protected void onStart() {
super.onStart();
final FirebaseUser curruser =fAuth.getCurrentUser();
if(curruser != null) {
u.setUid(curruser.getUid());
u.setEmail(curruser.getEmail());
database.getReference("Users").child(curruser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
u=dataSnapshot.getValue(User.class);
u.setUid(curruser.getUid());
AllMethods.name= u.getName();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
messagedb = database.getReference("messages");
messagedb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Message message = dataSnapshot.getValue(Message.class);
message.setKey(dataSnapshot.getKey());
messages.add(message);
displayMessages(messages);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Message message = dataSnapshot.getValue(Message.class);
message.setKey(dataSnapshot.getKey());
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}}
private void displayMessages(List<Message> messages)
{
rvMessage.setLayoutManager(new LinearLayoutManager(MainActivity.this));
// rvMessage.setHasFixedSize(true);
messageAdapter = new MessageAdapter(MainActivity.this,messages,messagedb);
rvMessage.setAdapter(messageAdapter);
}
}
and it is User java class :
public class User {
String uid,name,email;
public User(){}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return "uid ("+uid + '\''+
",name= "+name + '\''+
",email= "+email+ '\''+")";
}}
it is message java class :
public class Message {
String message , name ,key;
public Message(){}
public Message(String message, String name, String key)
{
this.message=message;
this.name=name;
this.key=key;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@NonNull
@Override
public String toString() {
return "Message ("+message + '\''+
",name= "+name + '\''+
",key= "+key+ '\''+")";
}
}
it is allMethods java class :
public class AllMethods {
public static String name ="";
}
and manifest xml file :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.examble.chatapp">
<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>