Practical 1b
Aim : To Design a registration and login page with user credentials and profile image display. Use
intent to pass data between apps and incorporate android resources like color, theme, string,
drawable, dimension and image.
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="primaryColor">#6200EE</color>
<color name="buttonColor">#FF5722</color>
<color name="backgroundColor">#F5F5F5</color>
</resources>
dimens.xml
<resources>
<dimen name="spacing_medium">16dp</dimen>
<dimen name="spacing_large">50dp</dimen>
</resources>
string.xml
<resources>
<string name="app_name">RegistrationLoginApp</string>
<string name="login_button">Login</string>
<string name="register_button">Register</string>
<string name="username_hint">Username</string>
<string name="password_hint">Password</string>
<string name="profile_image">Profile Image</string>
</resources>
activity_registration.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:background="@color/backgroundColor">
<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_large"
android:hint="@string/username_hint"
android:minHeight="48dp" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_large"
android:hint="@string/password_hint"
android:inputType="textPassword"
android:minHeight="48dp" />
<Button
android:id="@+id/registerButton"
android:layout_width="match_parent"
android:layout_height="48dp"
android:backgroundTint="@color/buttonColor"
android:text="@string/register_button" />
</LinearLayout>
RegistrationActivity.kt
package com.example.forms
import android.os.Bundle
import android.content.Intent
import android.widget.Button
import android.widget.EditText
import android.content.Context;
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class RegistrationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration)
val usernameEditText = findViewById<EditText>(R.id.usernameEditText)
val passwordEditText = findViewById<EditText>(R.id.passwordEditText)
val registerButton = findViewById<Button>(R.id.registerButton)
val sharedPreferences = getSharedPreferences("user_data", Context.MODE_PRIVATE)
registerButton.setOnClickListener {
val username = usernameEditText.text.toString()
val password = passwordEditText.text.toString()
// Save credentials in SharedPreferences
sharedPreferences.edit().apply {
putString("username", username)
putString("password", password)
apply()
}
// Start login activity
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}
}
}
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:background="@color/backgroundColor">
<EditText
android:id="@+id/loginUsernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_large"
android:hint="@string/username_hint"
android:minHeight="48dp" />
<EditText
android:id="@+id/loginPasswordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_large"
android:hint="@string/password_hint"
android:inputType="textPassword"
android:minHeight="48dp" />
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="48dp"
android:backgroundTint="@color/buttonColor"
android:text="@string/login_button" />
<ImageView
android:id="@+id/profileImageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="@dimen/spacing_medium"
android:layout_gravity="center"
android:src="@drawable/profile_image"
android:visibility="gone"/>
</LinearLayout>
LoginActivity.kt
package com.example.forms
import android.os.Bundle
import android.widget.EditText
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import android.content.Context;
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val loginUsernameEditText = findViewById<EditText>(R.id.loginUsernameEditText)
val loginPasswordEditText = findViewById<EditText>(R.id.loginPasswordEditText)
val loginButton = findViewById<Button>(R.id.loginButton)
val profileImageView = findViewById<ImageView>(R.id.profileImageView)
val sharedPreferences = getSharedPreferences("user_data", Context.MODE_PRIVATE)
loginButton.setOnClickListener {
val username = loginUsernameEditText.text.toString()
val password = loginPasswordEditText.text.toString()
// Retrieve saved credentials
val savedUsername = sharedPreferences.getString("username", null)
val savedPassword = sharedPreferences.getString("password", null)
if (username == savedUsername && password == savedPassword) {
profileImageView.visibility = View.VISIBLE
} else {
Toast.makeText(this, "Invalid credentials", Toast.LENGTH_SHORT).show()
}
}
}
}
Conclusion : This practical has been done successfully.