How to Create a Social Media App in Android Studio (Full Structure & Code)
This guide will help you create a fully functional social media app in Android Studio using Firebase, including:
User Authentication (Login/Signup)
Profile Creation & Picture Upload
Post Creation (Text, Images, Videos)
Like & Comment System
Follow & Unfollow Users
Realtime Notifications
AdMob Integration (Banner & Interstitial Ads)
Project Structure (Path Overview)
SocialMediaApp/
├── app/
│ ├── src/
│ │ ├── main/
│ │ │ ├── java/com/example/socialmediaapp/
│ │ │ │ ├── MainActivity.java (Home Feed)
│ │ │ │ ├── LoginActivity.java (User Login)
│ │ │ │ ├── RegisterActivity.java (User Signup)
│ │ │ │ ├── ProfileActivity.java (User Profile)
│ │ │ │ ├── PostActivity.java (Create Posts)
│ │ │ │ ├── FollowActivity.java (Follow Users)
│ │ │ │ ├── NotificationActivity.java (Realtime Notifications)
│ │ │ │ ├── AdManager.java (AdMob Ads)
│ │ │ ├── res/
│ │ │ │ ├── layout/
│ │ │ │ │ ├── activity_main.xml (Home Screen)
│ │ │ │ │ ├── activity_login.xml (Login Screen)
│ │ │ │ │ ├── activity_register.xml (Signup Screen)
│ │ │ │ │ ├── activity_profile.xml (User Profile)
│ │ │ │ │ ├── activity_post.xml (Create Post)
│ │ │ │ │ ├── activity_follow.xml (Follow Users)
│ │ │ │ │ ├── activity_notification.xml (Notifications)
│ │ │ ├── AndroidManifest.xml (Permissions & Services)
│ ├── google-services.json (Firebase Config)
├── build.gradle (Dependencies)
├── settings.gradle
Step 1: Setup Firebase & Dependencies
Create Firebase Project
- Go to Firebase Console → Firebase Console
- Click “Add Project” → Name it
"SocialMediaApp"
- Enable Firestore Database, Authentication, and Storage
- Download
google-services.json
and place it in theapp/
folder
Add Dependencies (build.gradle
)
dependencies {
implementation 'com.google.firebase:firebase-auth:22.1.1'
implementation 'com.google.firebase:firebase-firestore:24.10.0'
implementation 'com.google.firebase:firebase-storage:20.3.1'
implementation 'com.google.firebase:firebase-messaging:23.4.0'
implementation 'com.google.firebase:firebase-database:20.3.1'
implementation 'com.google.android.gms:play-services-ads:22.6.0'
}
apply plugin: 'com.google.gms.google-services'
Now, sync the project.
Step 2: User Authentication (Login & Signup)
Register User (RegisterActivity.java
)
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseFirestore db = FirebaseFirestore.getInstance();
registerButton.setOnClickListener(v -> {
String email = emailInput.getText().toString();
String password = passwordInput.getText().toString();
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Map<String, Object> user = new HashMap<>();
user.put("email", email);
user.put("profilePic", "");
db.collection("users").document(email).set(user);
startActivity(new Intent(this, MainActivity.class));
}
});
});
Login User (LoginActivity.java
)
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
startActivity(new Intent(this, MainActivity.class));
}
});
Now, users can create an account and log in.
Step 3: User Profile & Picture Upload
Modify ProfileActivity.java
FirebaseStorage storage = FirebaseStorage.getInstance();
FirebaseFirestore db = FirebaseFirestore.getInstance();
uploadButton.setOnClickListener(v -> {
StorageReference storageRef = storage.getReference().child("profile_pics/user.jpg");
storageRef.putFile(imageUri).addOnSuccessListener(taskSnapshot -> {
storageRef.getDownloadUrl().addOnSuccessListener(uri -> {
db.collection("users").document(userEmail).update("profilePic", uri.toString());
});
});
});
Now, users can upload a profile picture.
Step 4: Create & Display Posts
Create Post (PostActivity.java
)
FirebaseFirestore db = FirebaseFirestore.getInstance();
Map<String, Object> post = new HashMap<>();
post.put("text", postText);
post.put("imageUrl", imageUrl);
post.put("timestamp", System.currentTimeMillis());
db.collection("posts").add(post);
Display Posts (MainActivity.java
)
db.collection("posts").orderBy("timestamp", Query.Direction.DESCENDING)
.addSnapshotListener((snapshots, e) -> {
for (DocumentSnapshot snapshot : snapshots) {
String text = snapshot.getString("text");
String imageUrl = snapshot.getString("imageUrl");
// Load into RecyclerView
}
});
Now, users can post and see posts in the feed.
Step 5: Follow & Unfollow Users
Map<String, Object> follow = new HashMap<>();
follow.put("following", followedUserEmail);
db.collection("followers").document(currentUserEmail).collection("following").add(follow);
Now, users can follow/unfollow each other.
Step 6: Notifications System
FirebaseMessaging.getInstance().subscribeToTopic("notifications");
FirebaseMessaging.getInstance().send(new RemoteMessage.Builder("user_token")
.addData("message", "New Post from " + userName).build());
Now, users receive push notifications.
Step 7: AdMob Monetization
Modify AndroidManifest.xml
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
Modify AdManager.java
MobileAds.initialize(this);
AdView adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
Now, ads are enabled for monetization.
Final Features (Full Social Media App)
User Authentication (Login/Signup)
User Profile & Picture Upload
Post Creation & Feed Display
Like & Comment System
Follow & Unfollow Users
Realtime Notifications (Push Messaging)
AdMob Integration (Monetization)
Need Full Source Code?
Let me know, and I’ll provide a ready-to-use Android Studio project!