0% found this document useful (0 votes)
28 views

ChatApp 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)
28 views

ChatApp 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
You are on page 1/ 3

// Java Code for the Chat Application (Main Backend Logic)

package com.example.chatapp;

import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;


private DatabaseReference mDatabase;

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

// Initialize Firebase Authentication and Database Reference


mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();

// Example: Create a new chat room in Firebase


createChatRoom("General Chat");
}

private void createChatRoom(String roomName) {


mDatabase.child("chatrooms").child(roomName).setValue("Welcome to " +
roomName)
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Chat Room Created!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Failed to Create Chat Room",
Toast.LENGTH_SHORT).show();
}
});
}
}

/* XML Layout for activity_main.xml */


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
android:id="@+id/message_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type your message here"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dp"/>
<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:layout_toEndOf="@id/message_input"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"/>

<ListView
android:id="@+id/message_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/message_input"
android:layout_marginBottom="8dp"/>

</RelativeLayout>

// Example HTML Interface for Web (if applicable)


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat App</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
#chat-container {
display: flex;
flex-direction: column;
height: 100vh;
max-width: 600px;
margin: auto;
border: 1px solid #ccc;
}
#message-list {
flex: 1;
overflow-y: auto;
padding: 10px;
}
#message-input {
display: flex;
padding: 10px;
}
input[type="text"] {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
margin-left: 10px;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="chat-container">
<div id="message-list"></div>
<div id="message-input">
<input type="text" id="input-field" placeholder="Type your message
here...">
<button id="send-button">Send</button>
</div>
</div>

<script>
const messageList = document.getElementById("message-list");
const inputField = document.getElementById("input-field");
const sendButton = document.getElementById("send-button");

sendButton.addEventListener("click", () => {
const message = inputField.value;
if (message.trim()) {
const messageDiv = document.createElement("div");
messageDiv.textContent = message;
messageDiv.style.marginBottom = "10px";
messageList.appendChild(messageDiv);
inputField.value = "";
}
});
</script>
</body>
</html>

You might also like