Android material TextInputLayout doesn't reconnect outline after setHintEnabled(false) - android

Using com.google.android.material:material:1.6.1, when setting hint enabled to true and then to false, the outline gets broken. Here is the example code.
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val someHintButton = findViewById<MaterialButton>(R.id.someHintButton)
val noHintButton = findViewById<MaterialButton>(R.id.noHintButton)
val textInput = findViewById<TextInputEditText>(R.id.textInput)
val textLayout = findViewById<TextInputLayout>(R.id.textLayout)
someHintButton.setOnClickListener {
textInput.setText("Some text")
textLayout.setHintEnabled(true)
textLayout.setHint("Entered text:")
}
noHintButton.setOnClickListener {
textLayout.setHint(null)
textLayout.setHintEnabled(false)
textInput.setText("Some text will be entered here")
}
}
}
activity_main.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"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:layout_marginTop="5dp"
android:layout_marginHorizontal="5dp"
android:id="#+id/textLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
>
<com.google.android.material.textfield.TextInputEditText
android:focusable="false"
android:editable="false"
android:id="#+id/textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here some text"
android:textAlignment="center"
android:inputType="none"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/someHintButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="some hint"
android:textColor="#color/black"
android:textColorLink="#FFFFFF"
app:backgroundTint="#FFFFFF"
app:cornerRadius="8dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<com.google.android.material.button.MaterialButton
android:id="#+id/noHintButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No hint"
android:textColor="#color/black"
android:textColorLink="#FFFFFF"
app:backgroundTint="#FFFFFF"
app:cornerRadius="8dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Screen after launch:
Screen after 'Some hint' pressed:
Screen after 'No hint' pressed:
setHint(null) or setHint("") don't do any difference. Why doesn't outline get reconnected? Please advise.

Related

Can't update data in Firestore with TextInputLayout

I am using TextInputLayout on Edit Profile screen and I wanted to update datas in that screen but when I used that, datas update to like
"com.google.android.material.textfield.TextInputLayout{9544eb3 V.ED..... ........ 93,232-931,455 #7f0a00d8 app:id/etEditName aid=1073741880}"
I will leave my codes.
ProfileFragment.kt
....
binding.btnEditProfile.setOnClickListener {
val dialog =
LayoutInflater.from(context).inflate(R.layout.edit_profile_dialog, null)
val builder = AlertDialog.Builder(context).setView(dialog).show()
builder.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
builder.setCancelable(true)
val etName = dialog.findViewById<TextInputLayout>(R.id.etEditName)
etName.editText?.setText(user.name)
val etSurname = dialog.findViewById<TextInputLayout>(R.id.etEditSurname)
etSurname.editText?.setText(user.surname)
val etWeight = dialog.findViewById<TextInputLayout>(R.id.etEditWeight)
etWeight.editText?.setText(user.weight)
val etHeight = dialog.findViewById<TextInputLayout>(R.id.etEditHeight)
etHeight.editText?.setText(user.height)
val etGoal = dialog.findViewById<TextInputLayout>(R.id.etEditGoal)
etGoal.editText?.setText(user.calorieGoal.toString())
dialog.findViewById<Button>(R.id.btnEditProfile).setOnClickListener {
if (etSurname.isNotEmpty() && etHeight.isNotEmpty() && etWeight.isNotEmpty() && etGoal.isNotEmpty())
dbUser.document(auth.currentUser?.email.toString())
.update(
mapOf(
"name" to etName.toString(),
"surname" to etSurname.toString(),
"height" to etHeight.toString(),
"weight" to etWeight.toString(),
)
)
.addOnSuccessListener {
Toast.makeText(context, "Updated!!", Toast.LENGTH_SHORT).show()
builder.dismiss()
} else {
Toast.makeText(context, "Fill in the fields!!", Toast.LENGTH_SHORT).show()
}
}
}
edit_dialog.xml
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="36dp"
android:background="#drawable/dialog_bg">
<TextView
android:id="#+id/txtEditProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="monospace"
android:text="#string/edit_profile"
android:textColor="#color/primaryDarkColor"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/etEditName"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="24dp"
android:hint="#string/enter_name"
android:maxLines="1"
app:errorEnabled="true"
app:hintTextColor="#color/gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/txtEditProfile">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:textColorHint="#color/primaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/etEditSurname"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="24dp"
android:hint="#string/enter_surname"
android:maxLines="1"
app:errorEnabled="true"
app:hintTextColor="#color/gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/etEditName">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:textColorHint="#color/primaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/etEditWeight"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="24dp"
android:focusableInTouchMode="false"
android:hint="#string/weight"
android:maxLines="1"
android:text=""
app:errorEnabled="true"
app:layout_constraintEnd_toStartOf="#+id/etEditHeight"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/etEditSurname">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cursorVisible="false"
android:inputType="number"
android:textColorHint="#color/primaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/etEditGoal"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="8dp"
android:hint="#string/calorie_goal"
android:maxLines="1"
android:text=""
app:errorEnabled="true"
app:hintTextColor="#color/gray"
app:layout_constraintEnd_toStartOf="#+id/etEditHeight"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintStart_toEndOf="#+id/etEditWeight"
app:layout_constraintTop_toBottomOf="#id/etEditHeight">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cursorVisible="false"
android:inputType="number"
android:textColorHint="#color/primaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/etEditHeight"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="24dp"
android:hint="#string/height"
android:maxLines="1"
android:text=""
app:errorEnabled="true"
app:hintTextColor="#color/gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/etEditWeight"
app:layout_constraintTop_toBottomOf="#id/etEditSurname">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cursorVisible="false"
android:inputType="number"
android:textColorHint="#color/primaryDarkColor" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/btnEditProfile"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:text="#string/submit"
android:textColor="#color/secondaryTextColor"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/etEditGoal" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thanks for your helps :)
You are updating the TextInputLayout itself when you need to update the text in the EditText inside the TextInputLayout. So
Replace
"name" to etName.toString(),
"surname" to etSurname.toString(),
"height" to etHeight.toString(),
"weight" to etWeight.toString(),
With
"name" to etName.editText?.text.toString(),
"surname" to etSurname.editText?.text.toString(),
"height" to etHeight.editText?.text.toString(),
"weight" to etWeight.editText?.text.toString(),
This will update user data in the Edit Profile screen.

Unresolved reference to constraintLayout variable in Kotlin code in android app

I am following this tutorial to create an android app with space animation stuff. In the step titled "Transition Manager" you're instructed to add the following code to your MainActivity.kt file:
`
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
`
When this code is added the variable "constraintLayout" throws up an unresolved reference error
I think constraintLayout is supposed to be pointing to something in the xml and something is failing to make that happen, I just don't know what.
The kotlin code after adding the above block is:
`
package com.raywenderlich.android.razegalactic
import android.os.Bundle
import android.support.constraint.*
import android.support.v7.app.AppCompatActivity
import android.transition.TransitionManager
import kotlinx.android.synthetic.main.keyframe1.*
/**
* Main Screen
*/
class MainActivity : AppCompatActivity() {
private val constraintSet1 = ConstraintSet()
private val constraintSet2 = ConstraintSet()
private var isOffscreen = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.keyframe1)
switch1.setOnCheckedChangeListener { _, isChecked ->
switch1.setText(if (isChecked) R.string.round_trip else R.string.one_way)
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
}
}
}
`
The main xml is as follows:
`
<android.support.constraint.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:id="#+id/ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/flightsIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rocket_icon"
app:layout_constraintBottom_toBottomOf="#+id/spaceStationIcon"
app:layout_constraintEnd_toStartOf="#+id/roverIcon"
app:layout_constraintStart_toEndOf="#+id/spaceStationIcon"
app:layout_constraintTop_toTopOf="#+id/spaceStationIcon" />
<ImageView
android:id="#+id/spaceStationIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="#drawable/space_station_icon"
app:layout_constraintEnd_toStartOf="#+id/flightsIcon"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/roverIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rover_icon"
app:layout_constraintBottom_toBottomOf="#+id/flightsIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/flightsIcon"
app:layout_constraintTop_toTopOf="#+id/flightsIcon" />
<TextView
android:id="#+id/spaceStationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/space_stations"
app:layout_constraintEnd_toEndOf="#+id/spaceStationIcon"
app:layout_constraintStart_toStartOf="#+id/spaceStationIcon"
app:layout_constraintTop_toBottomOf="#+id/spaceStationIcon" />
<TextView
android:id="#+id/textView1"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginEnd="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingEnd="20dp"
android:text="#string/dca"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintEnd_toEndOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/roverLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/rovers"
app:layout_constraintEnd_toEndOf="#+id/roverIcon"
app:layout_constraintStart_toStartOf="#+id/roverIcon"
app:layout_constraintTop_toBottomOf="#+id/roverIcon" />
<TextView
android:id="#+id/textView2"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginStart="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingStart="20dp"
android:text="#string/mars"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintStart_toStartOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/flightsLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/flights"
app:layout_constraintEnd_toEndOf="#+id/flightsIcon"
app:layout_constraintStart_toStartOf="#+id/flightsIcon"
app:layout_constraintTop_toBottomOf="#+id/flightsIcon" />
<ImageView
android:id="#+id/doubleArrowsIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="40dp"
android:src="#drawable/double_arrows"
app:layout_constraintBottom_toTopOf="#+id/guideline1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Switch
android:id="#+id/switch1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="200dp"
android:background="#color/colorAccent"
android:checked="false"
android:padding="8dp"
android:switchPadding="24dp"
android:text="#string/one_way"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorAccent"
android:padding="8dp"
android:text="#string/traveller"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toBottomOf="#+id/switch1" />
<ImageView
android:id="#+id/rocketIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="8dp"
android:src="#drawable/rocket_icon"
app:layout_constraintCircle="#id/galaxyIcon"
app:layout_constraintCircleAngle="270"
app:layout_constraintCircleRadius="100dp" />
<ImageView
android:id="#+id/galaxyIcon"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/galaxy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.495" />
<Button
android:id="#+id/departButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/depart"
android:textColor="#android:color/white"
app:backgroundTint="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="158dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="0dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintGuide_percent="1" />
</android.support.constraint.ConstraintLayout>
`
can anyone tell me what I might be missing or how this can be fixed?
Thanks in advance.
You're suppose to reference a view from the xml before you can use it.
Do this, also if the name of the layout the MainActivity is suppose to use is main.xml, it should be the one your setContentView use not some other layout.
lateinit var cosntraintLayout: ConstraintLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main) // <- main.xml
cosntraintLayout = findViewById<ConstraintLayout>(R.id.ConstraintLayout) // <- add this line
...
...
}
switch1 and departButton will also throw the same error, but I'll leave it for you to figure it out based on the code I provided.

Setting setOnClickListener on a Button in a View Stub

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

No view found for id for fragment InspectionDetailsFragment

I have the following XML
<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragment_inspection_details_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="#dimen/size_0dp"
android:layout_height="#dimen/size_0dp"
app:layout_constraintBottom_toTopOf="#+id/fragment_inspection_details_buttons_container"
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="wrap_content">
<TextView
android:id="#+id/fragment_inspection_details_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_16dp"
android:text="#string/details"
android:textAllCaps="true"
android:textColor="#color/gold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="#dimen/size_0dp"
android:layout_height="1dp"
android:layout_margin="#dimen/size_16dp"
android:background="#color/lightGray"
app:layout_constraintBottom_toBottomOf="#+id/fragment_inspection_details_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fragment_inspection_details_title"
app:layout_constraintTop_toTopOf="#+id/fragment_inspection_details_title" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/fragment_inspection_details_status_til"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="#dimen/size_0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragment_inspection_details_title">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/fragment_inspection_details_status_tie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:hint="#string/status" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/fragment_inspection_details_start_time_til"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="#dimen/size_0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragment_inspection_details_status_til">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/fragment_inspection_details_start_time_tie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:hint="#string/start_time" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/fragment_inspection_details_end_time_til"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="#dimen/size_0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragment_inspection_details_start_time_til">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/fragment_inspection_details_end_time_tie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:hint="#string/end_time" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/fragment_inspection_details_desc_time_til"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="#dimen/size_0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragment_inspection_details_end_time_til">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/fragment_inspection_details_desc_time_tie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:hint="#string/description" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="#+id/fragment_inspection_details_handling_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_16dp"
android:text="#string/handling"
android:textAllCaps="true"
android:textColor="#color/gold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragment_inspection_details_desc_time_til" />
<View
android:layout_width="#dimen/size_0dp"
android:layout_height="1dp"
android:layout_margin="#dimen/size_16dp"
android:background="#color/lightGray"
app:layout_constraintBottom_toBottomOf="#+id/fragment_inspection_details_handling_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fragment_inspection_details_handling_title"
app:layout_constraintTop_toTopOf="#+id/fragment_inspection_details_handling_title" />
<TextView
android:id="#+id/fragment_inspection_details_outcome_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_16dp"
android:text="#string/outcome"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragment_inspection_details_handling_title" />
<Spinner
android:id="#+id/fragment_inspection_details_outcome_spinner"
android:layout_width="#dimen/size_0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/size_8dp"
android:layout_marginEnd="#dimen/size_16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragment_inspection_details_outcome_title" />
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="#dimen/size_0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/size_16dp"
android:layout_marginEnd="#dimen/size_16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fragment_inspection_details_outcome_spinner">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/fragment_inspection_details_outcome_report_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Outcome report" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<LinearLayout
android:id="#+id/fragment_inspection_details_buttons_container"
android:layout_width="#dimen/size_0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.appcompat.widget.AppCompatButton
android:layout_width="#dimen/size_0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_16dp"
android:layout_weight="1"
android:background="#drawable/white_background_gray_rounded_border"
android:text="#string/save"
android:textAllCaps="false" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="#dimen/size_0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/size_16dp"
android:layout_weight="1"
android:background="#drawable/white_background_gray_rounded_border"
android:text="#string/handle"
android:textAllCaps="false" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
and the following code -
class InspectionDetailsFragment : Fragment() {
companion object {
fun newInstance(inspection: Inspection): InspectionDetailsFragment {
val inspectionDetailsFragment = InspectionDetailsFragment()
val bundle = Bundle()
bundle.putParcelable(Constants.INSPECTION, inspection)
inspectionDetailsFragment.arguments = bundle
return inspectionDetailsFragment
}
}
...
...
}
//Inside another Fragment
changeFragment(
parentFragmentManager, R.id.fragment_inspection_details_root,
InspectionDetailsFragment.newInstance(inspection), true
)
// Change fragment extension function to handle navigation easily
#SuppressLint("PrivateResource")
fun changeFragment(fragmentManager: FragmentManager?, #IdRes containerId: Int, fragment: Fragment?, addToBackStack: Boolean = false) {
if (fragmentManager == null) return
val fragmentTransaction = fragmentManager.beginTransaction()
if (addToBackStack) fragmentTransaction.addToBackStack(null)
/*fragmentTransaction.setCustomAnimations(
R.anim.abc_fade_in,
R.anim.abc_shrink_fade_out_from_bottom,
R.anim.abc_grow_fade_in_from_bottom,
R.anim.abc_popup_exit
)*/
fragment?.let {
fragmentTransaction
.replace(containerId, it, it::class.java.simpleName)
.commit()
}
}
For some reason when trying to navigate to the InspectionDetailsFragment the app crashes with the No view found for id 0x7f0900df (com.supercom.myapplication:id/fragment_inspection_details_root) for fragment InspectionDetailsFragment I really have no idea what's wrong, I tried to look up but it seems like I am doing everything correct.
The InspectionDetailsFragment used to have a container fragment for it but I deleted it, maybe that is related to the issue I am getting?

Two-Way Data Binding with a LiveData object of modal class

I'm trying to create a Two-Way Data binding connection with a LiveData object of a modal class. But when I tries to read the values of user it turns out to be null. Here is my code -
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModal: MainActivityViewModal = ViewModelProvider(
this,
MainActivityViewModalFactory()
).get(MainActivityViewModal::class.java)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(
this,
R.layout.activity_main
).apply {
this.lifecycleOwner = this#MainActivity
this.viewModal = viewModal
}
}}
Here is the Layout file -
<data>
<variable
name="viewModal"
type="com.weaponx.databindingexample.MainActivityViewModal" />
</data>
<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">
<ImageView
android:id="#+id/ivLogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="#string/logo"
android:tint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/ic_menu_myplaces" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ivLogo">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.10" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilUserName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/edit_text_bg"
android:hint="#string/user_name"
android:padding="5dp"
app:layout_constraintEnd_toStartOf="#id/guideline2"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/tietUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:inputType="text"
android:text="#={viewModal.user.userName}" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/edit_text_bg"
android:hint="#string/password"
android:padding="5dp"
app:layout_constraintEnd_toStartOf="#id/guideline2"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toBottomOf="#id/tilUserName">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/tietPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:inputType="textPassword"
android:text="#={viewModal.user.password}" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:background="#drawable/button_bg"
android:text="#string/login"
android:onClick="#{() -> viewModal.login()}"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#id/guideline2"
app:layout_constraintStart_toEndOf="#id/guideline1"
app:layout_constraintTop_toBottomOf="#id/tilPassword" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.90" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the ViewModal
class MainActivityViewModal : ViewModel() {
var user = MutableLiveData<User>()
fun login() {
println("Login Success ${user.value}")
}}
And here is the Modal Class
class User() {
var userName: String = ""
var password: String = ""
override fun toString(): String {
return "User(userName='$userName', password='$password')"
}}
Please tell me any solution or a good way around to this problem
In the MainActivityViewModal define user as just User type rather as MutableLiveData.

Categories

Resources