Login and Register using shared preference

Why use shared preference

The purpose of the shared preference is to help users save the state of their app for later use. It works for any primitive data type (like : Ints, booleans, longs, Strings, and Floats). In other words, it is pretty simple to work with.For this tutorial, the user will be able to save his/her login details and it will be remembered when next it is inputed so lets get started.

Setting up

First,Create a new project, call it “SharedPreferenceExample”. Now create another activity and call it “StarterActivity” making it the launcher activity.The code for the activity_starter.xml looks this way: note, the bolded area should be added by you.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StarterActivity"
android:background="@drawable/welcome">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/loginbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="40dp"
android:text="Login"
android:gravity="center"
android:textSize="20sp"
android:textStyle="italic"
android:textColor="@color/blue"
android:background="@drawable/welcome_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
android:id="@+id/registerbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:text="Register"
android:gravity="center"
android:textSize="20sp"
android:textStyle="italic"
android:textColor="@color/white"
android:background="@drawable/welcm_register"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/loginbtn" />

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/login_acct"
>

<EditText
android:id="@+id/Text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="User Name"
android:inputType="textPersonName"
android:layout_marginTop="350dp"
android:drawableLeft="@drawable/ic_baseline_account_circle_24"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>

<EditText
android:id="@+id/Text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/Text1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="20dp"
android:inputType="textPassword"
android:drawableLeft="@drawable/ic_baseline_lock_24"
android:hint="Password"/>

<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:layout_marginRight="40dp"
android:text="Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Text2" />

<TextView
android:id="@+id/registerMe"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:layout_marginRight="40dp"
android:background="@drawable/bluestroke"
android:text="Register"
android:gravity="center"
android:textIsSelectable="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/login"
app:rippleColor="#FFFFFF" />

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/create_acct">

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:hint="User Name"
android:inputType="textPersonName"
android:drawableLeft="@drawable/ic_baseline_account_circle_24"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="350dp"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@+id/editText1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:drawableLeft="@drawable/ic_baseline_lock_24"
android:hint="Password"
android:layout_margin="20dp"/>

<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_baseline_mail_outline_24"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintTop_toBottomOf="@+id/editText2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="20dp"/>

<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/editText3"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="20dp"
android:text="Register"/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="119dp"
android:text="Go back to "
android:textSize="20sp"
android:layout_marginTop="10dp"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/register" />


<TextView
android:id="@+id/back_to_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login "
android:textColor="@color/blue"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:textStyle="italic"
android:layout_marginLeft="10dp"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/register" />

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:textSize="25sp"
android:padding="30dp"
android:layout_marginTop="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
class Register : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.register)

val userName = findViewById<EditText>(R.id.user_name)
val password = findViewById<EditText>(R.id.password)
val email = findViewById<EditText>(R.id.email)
val btnRegister = findViewById<Button>(R.id.register)
val backToLogin = findViewById<TextView>(R.id.back_to_login)

btnRegister.setOnClickListener {
val preferences: SharedPreferences = getSharedPreferences("MYPREFS", MODE_PRIVATE)
val newUser: String = userName.getText().toString()
val newPassword: String = password.getText().toString()
val newEmail: String = email.getText().toString()
val editor: SharedPreferences.Editor = preferences.edit()


editor.putString(newUser + newPassword + "data",newUser + "\n" + newEmail)
editor.commit()
}
backToLogin.setOnClickListener {
val intent = Intent(this,MainActivity::class.java)
startActivity(intent)

}
}
}
// Import where necessary
val preferences: SharedPreferences = getSharedPreferences("MYPREFS", MODE_PRIVATE)
val editor: SharedPreferences.Editor = preferences.edit()
editor.putString(newUser, newUser)
editor.commit()
editor.putString(newPassword, newPassword)
editor.commit()
editor.putString(newUser + newPassword + "data", """ $newUser $newEmail """.trimIndent())
editor.commit()
class DisplayScreen: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.display_info)

val preferences: SharedPreferences = getSharedPreferences("MYPREFS", MODE_PRIVATE)
val display: String? = preferences.getString("display", "")
val displayInfo = findViewById<TextView>(R.id.display)
displayInfo.text = display
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val etName = findViewById<EditText>(R.id.text_username)
val etPassword = findViewById<EditText>(R.id.text_password)
val btnLogin = findViewById<Button>(R.id.login)
val btnRegister = findViewById<TextView>(R.id.registerMe)

btnRegister.setOnClickListener {
Toast.makeText(this,"done",Toast.LENGTH_LONG).show()
val intent = Intent(this,Register::class.java)
startActivity(intent)
}

btnLogin.setOnClickListener {

val user: String = etName.text.toString()
val password: String = etPassword.text.toString()
val preferences: SharedPreferences = getSharedPreferences("MYPREFS", MODE_PRIVATE)
val userDetails: String? = preferences.getString(user + password + "data", "Username or Password is Incorrect.")
val editor: SharedPreferences.Editor = preferences.edit()
editor.putString("display", userDetails)
editor.commit()
val displayScreen = Intent(this@MainActivity, DisplayScreen::class.java)
startActivity(displayScreen)
}
btnRegister.setOnClickListener {
val intent = Intent(this, Register::class.java)
startActivity(intent)
}

}

}
class StarterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_starter)

val lgnBttn = findViewById<TextView>(R.id.loginbtn)
val registerBttn = findViewById<TextView>(R.id.registerbtn)

lgnBttn.setOnClickListener {
val intent = Intent(this,MainActivity::class.java)
startActivity(intent)
}

registerBttn.setOnClickListener {
val intent = Intent(this,Register::class.java)
startActivity(intent)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sharedpreferenceexample"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SharedPreferenceExample">
<activity android:name=".StarterActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"> </activity>
<activity android:name=".DisplayScreen"> </activity>
<activity android:name=".Register"> </activity>
</application></manifest>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store