0% found this document useful (0 votes)
17 views4 pages

ChatApp Main Interface Code

Uploaded by

a9650827710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

ChatApp Main Interface Code

Uploaded by

a9650827710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Java Code for Main Chat List Activity

package com.example.chatapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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;
import java.util.ArrayList;

public class ChatListActivity extends AppCompatActivity {

private DatabaseReference mDatabase;


private ArrayList<String> chatRooms;
private ChatRoomAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_list);

// Firebase reference to chatrooms


mDatabase = FirebaseDatabase.getInstance().getReference("chatrooms");
chatRooms = new ArrayList<>();

ListView chatRoomList = findViewById(R.id.chat_room_list);


adapter = new ChatRoomAdapter(this, chatRooms);
chatRoomList.setAdapter(adapter);

Button addChatRoomButton = findViewById(R.id.add_chatroom_button);


EditText chatRoomInput = findViewById(R.id.chatroom_input);

// Fetch chatrooms from Firebase


fetchChatRooms();

// Add a new chatroom


addChatRoomButton.setOnClickListener(v -> {
String chatRoomName = chatRoomInput.getText().toString().trim();
if (!chatRoomName.isEmpty()) {
mDatabase.child(chatRoomName).setValue("Welcome to " +
chatRoomName)
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Toast.makeText(ChatListActivity.this, "Chat Room
Added", Toast.LENGTH_SHORT).show();
chatRoomInput.setText("");
} else {
Toast.makeText(ChatListActivity.this, "Failed to
Add Chat Room", Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(ChatListActivity.this, "Chat Room Name Cannot Be
Empty", Toast.LENGTH_SHORT).show();
}
});

// Navigate to specific chatroom


chatRoomList.setOnItemClickListener((parent, view, position, id) -> {
String chatRoomName = chatRooms.get(position);
Intent intent = new Intent(ChatListActivity.this, ChatActivity.class);
intent.putExtra("chatRoomName", chatRoomName);
startActivity(intent);
});
}

private void fetchChatRooms() {


mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
chatRooms.clear();
for (DataSnapshot roomSnapshot : snapshot.getChildren()) {
chatRooms.add(roomSnapshot.getKey());
}
adapter.notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError error) {
Toast.makeText(ChatListActivity.this, "Failed to load chat rooms",
Toast.LENGTH_SHORT).show();
}
});
}
}

/* XML Layout for activity_chat_list.xml */


<?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"
android:padding="16dp">

<TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat App"
android:textSize="24sp"
android:textStyle="bold"
android:layout_gravity="center"/>

<EditText
android:id="@+id/chatroom_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter chat room name"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"/>

<Button
android:id="@+id/add_chatroom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Chat Room"
android:layout_gravity="end"/>

<ListView
android:id="@+id/chat_room_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"/>

</LinearLayout>

// Adapter Class for Chat Rooms (ChatRoomAdapter.java)


package com.example.chatapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class ChatRoomAdapter extends


RecyclerView.Adapter<ChatRoomAdapter.ViewHolder> {

private final Context context;


private final List<String> chatRooms;

public ChatRoomAdapter(Context context, List<String> chatRooms) {


this.context = context;
this.chatRooms = chatRooms;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.chat_room_item,
parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.chatRoomName.setText(chatRooms.get(position));
}

@Override
public int getItemCount() {
return chatRooms.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {


TextView chatRoomName;

public ViewHolder(@NonNull View itemView) {


super(itemView);
chatRoomName = itemView.findViewById(R.id.chat_room_name);
}
}
}

/* XML Layout for chat_room_item.xml */


<?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="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
android:background="?attr/selectableItemBackground">

<TextView
android:id="@+id/chat_room_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"/>

</LinearLayout>

You might also like