How to create Android user registration using shared preferences with string



shared preferences with string


MainActivity.java



package com.example.admin.test;

import android.content.Intent;
import android.content.SharedPreferences;
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;


public class MainActivity extends AppCompatActivity {
    SharedPreferences pref;
    Boolean isFirstRun;
    SharedPreferences.Editor editor;
    String firstName;
    String lastName;
    String email;
    String phoneNumber;
    String userName;
    String password;
    String prefFirstName,prefLastName,prefEmail,prefPhoneNumber,prefUserName,prefPassword;


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

        pref=getApplicationContext().getSharedPreferences("Mypref", MODE_PRIVATE);
        editor=pref.edit();

        //checking app for running first time
        isFirstRun=getSharedPreferences("preferences", MODE_PRIVATE).getBoolean("isfirstrun", true);




        Button signUp= (Button) findViewById(R.id.sign_up);
        final Button logIn=(Button) findViewById(R.id.log_in);




        //signUp button working
        signUp.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {





                    firstName=((EditText) findViewById(R.id.first_name)).getText().toString();
                    lastName=((EditText) findViewById(R.id.last_name)).getText().toString();
                    userName=((EditText) findViewById(R.id.user_name)).getText().toString();
                    password=((EditText) findViewById(R.id.pass)).getText().toString();
                    email=((EditText) findViewById(R.id.email)).getText().toString();
                    phoneNumber=((EditText) findViewById(R.id.phone_number)).getText().toString();

                if(isFirstRun)
                {
                    editor.putString("first_name", firstName);
                    editor.putString("last_name", lastName);
                    editor.putString("user_name", userName);
                    editor.putString("pass", password);
                    editor.putString("email", email);
                    editor.putString("phone_number", phoneNumber);
                    editor.commit();
                    getSharedPreferences("preferences", MODE_PRIVATE).edit().putBoolean("isfirstrun", false).commit();
                }

                else {
                    prefFirstName = pref.getString("first_name", null);
                    prefLastName = pref.getString("last_name", null);
                    prefUserName = pref.getString("user_name", null);
                    prefPassword = pref.getString("pass", null);
                    prefEmail = pref.getString("email", null);
                    prefPhoneNumber = pref.getString("phone_number", null);

                    prefFirstName += "," + firstName;
                    prefLastName += "," + lastName;
                    prefUserName += "," + userName;
                    prefPassword += "," + password;
                    prefEmail += "," + email;
                    prefPhoneNumber += "," + phoneNumber;


                    editor.putString("first_name", prefFirstName);
                    editor.putString("last_name", prefLastName);
                    editor.putString("user_name", prefUserName);
                    editor.putString("pass", prefPassword);
                    editor.putString("email", prefEmail);
                    editor.putString("phone_number", prefPhoneNumber);
                    editor.commit();
                }


             Intent i=new Intent(getApplicationContext(),LogIn.class);
                startActivity(i);
                finish();
            }
        });

        //logIn button working
        logIn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(getApplicationContext(),LogIn.class);
                startActivity(i);
                finish();
            }
        });

    }
}



Login.java


package com.example.admin.test;

import android.content.Intent;
import android.content.SharedPreferences;
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.TextView;
import android.widget.Toast;

import java.sql.SQLOutput;

public class LogIn extends AppCompatActivity {
    String userName;
    String password;
    SharedPreferences pref;

    String firstName[];
    String lastName[];
    String userNameArray[];
    String passwordArray[];
    String email[];
    String phoneNumber[];

    int i,flag=0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        pref=getSharedPreferences("Mypref",MODE_PRIVATE);

        //sharedpref data
        String prefFirstName=pref.getString("first_name",null);
        String prefLastName=pref.getString("last_name",null);
        String prefUserName=pref.getString("user_name",null);
        String prefPassword=pref.getString("pass",null);
        String prefEmail=pref.getString("email",null);
        String prefPhoneNumber=pref.getString("phone_number", null);

        //splitting
        firstName=prefFirstName.split(",");
        lastName=prefLastName.split(",");
        userNameArray=prefUserName.split(",");
        passwordArray=prefPassword.split(",");
        email=prefEmail.split(",");
        phoneNumber=prefPhoneNumber.split(",");



       Button logIn= (Button) findViewById(R.id.sign_in);

        //log in button working
        logIn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                userName=((EditText) findViewById(R.id.user_name)).getText().toString();
                password=((EditText) findViewById(R.id.password)).getText().toString();

                for(i=0;i<firstName.length;i++)
                {
                    if(userName.equalsIgnoreCase(userNameArray[i]))
                    {
                        Intent welcome=new Intent(getApplicationContext(),Welcome.class);
                        welcome.putExtra("first_name",firstName[i]);
                        welcome.putExtra("last_name",lastName[i]);
                        welcome.putExtra("user_name",userNameArray[i]);
                        welcome.putExtra("password",passwordArray[i]);
                        welcome.putExtra("email",email[i]);
                        welcome.putExtra("phone_number",phoneNumber[i]);
                        startActivity(welcome);
                        finish();
                    }
                    else
                    {
                        flag=1;
                    }
                }


                if (flag==1)
                {
                    Toast.makeText(getApplicationContext(), "you are not registered", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }
}

Welcome.java


package com.example.admin.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Welcome extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        Bundle i=getIntent().getExtras();

        String firstName=i.getString("first_name");
        String lastName=i.getString("last_name");
        String userName=i.getString("user_name");
        String password=i.getString("password");
        String email=i.getString("email");
        String phoneNumber=i.getString("phone_number");

        //initializing textview
        TextView firstNameText= (TextView) findViewById(R.id.first_name_get);
        TextView lastNameText= (TextView) findViewById(R.id.last_name_get);
        TextView userNameText= (TextView) findViewById(R.id.user_name_get);
        TextView passwordText= (TextView) findViewById(R.id.password_get);
        TextView emailText= (TextView) findViewById(R.id.email_get);
        TextView phoneNumberText= (TextView) findViewById(R.id.phone_number_get);

        //putting values in textviews
        firstNameText.setText(firstName);
        lastNameText.setText(lastName);
        userNameText.setText(userName);
        passwordText.setText(password);
        emailText.setText(email);
        phoneNumberText.setText(phoneNumber);

    }
}




Layouts

activity_log_in.xml

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Login"
    android:gravity="center_horizontal"
    android:textSize="50dp"/>

    <EditText
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="User name"
        android:layout_marginTop="30dp"
        />
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_gravity="right"
        android:layout_height="wrap_content"
        android:id="@+id/sign_in"
        android:text="sign in"/>

</LinearLayout>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Registration"
    android:gravity="center_horizontal"
    android:textSize="50dp"/>
    <EditText
        android:id="@+id/first_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="First Name"
        android:layout_marginTop="30dp"/>
    <EditText
        android:id="@+id/last_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Last Name"
        android:layout_marginTop="10dp"/>
    <EditText
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="User name"
        android:layout_marginTop="10dp"/>
    <EditText
        android:id="@+id/pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:layout_marginTop="10dp"/>
    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email"
        android:layout_marginTop="10dp"/>
    <EditText
        android:id="@+id/phone_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Phone number"
        android:layout_marginTop="10dp"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="10dp">
    <Button
        android:id="@+id/sign_up"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Sign up"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/log_in"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Log in"
        android:layout_weight="1" />

</LinearLayout>
</LinearLayout>


activity_welcome.xml

<LinearLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/first_name_get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/last_name_get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/user_name_get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/password_get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/email_get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/phone_number_get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Share on Google Plus

About Mayank Sharma

Mayadi is a knowlage source. You can learn, explore and play many things in a proper way. Mayadi Provides Best, Perfect and Quality Information. subscribe our youtube channel to be updated and and don't forget to take a look on our blog within every week, because we are here to upgrade your knowladge. We love to see you again and again and again...
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment