Assignment 2
Assignment 2
Total Marks: 4
Obtained Marks:
Android Application
Development
Assignment # 02
Last date of Submission: 28th Oct 2022 (11:59 pm)
Instructions:
● Late submissions are not entertained in any case.
Question#1: (4 Marks)
By applying the concepts taught in class, you are to implement a Sign-Up and Login form in
Android Studio using Fragments. You will have the following files in your project:
In the main activity layout file you will place two buttons (B1, B2) and keep the rest space empty
for Fragment swapping/replacement.
If B1 is clicked LoginFragment will pop up & if B2 is clicked SignupFragment will replace
LoginFragment.
B1 B2
If B1 is clicked
LoginFragment
here.
If B2 is clicked
SignupFragment
here.
Submission guidelines:
You are to submit your code and output screenshots in a word document (in the form of a
report). Submit project file (code) as-well but zipped. Do not include report in project zip file.
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
Code:
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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/b1"
android:layout_above="@id/b2"
android:layout_margin="8dp"
android:background="#EDEDED"
tools:ignore="NotSibling">
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Login"
android:textAlignment="center"
tools:ignore="MissingConstraints" />
</FrameLayout>
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/b1"
android:text="Signup"
tools:ignore="MissingConstraints,NotSibling" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code:
package com.example.assingment2;
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginButton = findViewById(R.id.b1);
Button signupButton = findViewById(R.id.b2);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new
LoginFragment())
.commit();
}
});
signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new
SignupFragment())
.commit();
}
});
}
}
package com.example.assingment2;
import androidx.fragment.app.Fragment;
package com.example.assingment2;
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
import androidx.fragment.app.Fragment;
Output: