You are on page 1of 14

Create a new project in Eclipse File > New > Android Application Project.

Fill in the
details and name your project ParseLoginTutorial.

Application Name : ParseLoginTutorial

Project Name : ParseLoginTutorial

Package Name : com.androidbegin.parselogintutorial

Open your MainActivity.java and paste the following code.

MainActivity.java
1 package com.androidbegin.parselogintutorial;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6
7 import com.parse.ParseAnonymousUtils;
8 import com.parse.ParseUser;
9
1 public class MainActivity extends Activity {
0
1 public void onCreate(Bundle savedInstanceState) {
1 super.onCreate(savedInstanceState);
1
2 // Determine whether the current user is an anonymous user
1 if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
3 // If user is anonymous, send the user to LoginSignupActivity.class
1 Intent intent = new Intent(MainActivity.this,
4 LoginSignupActivity.class);
1 startActivity(intent);
5 finish();
1 } else {
6 // If current user is NOT anonymous user
1 // Get current user data from Parse.com
7 ParseUser currentUser = ParseUser.getCurrentUser();
1 if (currentUser != null) {
8 // Send logged in users to Welcome.class
1 Intent intent = new Intent(MainActivity.this, Welcome.class);
9 startActivity(intent);
2 finish();
0 } else {
2 // Send user to LoginSignupActivity.class
1 Intent intent = new Intent(MainActivity.this,
2 LoginSignupActivity.class);
2 startActivity(intent);
2 finish();
3 }
2 }
4
2 }
5 }
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
In this activity, we try to determine whether the current user is an anonymous user and if
the user has previously signed up and logged into the application, the user will be
automatically converted into a regular user. Every Signup and login methods will cache
the user data on disk. Caching users data on disk will prevent a regular user to log in
every time they open your application. To clear a regular user cached data will require
calling ParseUser. logOut (); and an anonymous users will be sent to the
LoginSignupActivity.java.

Next, create an authentication class to identify your app. Go to File > New > Class and
name it ParseApplication.java. Select your package
named com.androidbegin.parselogintutorial and click Finish.

Open your ParseApplication.java and paste the following code.

ParseApplication.java
1 package com.androidbegin.parselogintutorial;
2
3 import com.parse.Parse;
4 import com.parse.ParseACL;
5
6 import com.parse.ParseUser;
7
8 import android.app.Application;
9
1 public class ParseApplication extends Application {
0
1
1
1
2
1
3
1
4
1
5
1 @Override
6 public void onCreate() {
1 super.onCreate();
7
1 // Add your initialization code here
8 Parse.initialize(this, YOUR_APPLICATION_ID, YOUR_CLIENT_KEY);
1
9 ParseUser.enableAutomaticUser();
2 ParseACL defaultACL = new ParseACL();
0
2 // If you would like all objects to be private by default, remove this
1 // line.
2 defaultACL.setPublicReadAccess(true);
2
2 ParseACL.setDefaultACL(defaultACL, true);
3 }
2
4 }
2
5
2
6
2
7
2
8
2
9
Next, create a login and signup activity. Go to File > New > Class and name
it LoginSignupActivity.java. Select your package
named com.androidbegin.parselogintutorial and click Finish.

Open your LoginSignupActivity.java and paste the following code.

LoginSignupActivity.java
1 package com.androidbegin.parselogintutorial;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.View.OnClickListener;
8 import android.widget.Button;
9 import android.widget.EditText;
10 import android.widget.Toast;
11
12 import com.parse.LogInCallback;
13 import com.parse.ParseException;
14 import com.parse.ParseUser;
15 import com.parse.SignUpCallback;
16
17 public class LoginSignupActivity extends Activity {
18 // Declare Variables
19 Button loginbutton;
20 Button signup;
21 String usernametxt;
22 String passwordtxt;
23 EditText password;
24 EditText username;
25
26 /** Called when the activity is first created. */
27 public void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 // Get the view from main.xml
30 setContentView(R.layout.main);
31 // Locate EditTexts in main.xml
32 username = (EditText) findViewById(R.id.username);
33 password = (EditText) findViewById(R.id.password);
34
35 // Locate Buttons in main.xml
36 loginbutton = (Button) findViewById(R.id.login);
37 signup = (Button) findViewById(R.id.signup);
38
39 // Login Button Click Listener
40 loginbutton.setOnClickListener(new OnClickListener() {
41
42 public void onClick(View arg0) {
43 // Retrieve the text entered from the EditText
44 usernametxt = username.getText().toString();
45 passwordtxt = password.getText().toString();
46
47 // Send data to Parse.com for verification
48 ParseUser.logInInBackground(usernametxt, passwordtxt,
49 new LogInCallback() {
50 public void done(ParseUser user,
51 ParseException e) {
52 if (user != null) {
53 // If user exist and
54 authenticated, send user to Welcome.class
55 Intent intent = new Intent(
56
57 LoginSignupActivity.this,
58
59
Welcome.class);
60
startActivity(intent);
61
62
63 Toast.makeText(getApplicationContext(),
64 "Successful
65 ly Logged in",
66
67 Toast.LENGTH_LONG).show();
68 finish();
69 } else {
70 Toast.makeText(
71
72 getApplicationContext(),
"No such
user exist, please signup",

73 Toast.LENGTH_LONG).show();
74 }
75 }
76 });
77 }
78 });
79 // Sign up Button Click Listener
80 signup.setOnClickListener(new OnClickListener() {
81
82 public void onClick(View arg0) {
83 // Retrieve the text entered from the EditText
84 usernametxt = username.getText().toString();
85 passwordtxt = password.getText().toString();
86
87 // Force user to fill up the form
88 if (usernametxt.equals("") && passwordtxt.equals("")) {
89 Toast.makeText(getApplicationContext(),
90 "Please complete the sign up form",
91 Toast.LENGTH_LONG).show();
92
93 } else {
94 // Save new user data into Parse.com Data Storage
95 ParseUser user = new ParseUser();
96 user.setUsername(usernametxt);
97 user.setPassword(passwordtxt);
98 user.signUpInBackground(new SignUpCallback() {
99 public void done(ParseException e) {
10 if (e == null) {
0 // Show a simple Toast message
10 upon successful registration
1
10 Toast.makeText(getApplicationContext(),
2 "Successfully
10 Signed up, please log in.",
3
10 Toast.LENGTH_LONG).show();
4 } else {
10
5
Toast.makeText(getApplicationContext(),
10
"Sign up Error",
6
Toast.LENGTH_LONG)
10
.show();
7
}
10
}
8
});
10
}
9
11
}
0
});

}
}
In this activity we created two buttons that allow users to either login or signup. Both
username and password entered by a user are captured using an EditText. On login
button click will sent the username and password to Parse.com for verification and if the
user exists will show a toast message and display a welcome screen. On signup button
click will check to make sure both of the username and password are not empty and on
successful signup will store both username and password in Parse.com.

Next, create an XML graphical layout for your login and signup activity. Go
to res > layout > Right Click on layout > New > Android XML File

Name your new XML file loginsignup.xml and paste the following code.

loginsignup.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical"
6 android:padding="10dip" >
7
8 <TextView
9 android:id="@+id/txtusername"
1 android:layout_width="fill_parent"
0 android:layout_height="wrap_content"
1 android:text="@string/Username" />
1
1 <EditText
2 android:id="@+id/username"
1 android:layout_width="fill_parent"
3 android:layout_height="wrap_content"
1 android:layout_below="@+id/txtusername"
4 android:inputType="text" />
1
5 <TextView
1 android:id="@+id/txtpassword"
6 android:layout_width="fill_parent"
1 android:layout_height="wrap_content"
7 android:layout_below="@+id/username"
1 android:text="@string/Password" />
8
1 <EditText
9 android:id="@+id/password"
2 android:layout_width="fill_parent"
0 android:layout_height="wrap_content"
2 android:layout_below="@+id/txtpassword"
1 android:inputType="textPassword" />
2
2 <Button
2 android:id="@+id/login"
3 android:layout_width="fill_parent"
2 android:layout_height="wrap_content"
4 android:layout_below="@+id/password"
2 android:text="@string/LoginBtn" />
5
2 <Button
6 android:id="@+id/signup"
2 android:layout_width="fill_parent"
7 android:layout_height="wrap_content"
2 android:layout_below="@+id/login"
8 android:text="@string/SignupBtn" />
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9 </RelativeLayout>
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
Output:
Next, create an activity to show a welcome screen. Go to File > New > Class and name
it Welcome.java. Select your package
named com.androidbegin.parseloginsignuptutorial and click Finish.

Open your Welcome.java and paste the following code.

Welcome.java
1 package com.androidbegin.parselogintutorial;
2
3 import com.parse.ParseUser;
4
5 import android.app.Activity;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.Button;
1 import android.widget.TextView;
0
1 public class Welcome extends Activity {
1
1 // Declare Variable
2 Button logout;
1
3 @Override
1 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
1 // Get the view from singleitemview.xml
5 setContentView(R.layout.welcome);
1
6 // Retrieve current user from Parse.com
1 ParseUser currentUser = ParseUser.getCurrentUser();
7
1 // Convert currentUser into String
8 String struser = currentUser.getUsername().toString();
1
9 // Locate TextView in welcome.xml
2 TextView txtuser = (TextView) findViewById(R.id.txtuser);
0
2 // Set the currentUser String into TextView
1 txtuser.setText("You are logged in as " + struser);
2
2 // Locate Button in welcome.xml
2 logout = (Button) findViewById(R.id.logout);
3
2 // Logout Button Click Listener
4 logout.setOnClickListener(new OnClickListener() {
2
5 public void onClick(View arg0) {
2 // Logout current user
6 ParseUser.logOut();
2 finish();
7 }
2 });
8 }
2 }
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
In this activity, we have created a Log Out button and a simple welcome text message to
show with the current username. The Log Out button will clear the current logged in
user cache data on disk. We used ParseUser. getCurrentUser() and called
currentUser.getUsername() to retrieve the current logged username. Then display the
username on a textview together with the welcome message.

Next, create an XML graphical layout for your welcome screen. Go


to res > layout > Right Click on layout > New > Android XML File

Name your new XML file welcome.xml and paste the following code.

welcome.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:id="@+id/txtwelcome"
9 android:layout_width="wrap_content"
1 android:layout_height="wrap_content"
0 android:layout_centerHorizontal="true"
1 android:text="@string/Welcome"
1 android:paddingTop="50dp"
1 android:textSize="25sp" />
2
1 <TextView
3 android:id="@+id/txtuser"
1 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
1 android:layout_below="@+id/txtwelcome"
5 android:layout_centerHorizontal="true" />
1
6 <Button
1 android:id="@+id/logout"
7 android:layout_width="wrap_content"
1 android:layout_height="wrap_content"
8 android:layout_centerInParent="true"
1 android:text="@string/LogoutBtn" />
9
2 </RelativeLayout>
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
Next, change the application name and texts. Open your strings.xml in your res >
values folder and paste the following code.

strings.xml
1
2
3 <?xml version="1.0" encoding="utf-8"?>
4 <resources>
5
6 <string name="hello">Hello World, ParseStarterProjectActivity!</string>
7 <string name="app_name">Parse.com Login Tutorial</string>
8 <string name="Username">Username</string>
9 <string name="Password">Password</string>
1 <string name="LoginBtn">Login</string>
0 <string name="SignupBtn">Sign Up</string>
1 <string name="LogoutBtn">Log Out</string>
1 <string name="Welcome">Welcome!</string>
1
2 </resources>
1
3
In your AndroidManifest.xml, we need to declare an activity and permissions to allow
the application to connect to the Internet and ParseApplication below your application
opening. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.androidbegin.parselogintutorial"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="4"
9 android:targetSdkVersion="15" />
1
0 <uses-permission android:name="android.permission.INTERNET" />
1
1 <application
1 android:name="ParseApplication"
2
1
3
1
4
1
5
1
6
1
7
1
android:icon="@drawable/ic_launcher"
8
android:label="@string/app_name" >
1
<activity
9
android:name=".MainActivity"
2
android:label="@string/app_name" >
0
<intent-filter>
2
<action android:name="android.intent.action.MAIN" />
1
2
<category android:name="android.intent.category.LAUNCHER" />
2
</intent-filter>
2
</activity>
3
<activity android:name=".LoginSignupActivity" >
2
</activity>
4
<activity android:name=".Welcome" >
2
</activity>
5
</application>
2
6
</manifest>
2
7
2
8
2
9
3
0
3
1
3
2
Return to Parse.com dashboard, select your app and click on Data Browser.
There are many other properties that you can capture from users, such as email
address, address, phone number and etc. However, this tutorial only shows you a basic
idea on how to manage user accounts. At the meantime, you can visit Parse.com for
learn more regarding user account management.
Source https://www.parse.com/docs/android_guide#users

Output:
Source Code

You might also like