I'm creating a View Stub for my login screen, however I have a question. When I try to set an setOnClickListener
on my login button in my viewstub_login.kt
file, the button action won't go through. Do I need to change my extension on my Kotlin file, or do I need to put my setOnClickListener
in the onStart()
function? Thank you!
viewstub_login.xml
<?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"
android:background="#BF090909">
<androidx.cardview.widget.CardView
android:id="@+id/loginCardView"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
app:cardBackgroundColor="@color/main_purple"
app:cardCornerRadius="8dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/logoLoginImageView"
android:layout_width="90dp"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:contentDescription="@string/login_logo_image"
android:scaleType="fitXY"
android:src="@drawable/skedaddle_services_"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/emailTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:textColorHint="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logoLoginImageView">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/emailInputTextView"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/textview_underlinr"
android:drawableStart="@drawable/outline_mail_white_24"
android:drawablePadding="5dp"
android:hint="@string/e_mail"
android:textColor="@color/white"
android:textColorHint="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/passwordTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:hint="@string/password"
android:textColorHint="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/emailTextInputLayout"
app:passwordToggleEnabled="true"
app:passwordToggleTint="@color/white">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/passwordTextInputView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/textview_underlinr"
android:drawableStart="@drawable/outline_lock_white_24"
android:drawablePadding="5dp"
android:hint="@string/password"
android:inputType="textPassword"
android:textColor="@color/white"
android:textColorHint="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/forgotPasswordButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:background="@android:color/transparent"
android:text="@string/forgot_password"
android:textAlignment="textEnd"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="12sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordTextInputLayout" />
<Button
android:id="@+id/cancelButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:background="@color/black"
android:text="@android:string/cancel"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/secondLoginButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/forgotPasswordButton" />
<Button
android:id="@+id/secondLoginButton"
android:layout_width="120dp"
android:layout_height="48dp"
android:layout_marginStart="32dp"
android:background="@color/main_teal"
android:text="@string/log_in"
android:textAllCaps="false"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="@+id/cancelButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/cancelButton"
app:layout_constraintTop_toTopOf="@+id/cancelButton"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
viewstub_login.kt
class LoginViewStub : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.viewstub_login)
val emailTextView = findViewById<TextInputEditText>(R.id.emailInputTextView)
val passwordTextView = findViewById<TextInputEditText>(R.id.passwordTextInputView)
val forgetPasswordButton = findViewById<Button>(R.id.forgotPasswordButton)
val loginButton = findViewById<Button>(R.id.secondLoginButton)
val cancelButton = findViewById<Button>(R.id.cancelButton)
val emailText = emailTextView.text
val passwordText = passwordTextView.text
loginButton.setOnClickListener {
if (emailText.isEmpty(emailTextView.text) || passwordText.isEmpty(passwordTextView.text)) {
val dialogBuilder = AlertDialog.Builder(this)
dialogBuilder.setMessage("Please fill out all fields.").setCancelable(false).setPositiveButton("Okay", DialogInterface.OnClickListener {
dialog, id -> finish()
}).setNegativeButton("Cancel", DialogInterface.OnClickListener {
dialog, id -> dialog.cancel()
})
val alert = dialogBuilder.create()
alert.show()
}
}
cancelButton.setOnClickListener {
}
forgetPasswordButton.setOnClickListener {
}
}
}
private fun Boolean.isEmpty(str: CharSequence?): Boolean {
return str == null || str.length == 0
}
click listener is correct, but inside the block, your code is not correct because emailText and passwordText is not null, as you have defined above, thus the if condition returning false and your code just going over it