What's new

Closed Log in Sign Up without database on android app

Status
Not open for further replies.

Maryrose143

Forum Expert
Elite
Joined
Dec 23, 2018
Posts
3,560
Solutions
1
Reaction
17,472
Points
2,753
Add an Login and registration layout without database using aide apk :)

Code



main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="You do not have permission to view the full content of this post. Log in or register now."
xmlns:tools="You do not have permission to view the full content of this post. Log in or register now."
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:text="hello friends"/>

</RelativeLayout>


activity_login.xml


<RelativeLayout xmlns:android="You do not have permission to view the full content of this post. Log in or register now."
xmlns:tools="You do not have permission to view the full content of this post. Log in or register now."
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:id="@+id/login_email"
android:hint="Email Address" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_below="@+id/login_email"
android:layout_alignStart="@+id/login_email"
android:layout_marginTop="58dp"
android:id="@+id/login_password"
android:hint="Password" />

<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/login_password"
android:layout_alignParentStart="true"
android:layout_marginStart="45dp"
android:layout_marginTop="48dp"
android:id="@+id/login_button" />

<Button
android:text="Register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/login_button"
android:layout_alignParentEnd="true"
android:layout_marginEnd="34dp"
android:id="@+id/register_button" />
</RelativeLayout>


activity_register

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="You do not have permission to view the full content of this post. Log in or register now."
xmlns:tools="You do not have permission to view the full content of this post. Log in or register now."
android:id="@+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:id="@+id/Name"
android:hint="Name" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:layout_below="@+id/Name"
android:layout_alignStart="@+id/Name"
android:layout_marginTop="33dp"
android:id="@+id/Email"
android:hint="Email" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/Email"
android:id="@+id/Password"
android:hint="Password" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_below="@+id/Password"
android:layout_alignStart="@+id/Password"
android:layout_marginTop="40dp"
android:id="@+id/confirm_password"
android:hint="confirm password" />

<Button
android:text="Register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/confirm_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:id="@+id/register" />
</RelativeLayout>



MainActivity.java

import android.app.*;
import android.os.*;
import android.content.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{

Intent login;


Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}
}


LoginActivity.java

import android.app.*;
import android.os.*;
import android.widget.*;
import android.content.*;
import android.view.*;

public class LoginActivity extends Activity
{
Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final SharedPreferences sharedPreferences=getSharedPreferences("USER_CREDENTIALS",MODE_PRIVATE);
final Boolean isloggedin=sharedPreferences.getBoolean("ISLOGGEDIN",false);
if(isloggedin)
{
Intent main = new Intent(LoginActivity.this, MainActivity.class);
startActivity(main);
}
final String required_email=sharedPreferences.getString("EMAIL","DEFAULT_EMAIL");
final String required_password=sharedPreferences.getString("PASSWORD","DEFAULT_PASSWORD");
final EditText email_field=(EditText)findViewById(R.id.login_email);
final EditText password_field=(EditText)findViewById(R.id.login_password);
Button login=(Button)findViewById(R.id.login_button);
Button register=(Button)findViewById(R.id.register_button);
login.setOnClickListener(new View.OnClickListener() {
Override
public void onClick(View v) {
String email=email_field.getText().toString();
String password=password_field.getText().toString();
if(email.equals(required_email)&&password.equals(required_password)) {
sharedPreferences.edit().putBoolean("ISLOGGEDIN",false).commit();
Intent main = new Intent(LoginActivity.this, MainActivity.class);
startActivity(main);
}
else
{
Toast.makeText(LoginActivity.this,"Email address or password is incorrect",Toast.LENGTH_LONG).show();
}
}
});
register.setOnClickListener(new View.OnClickListener() {
Override
public void onClick(View v) {
Intent register=new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(register);
finish();
}
});
}



}

RegisterActivity.java

import android.os.*;
import android.app.*;
import android.content.*;
import android.widget.*;
import android.view.*;

public class RegisterActivity extends Activity
{
Intent next_activity;


Override
protected void onCreate(Bundle savedInstanceState)
{


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
next_activity=new Intent(RegisterActivity.this,LoginActivity.class);
final EditText name_field=(EditText)findViewById(R.id.Name);
final EditText email_field=(EditText)findViewById(R.id.Email);
final EditText password_field=(EditText)findViewById(R.id.Password);
final EditText confirm_password_field=(EditText)findViewById(R.id.confirm_password);
Button registerbutton=(Button)findViewById(R.id.register);
registerbutton.setOnClickListener(new View.OnClickListener() {
Override
public void onClick(View v) {


String name=name_field.getText().toString();
String email_id=email_field.getText().toString();
String password_1=password_field.getText().toString();
String password_2=confirm_password_field.getText().toString();
if(password_1.equals(password_2))
{
//your code to register the user like an entry in the database
SharedPreferences sharedPreferences=getSharedPreferences("USER_CREDENTIALS",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("NAME",name);
editor.putString("EMAIL",email_id);
editor.putString("PASSWORD",password_1);
editor.putBoolean("ISLOGGEDIN",true);
editor.commit();
startActivity(next_activity);

Toast.makeText(RegisterActivity.this,"Registration Sucess",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(RegisterActivity.this,"Passwords don't match",Toast.LENGTH_LONG).show();
}

}
});
}
}

AndroidManifest.xml

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="Style/AppTheme"
android:resizeableActivity = "true">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".LoginActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity"></activity>
</application>

</manifest>
 
Status
Not open for further replies.
Back
Top