![]() |
VOOZH | about |
This is the Part 1 of "Build a Social Media App on Android Studio" tutorial, and we are going to cover the following functionalities in this article:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Connect your app to firebase
Go to the Tools > Firebase > Authentication and connect your app to firebase and also add the firebase authentication SDK to your app like the following image.
Then go to the Firebase Console> Authentication > Sign-in method and Enable the Email/Password.
Step 3: Create 4 new empty activities
Go to the package name > right-click > New > Activity > Empty Activity and name the activity as SplashScreen, RegistrationActivity, LoginActivity, and DashboardActivity. You may also refer to this article How to create new empty activity in android studio.
Step 4:Working with the SplashScreen Activity
Navigate to the app > res > layout > activity_splash_screen.xml and add the below code to that file. Below is the code for the activity_splash_screen.xml file.
Working with the SplashScreen.java file. Here we are checking that if the user is null then go to LoginActivity. Else move to DashboardActivity.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
FirebaseUser user=mAuth.getCurrentUser();
if(user==null){
Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
startActivity(intent);
finish();
}
else {
Intent mainIntent= new Intent(SplashScreen.this, DashboardActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
}
},1000);
Below is the code for the SplashScreen.java file.
Step 5:Working with the RegistrationActivity Activity
Navigate to the app > res > layout > activity_registration.xml and add the below code to that file. Below is the code for the activity_registration.xml file. Here we have created three EditText for the name, email and password and one Button to Register user.
Working with the RegistrationActivity.java file. Creating a user with the email and password written by the user. If it fails then we will be showing the error.
mAuth.createUserWithEmailAndPassword(emaill, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
progressDialog.dismiss();
}
else {
progressDialog.dismiss();
Toast.makeText(RegistrationActivity.this,"Error",Toast.LENGTH_LONG).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(RegistrationActivity.this,"Error Occurred",Toast.LENGTH_LONG).show();
}
});
Below is the code for the RegistrationActivity.java file.
Step 6:Working with the LoginActivity Activity
Navigate to the app > res > layout > activity_login.xml and add the below code to that file. Below is the code for the activity_login.xml file. Here We are creating two EditText for email and password and one Button to Login.
Working with the LoginActivity.java file. Signing in user with the email and password written by the user. If it fails then we will be showing the error.
mAuth.signInWithEmailAndPassword(emaill, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
loadingBar.dismiss();
}
else {
loadingBar.dismiss();
Toast.makeText(LoginActivity.this,"Login Failed",Toast.LENGTH_LONG).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
loadingBar.dismiss();
Toast.makeText(LoginActivity.this,"Error Occurred",Toast.LENGTH_LONG).show();
}
});
Below is the code for the LoginActivity.java file.
Step 7: Working with the AndroidManifest.xml file
Navigate to the AndroidManifest.xml file and add the below permission for getting internet permission in the app.
<uses-permission android:name=โandroid.permission.INTERNETโ/>
Also, make the SplashScreen Activity as the welcome screen. Refer to the following code
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Below is the complete code for the AndroidManifest.xml file.
Step 8: Working with the colors.xml file
Go to the app > res > values > colors.xml file and use the following color
Registration:
Login:
After successfully authenticated the admin can see the users in the firebase console like the following
Note: Please Add drawable items before running the Application
Below is the file structure after performing these operations:
Note: In this part, there is nothing to do with the DashboardActivity.