Q11

Question 11

Create a login application as above, on successful login redirect to another activity without logout button a dialog appears with OK and CANCEL button. On OK button click go to login activity and on CANCEL stay at same activity.

App Insights:

Used functional code of previous program with hard coded credentials, added a login activity on successful login, used DialogInterface and AlertDialog for logout functionality

Browse Source Code

Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Login Application"
        android:layout_marginTop="25dp"
        android:textSize="25dp"
        android:textColor="@color/black"
        />

    <TextView
        android:id="@+id/t2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginTop="220dp"
        android:text="Username"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/t1"
        android:layout_marginLeft="50dp" />

    <TextView
        android:id="@+id/t3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="250dp"
        android:text="Password"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/t2" />

    <EditText
        android:id="@+id/e1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:layout_marginTop="200dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toEndOf="@id/t2"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="50dp" />

    <EditText
        android:id="@+id/e2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="230dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintLeft_toRightOf="@id/t3"
        app:layout_constraintTop_toBottomOf="@id/t2" />

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="check"
        android:text="Login"
        android:layout_marginTop="300dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

Output

Studio Screenshot Main Activity (Login Page) (1/14)
App Screenshot Main Activity (Login Page) (2/14)
Studio Screenshot Taking User Credentials (3/14)
App Screenshot Taking User Credentials (4/14)
Studio Screenshot Validating User With New Activity (Login Activity) (5/14)
App Screenshot Validating User With New Activity (Login Activity) (6/14)
Studio Screenshot Alert Dialog On Pressing Logout Button (7/14)
App Screenshot Alert Dialog On Pressing Logout Button (8/14)
Studio Screenshot (On Pressing No) (9/14)
App Screenshot (On Pressing No) (10/14)
Studio Screenshot Alert Dialog On Pressing Logout Button (11/14)
App Screenshot Alert Dialog On Pressing Logout Button (12/14)
Studio Screenshot (On Pressing Yes) (13/14
Studio Screenshot (On Pressing Yes) (14/14)

Last updated