TY Exp 8

📱 Android Login App —

For: Sanjay Ghodawat Polytechnic, Atigre


✅ 1️⃣ AndroidManifest.xml

Path:
app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.loginapp">

    <application
        android:allowBackup="true"
        android:label="Login App"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">

        <activity android:name=".WelcomeActivity" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

✅ 2️⃣ activity_main.xml — Login Screen

Path:
app/src/main/res/layout/activity_main.xml

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="20dp"
    android:gravity="center"
    android:background="#ECECEC">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sanjay Ghodawat Polytechnic, Atigre"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginBottom="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username" />

    <EditText
        android:id="@+id/edtUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Username" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password"
        android:layout_marginTop="10dp" />

    <EditText
        android:id="@+id/edtPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Password"
        android:inputType="textPassword" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Gender"
        android:layout_marginTop="10dp" />

    <RadioGroup
        android:id="@+id/radioGroupGender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female" />

    </RadioGroup>

    <CheckBox
        android:id="@+id/checkRemember"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remember Me" />

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:layout_marginTop="15dp" />

</LinearLayout>
</ScrollView>

✅ 3️⃣ MainActivity.java — Login Logic

Path:
app/src/main/java/com/example/loginapp/MainActivity.java

package com.example.loginapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

    EditText edtUsername, edtPassword;
    RadioGroup radioGroupGender;
    CheckBox checkRemember;
    Button btnLogin;
    ProgressBar progressBar;

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

        edtUsername = findViewById(R.id.edtUsername);
        edtPassword = findViewById(R.id.edtPassword);
        radioGroupGender = findViewById(R.id.radioGroupGender);
        checkRemember = findViewById(R.id.checkRemember);
        btnLogin = findViewById(R.id.btnLogin);
        progressBar = findViewById(R.id.progressBar);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String username = edtUsername.getText().toString().trim();
                String password = edtPassword.getText().toString().trim();
                int selectedGenderId = radioGroupGender.getCheckedRadioButtonId();

                if (username.isEmpty()) {
                    Toast.makeText(MainActivity.this, "Username cannot be empty", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (password.isEmpty()) {
                    Toast.makeText(MainActivity.this, "Password cannot be empty", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (password.length() < 6) {
                    Toast.makeText(MainActivity.this, "Password must be at least 6 characters", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (selectedGenderId == -1) {
                    Toast.makeText(MainActivity.this, "Please select gender", Toast.LENGTH_SHORT).show();
                    return;
                }

                RadioButton selectedGender = findViewById(selectedGenderId);
                String gender = selectedGender.getText().toString();
                Boolean remember = checkRemember.isChecked();

                progressBar.setVisibility(View.VISIBLE);

                if (username.equals("student") && password.equals("123456")) {

                    Intent intent = new Intent(MainActivity.this, WelcomeActivity.class);
                    intent.putExtra("username", username);
                    intent.putExtra("gender", gender);
                    intent.putExtra("remember", remember.toString());
                    startActivity(intent);

                } else {
                    Toast.makeText(MainActivity.this, "Invalid Credentials", Toast.LENGTH_SHORT).show();
                }

                progressBar.setVisibility(View.GONE);
            }
        });
    }
}

✅ 4️⃣ activity_welcome.xml — Stable Success Screen

Path:
app/src/main/res/layout/activity_welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="20dp">

    <TextView
        android:id="@+id/txtWelcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textStyle="bold"
        android:textSize="22sp" />

</LinearLayout>

✅ 5️⃣ WelcomeActivity.java

Path:
app/src/main/java/com/example/loginapp/WelcomeActivity.java

package com.example.loginapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class WelcomeActivity extends AppCompatActivity {

    TextView txtWelcome;

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

        txtWelcome = findViewById(R.id.txtWelcome);

        String username = getIntent().getStringExtra("username");
        String gender = getIntent().getStringExtra("gender");
        String remember = getIntent().getStringExtra("remember");

        txtWelcome.setText(
                "Login Successful\n\nWelcome " + username +
                        "\nGender: " + gender +
                        "\nRemember Me: " + remember +
                        "\n\nSanjay Ghodawat Polytechnic, Atigre"
        );
    }
}

🔑 Demo Credentials (for validation)

Username: student
Password: 123456

🗂 Final Project Structure

LoginApp/
└── app/
    └── src/
        └── main/
            ├── AndroidManifest.xml
            ├── java/
            │   └── com/example/loginapp/
            │       ├── MainActivity.java
            │       └── WelcomeActivity.java
            └── res/
                └── layout/
                    ├── activity_main.xml
                    └── activity_welcome.xml


Popular posts from this blog