activitylogin.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout…
- User
activity_login.xml
<androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" app:cardCornerRadius="8dp" app:cardElevation="4dp">
</androidx.cardview.widget.CardView>
LoginActivity package com.example.gpay;
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat;
import com.google.firebase.FirebaseApp; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.FirebaseFirestore;
public class LoginActivity extends AppCompatActivity {
private FirebaseAuth mAuth; private FirebaseFirestore db;
private EditText phoneEditText; private Button registerButton, loginButton; private TextView messageTextView; private ProgressBar progressBar; private ImageView loadingImageView; private AnimatedVectorDrawableCompat loadingAnimation;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FirebaseApp.initializeApp(this); setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance(); db = FirebaseFirestore.getInstance();
phoneEditText = findViewById(R.id.phoneEditText); registerButton = findViewById(R.id.registerButton); loginButton = findViewById(R.id.loginButton); messageTextView = findViewById(R.id.messageTextView); progressBar = findViewById(R.id.progressBar);
createDummyAccounts();
registerButton.setOnClickListener(v -> { String phone = phoneEditText.getText().toString(); if (phone.isEmpty()) { messageTextView.setText("Please enter phone number"); } else { showLoading(true); registerUser(phone); } });
loginButton.setOnClickListener(v -> { String phone = phoneEditText.getText().toString(); if (phone.isEmpty()) { messageTextView.setText("Please enter phone number"); } else { showLoading(true); loginUser(phone); } }); }
private void showLoading(boolean show) { if (show) { progressBar.setVisibility(View.VISIBLE); } else { progressBar.setVisibility(View.GONE); } }
private void registerUser(String phone) { db.collection("users").document(phone) .get() .addOnCompleteListener(task -> { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (!document.exists()) { User user = new User(phone, 0); // Register with zero balance db.collection("users").document(phone).set(user) .addOnSuccessListener(aVoid -> { showLoading(false); messageTextView.setText("User registered successfully."); phoneEditText.setText(""); }) .addOnFailureListener(e -> { showLoading(false); messageTextView.setText("Error registering user: " + e.getMessage()); }); } else { showLoading(false); messageTextView.setText("User already exists."); } } else { showLoading(false); messageTextView.setText("Error checking user existence."); } }); }
private void createDummyAccounts() { User user1 = new User("1234567890", 100.0); // User 1 with 50
db.collection("users").document(user1.getPhoneNum()).set(user1) .addOnSuccessListener(aVoid -> { System.out.println("Dummy user 1 created."); }) .addOnFailureListener(e -> { System.out.println("Error creating dummy user 1: " + e.getMessage()); });
db.collection("users").document(user2.getPhoneNum()).set(user2) .addOnSuccessListener(aVoid -> { System.out.println("Dummy user 2 created."); }) .addOnFailureListener(e -> { System.out.println("Error creating dummy user 2: " + e.getMessage()); }); }
private void loginUser(String phone) { db.collection("users").document(phone) .get() .addOnCompleteListener(task -> { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (document.exists()) { showLoading(false); Intent intent = new Intent(LoginActivity.this, TransferActivity.class); intent.putExtra("phoneNum", phone); startActivity(intent); finish(); // Close LoginActivity } else { showLoading(false); messageTextView.setText("Login failed. User not found."); } } else { showLoading(false); messageTextView.setText("Error logging in. Try again."); } }); } } enhance the look and feel of login ui of my application make necessary changes and give me FULL CODE
- Assistant
Sure! I've enhanced the look and feel of your login UI to make it more modern and user-friendly. Below is the full updated code for both the
activity_login.xmlandLoginActivity.javafiles.Changes Made:
- Switched to ConstraintLayout for better control over the UI elements.
- Added an App Logo at the top to enhance branding.
- Improved Typography by using custom fonts and adjusting text sizes.
- Used Material Design Components such as
TextInputLayoutfor better input fields. - Styled Buttons with rounded corners and ripple effects.
- Added Color Scheme consistent with Material Design guidelines.
- Enhanced Progress Bar visibility by placing it over the UI when loading.
1. Updated
activity_login.xml<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <!-- App Logo --> <ImageView android:id="@+id/appLogo" android:layout_width="120dp" android:layout_height="120dp" android:src="@drawable/ic_app_logo" android:contentDescription="App Logo" app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_margin="48dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- Title --> <TextView android:id="@+id/loginTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to GPay" android:textSize="24sp" android:textStyle="bold" android:textColor="#333333" android:fontFamily="@font/poppins_bold" app:layout_constraintTop_toBottomOf="@+id/appLogo" app:layout_constraintTop_margin="16dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- Subtitle --> <TextView android:id="@+id/loginSubtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login or Register to continue" android:textSize="16sp" android:textColor="#666666" android:fontFamily="@font/poppins_regular" app:layout_constraintTop_toBottomOf="@+id/loginTitle" app:layout_constraintTop_margin="8dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- Phone Input --> <com.google.android.material.textfield.TextInputLayout android:id="@+id/phoneInputLayout" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Phone Number" app:boxBackgroundMode="outline" app:boxStrokeColor="#3498db" app:boxStrokeWidthFocused="2dp" app:layout_constraintTop_toBottomOf="@+id/loginSubtitle" app:layout_constraintTop_margin="32dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_margin="32dp" app:layout_constraintEnd_margin="32dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/phoneEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" android:fontFamily="@font/poppins_medium" /> </com.google.android.material.textfield.TextInputLayout> <!-- Buttons --> <LinearLayout android:id="@+id/buttonLayout" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintTop_toBottomOf="@+id/phoneInputLayout" app:layout_constraintTop_margin="24dp" app:layout_constraintStart_toStartOf="@+id/phoneInputLayout" app:layout_constraintEnd_toEndOf="@+id/phoneInputLayout"> <Button android:id="@+id/registerButton" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:text="Register" android:textColor="#FFFFFF" android:backgroundTint="#f1c40f" android:fontFamily="@font/poppins_medium" style="@style/Widget.MaterialComponents.Button" android:layout_marginEnd="8dp" /> <Button android:id="@+id/loginButton" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:text="Login" android:textColor="#FFFFFF" android:backgroundTint="#3498db" android:fontFamily="@font/poppins_medium" style="@style/Widget.MaterialComponents.Button" android:layout_marginStart="8dp" /> </LinearLayout> <!-- Message TextView --> <TextView android:id="@+id/messageTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="14sp" android:textColor="#e74c3c" android:fontFamily="@font/poppins_medium" app:layout_constraintTop_toBottomOf="@+id/buttonLayout" app:layout_constraintTop_margin="16dp" app:layout_constraintStart_toStartOf="@+id/phoneInputLayout" app:layout_constraintEnd_toEndOf="@+id/phoneInputLayout" /> <!-- Progress Bar Overlay --> <FrameLayout android:id="@+id/progressOverlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#80000000" android:visibility="gone"> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleLarge" android:layout_width="64dp" android:layout_height="64dp" android:layout_gravity="center" /> </FrameLayout> </androidx.constraintlayout.widget.ConstraintLayout>Explanation:
- ConstraintLayout: Replaced
LinearLayoutwithConstraintLayoutfor better positioning of elements. - App Logo: Added an
ImageViewat the top with@drawable/ic_app_logoas the source. You'll need to add an image namedic_app_logo.pngin yourdrawablefolder. This can be your app's logo. - Custom Fonts: Used a custom font
Poppins. You'll need to add thePoppinsfont files in yourres/fontdirectory. I've usedpoppins_bold,poppins_medium, andpoppins_regular. - TextInputLayout: Used
com.google.android.material.textfield.TextInputLayoutandTextInputEditTextfor the phone input to make it more materialistic. - Buttons: Styled the buttons using
style="@style/Widget.MaterialComponents.Button"and added rounded corners by default. - Progress Overlay: Wrapped the
ProgressBarinside aFrameLayoutthat covers the entire screen with a semi-transparent background when loading. - Colors: Adjusted the color scheme to make it more visually appealing.
2. Updated
LoginActivity.javapackage com.example.gpay; import android.content.Intent; import android.graphics.Typeface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ProgressBar; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.google.firebase.FirebaseApp; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.FirebaseFirestore; public class LoginActivity extends AppCompatActivity { private FirebaseFirestore db; private EditText phoneEditText; private Button registerButton, loginButton; private TextView messageTextView; private FrameLayout progressOverlay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FirebaseApp.initializeApp(this); setContentView(R.layout.activity_login); db = FirebaseFirestore.getInstance(); phoneEditText = findViewById(R.id.phoneEditText); registerButton = findViewById(R.id.registerButton); loginButton = findViewById(R.id.loginButton); messageTextView = findViewById(R.id.messageTextView); progressOverlay = findViewById(R.id.progressOverlay); // Set custom font if needed // Typeface poppinsRegular = Typeface.createFromAsset(getAssets(), "fonts/poppins_regular.ttf"); // phoneEditText.setTypeface(poppinsRegular); createDummyAccounts(); registerButton.setOnClickListener(v -> { String phone = phoneEditText.getText().toString().trim(); if (phone.isEmpty()) { messageTextView.setText("Please enter phone number"); } else { messageTextView.setText(""); showLoading(true); registerUser(phone); } }); loginButton.setOnClickListener(v -> { String phone = phoneEditText.getText().toString().trim(); if (phone.isEmpty()) { messageTextView.setText("Please enter phone number"); } else { messageTextView.setText(""); showLoading(true); loginUser(phone); } }); } private void showLoading(boolean show) { if (show) { progressOverlay.setVisibility(View.VISIBLE); } else { progressOverlay.setVisibility(View.GONE); } } private void registerUser(String phone) { db.collection("users").document(phone) .get() .addOnCompleteListener(task -> { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (!document.exists()) { User user = new User(phone, 0); // Register with zero balance db.collection("users").document(phone).set(user) .addOnSuccessListener(aVoid -> { showLoading(false); messageTextView.setText("User registered successfully."); phoneEditText.setText(""); }) .addOnFailureListener(e -> { showLoading(false); messageTextView.setText("Error registering user: " + e.getMessage()); }); } else { showLoading(false); messageTextView.setText("User already exists."); } } else { showLoading(false); messageTextView.setText("Error checking user existence."); } }); } private void createDummyAccounts() { User user1 = new User("1234567890", 100.0); // User 1 with $100 User user2 = new User("0987654321", 50.0); // User 2 with $50 db.collection("users").document(user1.getPhoneNum()).set(user1) .addOnSuccessListener(aVoid -> { System.out.println("Dummy user 1 created."); }) .addOnFailureListener(e -> { System.out.println("Error creating dummy user 1: " + e.getMessage()); }); db.collection("users").document(user2.getPhoneNum()).set(user2) .addOnSuccessListener(aVoid -> { System.out.println("Dummy user 2 created."); }) .addOnFailureListener(e -> { System.out.println("Error creating dummy user 2: " + e.getMessage()); }); } private void loginUser(String phone) { db.collection("users").document(phone) .get() .addOnCompleteListener(task -> { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (document.exists()) { showLoading(false); Intent intent = new Intent(LoginActivity.this, TransferActivity.class); intent.putExtra("phoneNum", phone); startActivity(intent); finish(); // Close LoginActivity } else { showLoading(false); messageTextView.setText("Login failed. User not found."); } } else { showLoading(false); messageTextView.setText("Error logging in. Try again."); } }); } }Explanation:
- Progress Overlay Control: Updated the
showLoading()method to control the visibility of theFrameLayoutthat contains theProgressBar. - Trimmed Input: Added
.trim()to the phone number input to remove any leading or trailing spaces. - Cleared Messages: Cleared the
messageTextViewbefore starting actions to prevent old messages from showing. - Custom Fonts (Optional): Included code to set custom fonts if you decide to use them.
3. Additional Resources Needed
3.1 Fonts
You'll need to add the
Poppinsfont files to your project. Create afontresource directory underres:- Directory:
app/src/main/res/font/
Place the following font files in the
fontdirectory:poppins_regular.ttfpoppins_medium.ttfpoppins_bold.ttf
You can download these font files from Google Fonts.
3.2 App Logo
Add your app logo image to the
drawableresource directory.- Directory:
app/src/main/res/drawable/
Place your logo image and name it
ic_app_logo.png. Ensure the image size is appropriate (e.g., 512x512 pixels).
4. Dependencies
Make sure you have the following dependencies in your
build.gradlefile:dependencies { // Other dependencies // Material Design Components implementation 'com.google.android.material:material:1.9.0' }
5. Final Notes
- Testing: After making these changes, run your app to see the enhanced UI.
- Further Customization: Feel free to adjust colors, fonts, and styles to match your app's branding.
- Error Handling: Ensure that all Firebase operations are properly secured and handle exceptions as needed.
I hope this enhances the look and feel of your login UI! Let me know if you need further assistance.