This is a continuation of my previous post showing how to make a login application using MS SQL Server database in Android. Here I will be using android shared preferences to achieve the Login and Logout functionality. Please visit previous to know about the database script and more information.
In your Android application project, edit the activity_main.xml file as :
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#282828" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" android:padding="10dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="SQL APP" android:textColor="#00aa55" android:textSize="35sp" android:textStyle="bold" android:typeface="sans" /> <EditText android:id="@+id/edtuserid" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="#5d5d5d" android:hint="USER ID" android:padding="10dp" android:textColor="#ffffff" android:textColorHint="#ffffff" android:textSize="20sp" android:textStyle="bold" /> <EditText android:id="@+id/edtpass" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#5d5d5d" android:hint="PASSWORD" android:inputType="textPassword" android:padding="10dp" android:textColor="#ffffff" android:textColorHint="#ffffff" android:textSize="20sp" android:textStyle="bold" /> <Button android:id="@+id/btnlogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="#808080" android:padding="10dp" android:text="Login" android:textColor="#ffffff" android:textSize="20sp" android:textStyle="bold" /> <ProgressBar android:id="@+id/pbbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout> </RelativeLayout>
This will make the design like this :
Now make a new layout xml file named secondactivity.xml
secondactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#282828" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/txtuserid" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="MMMM" android:textColor="#00aa55" android:textSize="35sp" android:textStyle="bold" android:typeface="sans" /> <TextView android:id="@+id/txtnooftimes" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="0000" android:textColor="#fff" android:textSize="25sp" android:textStyle="bold" android:typeface="sans" /> </LinearLayout> </RelativeLayout>
This will make the layout design like this :
Open the MainActivity.java file and edit it as following :
MainActivity.java
package com.example.sqllogin; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends Activity { ConnectionClass connectionClass; EditText edtuserid, edtpass; Button btnlogin; ProgressBar pbbar; SharedPreferences shp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); connectionClass = new ConnectionClass(); edtuserid = (EditText) findViewById(R.id.edtuserid); edtpass = (EditText) findViewById(R.id.edtpass); btnlogin = (Button) findViewById(R.id.btnlogin); pbbar = (ProgressBar) findViewById(R.id.pbbar); pbbar.setVisibility(View.GONE); shp = this.getSharedPreferences("UserInfo", MODE_PRIVATE); String userid = shp.getString("UserId", "none"); if (userid.equals("none") || userid.trim().equals("")) { } else { Intent i = new Intent(MainActivity.this, SecondActivity.class); startActivity(i); finish(); } btnlogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DoLogin doLogin = new DoLogin(); doLogin.execute(""); } }); } public class DoLogin extends AsyncTask<String, String, String> { String z = ""; Boolean isSuccess = false; String userid = edtuserid.getText().toString(); String password = edtpass.getText().toString(); @Override protected void onPreExecute() { pbbar.setVisibility(View.VISIBLE); } @Override protected void onPostExecute(String r) { pbbar.setVisibility(View.GONE); Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show(); if (isSuccess) { SharedPreferences.Editor edit = shp.edit(); edit.putString("UserId", userid); edit.commit(); Intent i = new Intent(MainActivity.this, SecondActivity.class); startActivity(i); finish(); } } @Override protected String doInBackground(String... params) { if (userid.trim().equals("") || password.trim().equals("")) z = "Please enter User Id and Password"; else { try { Connection con = connectionClass.CONN(); if (con == null) { z = "Error in connection with SQL server"; } else { String query = "select * from Usertbl where UserId='" + userid + "' and password='" + password + "'"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); if (rs.next()) { z = "Login successfull"; isSuccess = true; } else { z = "Invalid Credentials"; isSuccess = false; } } } catch (Exception ex) { isSuccess = false; z = "Exceptions"; } } return z; } } }
Here I’m using SharedPreferences to store the UserID of the user once he/she as logged in. Next time when the user is opening the app, if the method gets a userid value in the SharedPreferences variable, it will directly take the user to next activity.
Make another class named SecondActivity.java
SecondActivity.java
package com.example.sqllogin; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class SecondActivity extends Activity { TextView txtuserid, txtnooftimes; SharedPreferences shp; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity); txtuserid = (TextView) findViewById(R.id.txtuserid); txtnooftimes = (TextView) findViewById(R.id.txtnooftimes); shp = this.getSharedPreferences("UserInfo", MODE_PRIVATE); String userid = shp.getString("UserId", "none"); int nooftimes = shp.getInt("no", 0); if (userid.equals("none") || userid.trim().equals("")) { Intent i = new Intent(SecondActivity.this, MainActivity.class); startActivity(i); finish(); } else { nooftimes++; SharedPreferences.Editor edit = shp.edit(); edit.putInt("no", nooftimes); edit.commit(); txtuserid.setText(userid); txtnooftimes .setText("Opened this app for " + String.valueOf(nooftimes) + " time(s). You made something awesome today. Keep going :) :)"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_logout) { LogOut(); return true; } return super.onOptionsItemSelected(item); } public void LogOut() { SharedPreferences.Editor edit = shp.edit(); edit.putString("UserId", ""); edit.commit(); Intent intent = new Intent(SecondActivity.this, MainActivity.class); startActivity(intent); this.finish(); } }
When use the app again after logging in once it will show the number of times you have opened the app.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sqllogin" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:screenOrientation="portrait" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:screenOrientation="portrait" android:label="@string/app_name" /> </application> </manifest>
Instagram like Login screen Android Studio.
Thank You.
Pingback: Connect Android to MS SQL Database. • ParallelCodes();
Connection Class???
Pingback: Android Mysql Login - Login Portal
Pingback: Android Login Application With Mssql Server Using Jdbc - Login Portal
Pingback: Android Login Authentication With Remote Database - Login Portal
Pingback: Android Login And Registration With Sqlite - Login Portal
Pingback: Android Studio Login And Register - Login Portal
Pingback: Android Login Form Example - Login Portal
Pingback: ➤ Iniciar Sesion And Registration Form In Android Using Sharedpreferences 【Iniciar Sesion】
Pingback: Https android studio login activity database Portal Guide Instructions Help - centtip.com
Pingback: Sample Code For Login Page In Android - Login Portal
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
LogOut();
return true;
main and action_logout not defined anywhere