Skip to content

Instantly share code, notes, and snippets.

@nivir
Created October 22, 2013 23:21
Show Gist options
  • Save nivir/7109903 to your computer and use it in GitHub Desktop.
Save nivir/7109903 to your computer and use it in GitHub Desktop.
Android app Login functionality
package com.login.dplanner;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LogIn extends Activity {
EditText usernameEditText, passwordEditText;
Button login, login_cancel;
TextView registration;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
usernameEditText = (EditText) findViewById(R.id.UserName);
passwordEditText = (EditText) findViewById(R.id.PasswordName);
login = (Button) findViewById(R.id.loginButton);
login_cancel = (Button) findViewById(R.id.loginClearButton);
// Login Button Listener
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* Normal Condition try this
*/
// Intent i = new Intent(LogIn.this, MainPanel.class);
// startActivity(i);
// Toast.makeText(LogIn.this, "Login Successful",
// Toast.LENGTH_SHORT).show();
/**
* After Server part implementation following code will be taken
* away. This loging part will be done at the end part of development.
*/
if (loginCheck()) {
launchMainPanel();
Toast.makeText(LogIn.this, "Login Successful",
Toast.LENGTH_SHORT).show();
finish();
}
}
});
// Registration Button Listener
registration = (TextView) findViewById(R.id.RegistrationButton);
registration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i1 = new Intent(LogIn.this, Registration.class);
startActivity(i1);
Toast.makeText(LogIn.this, "Registration", Toast.LENGTH_SHORT)
.show();
}
});
// Cancel Button Listener
login_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usernameEditText.setText("");
passwordEditText.setText("");
Toast.makeText(LogIn.this, "Clear Text", Toast.LENGTH_SHORT)
.show();
}
});
}
private boolean loginCheck() {
String user_name_email = this.usernameEditText.getText().toString()
.trim();
String password = this.passwordEditText.getText().toString().trim();
if (isValidEmailAddress(user_name_email)) {
if (isValidUser(user_name_email, password))
return true;
else
return false;
} else
return false;
}
private void launchMainPanel() {
Intent i = new Intent(this, MainPanel.class);
startActivity(i);
}
private boolean isValidEmailAddress(String emailAddress) {
/**
* if mail address is NULL string
*/
if (TextUtils.isEmpty(emailAddress)) {
Toast.makeText(this, "Empty username", Toast.LENGTH_SHORT).show();
usernameShake();
return false;
}
// if mail address does NOT contain "@"
if (!emailAddress.contains("@")) {
Toast.makeText(this, "Invalid username", Toast.LENGTH_SHORT).show();
usernameShake();
return false;
}
/**
* if mail address does NOT contain "."
*/
if (!emailAddress.contains(".")) {
Toast.makeText(this, "Invalid username", Toast.LENGTH_SHORT).show();
usernameShake();
return false;
}
/**
* if number of characters after "." < 2
*/
if (numberOfCharsAfterDot(emailAddress) == false) {
Toast.makeText(this, "Invalid username", Toast.LENGTH_SHORT).show();
usernameShake();
return false;
}
return true;
}
private boolean isValidUser(String user_name_email, String password) {
ArrayList<NameValuePair> loginParameters = new ArrayList<NameValuePair>();
loginParameters.add(new BasicNameValuePair("username", usernameEditText
.getText().toString()));
loginParameters.add(new BasicNameValuePair("password", passwordEditText
.getText().toString()));
// String valid = "1";
String response = null;
try {
response = HttpConnector.executeHttpPost(
"http://10.0.2.2/d_planner_project/d_planner_login.php",
loginParameters);
String res = response.toString();
// res = res.trim();
res = res.replaceAll("\\s+", "");
// error.setText(res);
if (res.equals("1")) {
Toast.makeText(this, "loading...", Toast.LENGTH_SHORT).show();
return true;
} else {
usernameShake();
passwordShake();
Toast.makeText(this, "username or password incorrect",
Toast.LENGTH_SHORT).show();
return false;
}
} catch (Exception e) {
return false;
}
}
private void usernameShake() {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
findViewById(R.id.UserName).startAnimation(shake);
}
private void passwordShake() {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
findViewById(R.id.PasswordName).startAnimation(shake);
}
private boolean numberOfCharsAfterDot(String emailAddress) {
int a, b, c;
char ch = '.';
a = emailAddress.indexOf(ch);
b = emailAddress.length();
c = b - a;
if (c == 3 || c == 4) { // here to exactly to confirm the number of
// characters after "." = 2 OR 3
return true;
} else {
return false;
}
}
/**
* Create and menu option button in the bottom of the screen
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.activity_login, menu);
menu.add("Registration");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Registration", Toast.LENGTH_LONG).show();
Intent i1 = new Intent(LogIn.this, Registration.class);
startActivity(i1);
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment