📝
Android Programming
  • Introduction
  • Contents
  • Android
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
    • End
Powered by GitBook
On this page
  • 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.
  • Code
  • Output
  1. Android

Q11

Question 11

PreviousQ10NextQ12

Last updated 4 years ago

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

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>
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
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=".LoginActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="125dp"
        android:layout_marginTop="50dp"
        android:text="Login Successful"
        android:textSize="25dp"
        android:textColor="@color/black"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2"
        android:layout_marginStart="165dp"
        android:layout_marginTop="25dp"
        android:layout_marginBottom="8dp"
        android:onClick="onCL"
        android:text="Logout"
        android:layout_marginLeft="150dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.q11_advanced_login_app">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Q11_Advanced_Login_App">
        <activity android:name=".LoginActivity">
        </activity>
        <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>
package com.example.q11_advanced_login_app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


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

    public void check(View v) {
        String txt = "Welcome ";
        EditText ed1 = (EditText) findViewById(R.id.e1);
        EditText ed2 = (EditText) findViewById(R.id.e2);

        String a=ed1.getText().toString();
        String b=ed2.getText().toString();
        if(a.equals("Jatin") && b.equals("8647")) {
            Intent intent=new Intent(this,
             LoginActivity.class);
            intent.putExtra("username",a);
            startActivity(intent);

        } else {
            Toast.makeText(this,"Invalid User",
             Toast.LENGTH_LONG).show();
        }
    }

}
package com.example.q11_advanced_login_app;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;


public class LoginActivity extends AppCompatActivity {

    TextView textView;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        textView=findViewById(R.id.textView2);
        Intent f=getIntent();
        String d=f.getStringExtra("username");
        textView.setText("Hello "+d);
        Toast.makeText(this,"Login Successful", 
        Toast.LENGTH_LONG).show();
    }
    
    public void onCL(View v) {
        AlertDialog.Builder dbox = 
        new AlertDialog.Builder(this);
        dbox.setTitle("Confirm Dialog Box");
        dbox.setMessage("Do you want to log out?");

        dbox.setPositiveButton("YES", (dialog, which) -> {

            Intent i=new Intent(getApplicationContext(),
            MainActivity.class);
            startActivity(i);
        });

        dbox.setNegativeButton("NO", (dialog, which) -> 
        Toast.makeText(getApplicationContext(), 
        "Application not exited" , Toast.LENGTH_SHORT).show());
        dbox.show();
    }
}

Output

Download to Run and Test Locally

Q11_Advanced_Login_App.apk
Browse Source Code
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)