Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Created November 23, 2025 12:38
Show Gist options
  • Select an option

  • Save sunmeat/f5d93831621d54a894adba61c04f134e to your computer and use it in GitHub Desktop.

Select an option

Save sunmeat/f5d93831621d54a894adba61c04f134e to your computer and use it in GitHub Desktop.
telegram v.0.01 firebase android example
MainActivity.java:
package site.sunmeat.helloworld;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.firestore.*;
import java.util.*;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String COLLECTION_NAME = "chat_messages";
private FirebaseFirestore db;
private ListView listView;
private EditText editMessage;
private TextView tvLogin;
private ArrayList<String> messagesList;
private ArrayAdapter<String> adapter;
private ListenerRegistration listenerRegistration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = FirebaseFirestore.getInstance();
listView = findViewById(R.id.listView);
editMessage = findViewById(R.id.editMessage);
tvLogin = findViewById(R.id.tvLogin);
ImageButton btnSend = findViewById(R.id.btnSend);
tvLogin.setText("Sunmeat");
messagesList = new ArrayList<>();
adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, messagesList);
listView.setAdapter(adapter);
btnSend.setOnClickListener(v -> sendMessage());
editMessage.setOnEditorActionListener((v, actionId, event) -> {
sendMessage();
return true;
});
startListening();
}
private void sendMessage() {
String login = tvLogin.getText().toString().replace("Ти: ", "").trim(); // беремо з TextView
String message = editMessage.getText().toString().trim();
if (login.isEmpty()) {
Toast.makeText(this, "Зміни логін у верхньому полі", Toast.LENGTH_SHORT).show();
return;
}
if (message.isEmpty()) {
Toast.makeText(this, "Напиши повідомлення", Toast.LENGTH_SHORT).show();
return;
}
Map<String, Object> data = new HashMap<>();
data.put("login", login);
data.put("message", message);
data.put("timestamp", FieldValue.serverTimestamp());
db.collection(COLLECTION_NAME)
.add(data)
.addOnSuccessListener(documentReference -> {
editMessage.setText(""); // очищаємо поле
})
.addOnFailureListener(e -> {
Toast.makeText(this, "Помилка: " + e.getMessage(), Toast.LENGTH_LONG).show();
});
}
private void startListening() {
listenerRegistration = db.collection(COLLECTION_NAME)
.orderBy("timestamp", Query.Direction.ASCENDING)
.addSnapshotListener((snapshots, e) -> {
if (e != null) {
Toast.makeText(this, "Помилка підключення: " + e.getMessage(), Toast.LENGTH_LONG).show();
return;
}
messagesList.clear();
assert snapshots != null;
for (QueryDocumentSnapshot doc : snapshots) {
String login = doc.getString("login");
String message = doc.getString("message");
if (login != null && message != null) {
messagesList.add(login + ": " + message);
}
}
adapter.notifyDataSetChanged();
listView.setSelection(messagesList.size() - 1);
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (listenerRegistration != null) {
listenerRegistration.remove();
}
}
}
=======================================================================================================================
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#F7F7F8"
android:clipToPadding="false"
android:divider="@android:color/transparent"
android:dividerHeight="12dp"
android:padding="12dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:elevation="4dp"
android:orientation="vertical"
android:paddingTop="12dp"
android:paddingBottom="12dp">
<TextView
android:id="@+id/tvLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginBottom="6dp"
android:text="Sunmeat"
android:textColor="#777777"
android:textSize="14sp"
android:textStyle="italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:background="#F0F2F5"
android:backgroundTint="#F0F2F5"
android:gravity="center_vertical"
android:minHeight="50dp"
android:orientation="horizontal"
android:outlineAmbientShadowColor="#40000000"
android:outlineSpotShadowColor="#40000000"
android:padding="4dp">
<EditText
android:id="@+id/editMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:background="@null"
android:hint="Напиши повідомлення..."
android:imeOptions="actionSend"
android:inputType="textCapSentences|textMultiLine|textShortMessage"
android:maxLines="6"
android:textColor="#000000"
android:textColorHint="#888888"
android:textSize="16sp" />
<ImageButton
android:id="@+id/btnSend"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="Відправити"
android:scaleType="centerInside"
android:src="@android:drawable/ic_menu_send"
app:tint="#0088CC" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment