Creating new activity

Now we need an activity where we will create a function to insert the user details and save into the database. Right-click on the app folder and create Empty Activity named NewUserActivity.kt like the following screenshot:

Here is the code of the layout of this class named activity_new_user.xml. (The entire code can be found at GitHub link):

----
----
<EditText
android:id="@+id/editEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:padding="5dp"
android:textSize="18sp" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/editUsername" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<EditText
android:id="@+id/editContactID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_contact"
android:inputType="phone"
android:padding="5dp"
android:textSize="18sp" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/editEmail" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
----
---
<Button
android:id="@+id/buttonSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="@string/button_save"
android:textColor="@android:color/white"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/editAddress" app:layout_constraintVertical_bias="1.0"/>
</android.support.constraint.ConstraintLayout>

Here we have added four EditText where we can input—username, contactNumber, emailaddress, and a button named buttonSave to save this information into the database.

Here is the code of the NewUserActivity.kt class:

class NewUserActivity : AppCompatActivity(), View.OnClickListener {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_user)
buttonSave.setOnClickListener(this)
}

override fun onClick(view: View?) {
if (view!!.id == R.id.buttonSave){
val intent = Intent()
if (isTextFieldEmpty()){
Snackbar.make(view, "Empty Field", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
setResult(Activity.RESULT_CANCELED, intent)
} else {
val users = Users(editUsername.text.toString(),
editEmail.text.toString(),
editContactID.text.toString(),
editAddress.text.toString())

Log.wtf("CRAY", editUsername.text.toString()+" "+
editEmail.text.toString()+" "+
editContactID.text.toString()+" "+
editAddress.text.toString())

Log.wtf("CRAY", users.toString())
// If an instance of this Activity already exists, then it will be moved to the front.
// If an instance does NOT exist, a new instance will be created.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
intent.putExtra(getString(R.string.result_replay), users)
setResult(Activity.RESULT_OK, intent)
}
finish()
}
}

private fun isTextFieldEmpty(): Boolean {
return TextUtils.isEmpty(editUsername.text) ||
TextUtils.isEmpty(editEmail.text) ||
TextUtils.isEmpty(editContactID.text) ||
TextUtils.isEmpty(editAddress.text)
}
}

According to the preceding code:

  • Implement the View.OnClickListener and override the onClick(view: View?).
  • In the onCreate()setOnClickListener() for the buttonSave, and override the onClick(view: View?) that we want to execute with the button. Lastly, we call an Intent, which will change the activity from the UserModel to the MainActivity class. 
  • The isTextFieldEmpty() is designed to check whether the EditText fields are empty or not.
  • Then we get all the text, make a UserObject, and pass this Parcelable user object to the MainActivity using intent.putExtra(getString(R.string.result_replay), users).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.58.194