Last active
July 26, 2022 05:58
-
-
Save ankitchauhan20/23fdb282d55f5c92ffbc11b5d488bd55 to your computer and use it in GitHub Desktop.
Save Data User Input Data in Room Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout | |
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" | |
> | |
<data> | |
<variable | |
name="viewModel" | |
type="com.example.profilescreen.viewModel.UserDetailsViewModel" | |
/> | |
</data> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".view.activity.ProfileActivity"> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:id="@+id/topLayout" | |
android:layout_width="match_parent" | |
android:layout_height="250dp" | |
android:background="@color/ui_kit_rewards_gold_90" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintBottom_toTopOf="@id/scrollView" | |
> | |
<androidx.appcompat.widget.AppCompatImageView | |
android:layout_width="30dp" | |
android:layout_height="146dp" | |
android:src="@drawable/group_566" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
/> | |
<androidx.appcompat.widget.AppCompatImageView | |
android:layout_width="41dp" | |
android:layout_height="68dp" | |
android:src="@drawable/group" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintBottom_toBottomOf="parent" | |
/> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/p_information" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="50dp" | |
android:text="@string/personal_info" | |
android:fontFamily="sans-serif" | |
android:textStyle="bold" | |
android:textSize="14sp" | |
android:textColor="@color/black" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
/> | |
<androidx.appcompat.widget.AppCompatImageView | |
android:id="@+id/profile_image" | |
android:layout_width="86dp" | |
android:layout_height="86dp" | |
android:layout_marginTop="41dp" | |
android:src="@drawable/group_569" | |
app:civ_border_width="2dp" | |
app:civ_border_color="#FF000000" | |
android:layout_marginStart="24dp" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toStartOf="@+id/p_Name" | |
app:layout_constraintTop_toBottomOf="@id/p_information" | |
/> | |
<androidx.appcompat.widget.AppCompatImageView | |
android:layout_width="22dp" | |
android:layout_height="22dp" | |
android:src="@drawable/group_695" | |
app:layout_constraintBottom_toBottomOf="@id/profile_image" | |
app:layout_constraintStart_toStartOf="@id/profile_image" | |
app:layout_constraintEnd_toEndOf="@id/profile_image" | |
/> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/p_Name" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="17dp" | |
android:layout_marginStart="20dp" | |
android:fontFamily="sans-serif" | |
android:text="@string/profile_name" | |
android:textColor="@color/greens_house_green" | |
android:textSize="16sp" | |
android:textStyle="bold" | |
app:layout_constraintBottom_toTopOf="@id/p_rewards" | |
app:layout_constraintStart_toEndOf="@id/profile_image" | |
app:layout_constraintTop_toTopOf="@id/profile_image" /> | |
<TextView | |
android:id="@+id/p_rewards" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:fontFamily="sans-serif" | |
android:text="@string/gold_tier" | |
android:textColor="@color/ui_kit_rewards_gold" | |
android:textSize="18sp" | |
android:textStyle="bold" | |
app:layout_constraintStart_toStartOf="@id/p_Name" | |
app:layout_constraintTop_toBottomOf="@id/p_Name" | |
/> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
<androidx.core.widget.NestedScrollView | |
android:id="@+id/scrollView" | |
android:layout_width="match_parent" | |
android:fillViewport="true" | |
android:layout_height="0dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/topLayout"> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:id="@+id/bottomLayout" | |
android:layout_width="match_parent" | |
android:layout_height="573dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/topLayout"> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/f_name_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="50dp" | |
android:text="@string/first_name" | |
android:textColor="@color/black" | |
android:textSize="12sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.08" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<androidx.appcompat.widget.AppCompatEditText | |
android:id="@+id/et_firstName" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginHorizontal="24dp" | |
android:hint="@string/type" | |
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
android:textSize="15sp" | |
android:text="@={viewModel.firstName}" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="@id/f_name_text" | |
app:layout_constraintTop_toBottomOf="@id/f_name_text" /> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/l_name_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="20dp" | |
android:text="@string/last_name" | |
android:textColor="@color/black" | |
android:textSize="12sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.08" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@id/et_firstName" /> | |
<androidx.appcompat.widget.AppCompatEditText | |
android:id="@+id/et_lastName" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginHorizontal="24dp" | |
android:hint="@string/type" | |
android:text="@={viewModel.lastName}" | |
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
android:textSize="15sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="@id/l_name_text" | |
app:layout_constraintTop_toBottomOf="@id/l_name_text" /> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/mobile_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="20dp" | |
android:text="@string/mobile_number" | |
android:textAllCaps="true" | |
android:textColor="@color/black" | |
android:textSize="12sp" | |
app:layout_constraintStart_toStartOf="@id/et_Mobile" | |
app:layout_constraintTop_toBottomOf="@id/et_lastName" | |
/> | |
<androidx.appcompat.widget.AppCompatEditText | |
android:id="@+id/et_Mobile" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginHorizontal="24dp" | |
android:drawableEnd="@drawable/ui_24_edit_thick" | |
android:hint="@string/type" | |
android:inputType="phone" | |
android:maxLength="10" | |
android:text="@={viewModel.number}" | |
android:textSize="15sp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="@id/mobile_text" | |
app:layout_constraintTop_toBottomOf="@id/mobile_text" | |
/> | |
<androidx.appcompat.widget.AppCompatButton | |
android:id="@+id/submitBtn" | |
android:layout_width="match_parent" | |
android:layout_height="40dp" | |
android:layout_marginHorizontal="24dp" | |
android:background="@drawable/button_round_shape" | |
android:fontFamily="sans-serif" | |
android:onClick="@{() -> viewModel.onClick()}" | |
android:text="@string/submit" | |
android:textAllCaps="false" | |
android:textColor="@color/white" | |
android:textSize="12sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/et_Mobile" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</androidx.core.widget.NestedScrollView> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
package="com.example.profilescreen"> | |
<application | |
android:allowBackup="true" | |
android:dataExtractionRules="@xml/data_extraction_rules" | |
android:fullBackupContent="@xml/backup_rules" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/Theme.ProfileScreen" | |
tools:targetApi="31"> | |
<activity | |
android:name="com.example.profilescreen.view.activity.DataActivity" | |
android:exported="false" /> | |
<activity | |
android:name="com.example.profilescreen.view.activity.ProfileActivity" | |
android:exported="true"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'com.android.application' | |
id 'org.jetbrains.kotlin.android' | |
id 'kotlin-kapt' | |
id("kotlin-parcelize") | |
} | |
android { | |
compileSdk 32 | |
defaultConfig { | |
applicationId "com.example.profilescreen" | |
minSdk 23 | |
targetSdk 32 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
} | |
} | |
compileOptions { | |
sourceCompatibility JavaVersion.VERSION_1_8 | |
targetCompatibility JavaVersion.VERSION_1_8 | |
} | |
kotlinOptions { | |
jvmTarget = '1.8' | |
} | |
buildFeatures { | |
viewBinding true | |
dataBinding true | |
} | |
} | |
dependencies { | |
implementation 'androidx.core:core-ktx:1.7.0' | |
implementation 'androidx.appcompat:appcompat:1.4.2' | |
implementation 'com.google.android.material:material:1.6.1' | |
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' | |
testImplementation 'junit:junit:4.13.2' | |
androidTestImplementation 'androidx.test.ext:junit:1.1.3' | |
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' | |
implementation 'de.hdodenhof:circleimageview:3.1.0' | |
implementation("androidx.room:room-runtime:2.3.0") | |
implementation("androidx.room:room-ktx:2.3.0") | |
kapt("androidx.room:room-compiler:2.3.0") | |
implementation'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0' | |
implementation'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2' | |
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha04' | |
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' | |
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha04' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.profilescreen.view.activity | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import androidx.databinding.DataBindingUtil | |
import com.example.profilescreen.R | |
import com.example.profilescreen.databinding.ActivityDataBinding | |
class DataActivity : AppCompatActivity() { | |
lateinit var dataBinding:ActivityDataBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_data) | |
val bundle: Bundle = intent.extras!! | |
if (bundle!=null) { | |
val first = bundle.getString("1") | |
val last = bundle.getString("2") | |
val birth = bundle.getString("3") | |
val phone = bundle.getString("4") | |
val email = bundle.getString("5") | |
dataBinding.firstName.text = first | |
dataBinding.lastname.text = last | |
dataBinding.birthDate.text = birth | |
dataBinding.mobileNumber.text = phone | |
dataBinding.emailId.text = email | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.profilescreen.view.activity | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.databinding.DataBindingUtil | |
import androidx.lifecycle.ViewModelProvider | |
import com.example.profilescreen.R | |
import com.example.profilescreen.databinding.ActivityProfileBinding | |
import com.example.profilescreen.model.room.UserDetailsDatabase | |
import com.example.profilescreen.viewModel.UserDetailsViewModel | |
class ProfileActivity : AppCompatActivity() { | |
lateinit var binding: ActivityProfileBinding | |
lateinit var database: UserDetailsDatabase | |
lateinit var userDetailsViewModel : UserDetailsViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_profile) | |
userDetailsViewModel = ViewModelProvider(this).get(UserDetailsViewModel::class.java) | |
binding.viewModel = userDetailsViewModel | |
userDetailsViewModel.setContext(this) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.profilescreen.model.dataModel | |
import androidx.room.ColumnInfo | |
import androidx.room.Entity | |
import androidx.room.PrimaryKey | |
@Entity(tableName = "userData") | |
data class UserDetails( | |
@ColumnInfo(name = "firstname") | |
val firstName: String, | |
@ColumnInfo(name = "lastname") | |
val lastName: String, | |
@ColumnInfo(name = "number") | |
val number: Long | |
){ | |
@PrimaryKey(autoGenerate = true) | |
var userId : Int =0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.profilescreen.model.room | |
import androidx.lifecycle.LiveData | |
import androidx.room.* | |
import com.example.profilescreen.model.dataModel.UserDetails | |
@Dao | |
interface UserDetailsDao { | |
@Insert | |
fun insertData(userDetails: UserDetails) | |
@Update | |
fun updateData(userDetails: UserDetails) | |
@Delete | |
fun deleteData(userDetails: UserDetails) | |
@Query("Select * from userData") | |
fun getAllData() : LiveData<UserDetails> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.profilescreen.model.room | |
import android.content.Context | |
import androidx.room.Database | |
import androidx.room.Room | |
import androidx.room.RoomDatabase | |
import com.example.profilescreen.model.dataModel.UserDetails | |
@Database(entities = [UserDetails::class], version = 1) | |
abstract class UserDetailsDatabase : RoomDatabase() { | |
abstract fun userDao() : UserDetailsDao | |
companion object{ | |
@Volatile | |
private var INSTANCE : UserDetailsDatabase? = null | |
fun getDatabase(context: Context): UserDetailsDatabase{ | |
if (INSTANCE == null){ | |
synchronized(this){ | |
INSTANCE = Room.databaseBuilder(context.applicationContext, | |
UserDetailsDatabase::class.java, "UserDB") | |
.allowMainThreadQueries() | |
.build() | |
} | |
} | |
return INSTANCE!! | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.profilescreen.viewModel | |
import android.content.Context | |
import android.widget.Toast | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import com.example.profilescreen.model.dataModel.UserDetails | |
import com.example.profilescreen.model.room.UserDetailsDatabase | |
class UserDetailsViewModel() : ViewModel() { | |
private var context: Context? = null | |
var database = UserDetailsDatabase | |
var firstName = MutableLiveData<String>("") | |
var lastName = MutableLiveData<String>("") | |
val number = MutableLiveData<String>("") | |
fun setContext(myContext: Context) { | |
this.context = myContext | |
} | |
fun onClick() { | |
if (firstName.value.isNullOrEmpty() || lastName.value.isNullOrEmpty() || number.value.isNullOrEmpty()) { | |
Toast.makeText(context, "Enter Above Values", Toast.LENGTH_SHORT).show() | |
} else { | |
val detail = UserDetails(firstName.value!!, lastName.value!!, number.value!!.toLong()) | |
database.getDatabase(context!!).userDao().insertData(detail) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment