Login & Logout Features in Android Application
File Name: activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.abbu.login_logout.MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="187dp" android:id="@+id/editTextPass" android:hint="password" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:layout_alignBottom="@+id/editTextPass" android:layout_alignEnd="@+id/editTextPass" android:layout_marginBottom="80dp" android:id="@+id/editTextUser" android:hint="username" /> <Button android:text="Login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editTextPass" android:layout_centerHorizontal="true" android:layout_marginTop="72dp" android:id="@+id/button" /> </RelativeLayout>File Name: MainActivity.java
package com.example.abbu.login_logout; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import static android.R.attr.value; public class MainActivity extends AppCompatActivity { EditText username, password; Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); username = (EditText) findViewById(R.id.editTextUser); password = (EditText) findViewById(R.id.editTextPass); login = (Button) findViewById(R.id.button); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){ Intent i = new Intent(MainActivity.this.getApplicationContext(), Logout.class); i.putExtra("message", value); startActivity(i); } else{ Toast.makeText(MainActivity.this, "Invalid Details", Toast.LENGTH_SHORT).show(); } } }); } }File Name: logout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Logout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="125dp" android:id="@+id/buttonLogout" /> </RelativeLayout>File Name: Logout.java
package com.example.abbu.login_logout; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.util.Log; import android.view.View; import android.widget.Button; import static android.content.ContentValues.TAG; /** * Created by Abbu on 4/15/2017. */ public class Logout extends MainActivity { Button Logout; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logout); Logout = (Button) findViewById(R.id.buttonLogout); Intent in = getIntent(); String string = in.getStringExtra("message"); Logout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Logout.this); builder.setTitle("Confirmation PopUp!"). setMessage("You sure, that you want to logout?"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(getApplicationContext(), MainActivity.class); startActivity(i); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert11 = builder.create(); alert11.show(); } }); } }Output:
After clicking on LOGIN Button, logout.xml will apear.
activity_main.xml Now, when you click on LOGOUT Button, a confirmation popup (dialog box) will appear.
After_Successful_Login (logout.xml) After clicking on YES in the confirmation popup, we'll come back to activity_main.xml
Logout_Confirmation
After_Logout