Can't update data in Firestore with TextInputLayout - android

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.

Related

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

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.

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?

Resize android layout above soft keyboard input

I have a view that uses constraint layout. The view has an edit text and expects the soft keyboard input to show up. There are two buttons pinned to the bottom of the screen(Previous and Next). When the soft keyboard opens up, the two buttons hide behind the keyboard. However, I want the layout to resize so that the buttons are above the soft keyboard and not behind it.
I have the following in my manifest.
<activity
android:name=".activity.SomeActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
</activity>
I have looked at various other posts and tried a few different things like, adjustPan. I have set
android:fitsSystemWindows="true"
But both of them did not work.
My Constraint layout looks like this.
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorLibWhite"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".ui.main.question.QuestionFragment">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="8dp"
android:layout_marginTop="4dp"
android:background="#color/colorLibWhite"
android:indeterminate="false"
android:progressDrawable="#drawable/progress_bar_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/sectionHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#color/colorLibLighterGray"
android:ellipsize="end"
android:maxLines="2"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
android:text="new section"
android:textColor="#color/colorLibGray"
android:textSize="12sp"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="#+id/questionLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/progressBar"/>
<TextView
android:id="#+id/questionLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:text="#string/placeholder"
android:textAlignment="center"
app:layout_constraintBottom_toTopOf="#+id/inputContainerCL"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sectionHeader"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/inputContainerCL"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#+id/prevNextContainer"
app:layout_constraintTop_toBottomOf="#+id/questionLabel">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/inputContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/hintText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Hint text goes here"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputContainer"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/prevNextContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="16dp"
android:background="#drawable/primary_button"
android:text="#string/question_previous"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:background="#drawable/primary_button"
android:text="#string/question_next"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
EditText is added programatically.
var editText = CustomEditText(context)
editText.apply {
id = View.generateViewId()
layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)
setSingleLine(true)
imeOptions = EditorInfo.IME_ACTION_DONE
val questionType = question.validation?.type
if (questionType == Validation.Type.INTEGER) {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED
} else if (questionType == Validation.Type.NUMBER) {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_NUMBER_FLAG_DECIMAL
} else if (questionType == Validation.Type.MRN) {
inputType = InputType.TYPE_CLASS_NUMBER
filters = arrayOf(InputFilter.LengthFilter(9))
} else if (questionType == Validation.Type.ZIP_CODE) {
inputType = InputType.TYPE_CLASS_PHONE
filters = arrayOf(InputFilter.LengthFilter(10))
} else if (questionType == Validation.Type.PHONE) {
inputType = InputType.TYPE_CLASS_NUMBER
editText.setSelection(editText.text?.length!!)
editText.addTextChangedListener(PhoneFormatterTextWatcher(editText))
}
tag = TAG_FOCUS_FIRST
}
editText.setText(question.value ?: "")
if (question.validation?.type == Validation.Type.ZIP_CODE) {
editText.addTextChangedListener(ZipCodeTextWatcher(editText, question, updateCallback))
} else {
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
//
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
question.value = if (s != null && s.isNotBlank()) s.toString() else null
updateCallback()
}
}
)
}
editText.setOnEditorActionListener() { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
doneCallback()
true
} else {
false
}
}
return editText
Here is the code that adds this view to input container
inputContainer.addView(editText)
Where is your EditText view?.I think you must learn about Constraint Layout. how ever you can try this xml layout code..
Add your styles for views.
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".ui.main.question.QuestionFragment">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="411dp"
android:layout_height="7dp"
android:indeterminate="false"
android:progress="40"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/sectionHeader"
android:layout_width="99dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:maxLines="2"
android:paddingStart="16dp"
android:visibility="gone"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
android:text="new section"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/progressBar" />
<TextView
android:id="#+id/questionLabel"
android:layout_width="379dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="questionLabel"
android:layout_marginRight="8dp"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sectionHeader" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/inputContainerCL"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toTopOf="#+id/prevNextContainer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/questionLabel">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/inputContainer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toTopOf="#+id/hintText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<EditText
android:id="#+id/editText2"
android:layout_width="0dp"
android:layout_height="0dp"
android:ems="10"
android:gravity="top"
android:text="text"
android:inputType="textMultiLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/hintText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="Hint text goes here"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/prevNextContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
android:text="question_previous"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="16dp"
android:text="question_next"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
this is the preview

Setting View.VISIBLE does not always make my views visible

I have the following list of checkboxes and TextInputLayouts
<CheckBox
android:text="#string/has_lession"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/hasLeassionCheckBox"
android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="32dp"
app:layout_constraintHorizontal_bias="1.0"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="32dp"
android:hint="#string/lesion_comments" app:layout_constraintHorizontal_bias="0.0"
android:layout_marginTop="16dp" app:layout_constraintTop_toBottomOf="#+id/hasHadLessionCheckBox"
android:visibility="gone" android:id="#+id/lesionCommentsField">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"/>
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:text="#string/has_had_lession"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/hasHadLessionCheckBox" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/hasLeassionCheckBox"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:text="#string/uses_tobaco"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/tobacoCheckBox" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/lesionCommentsField"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
android:layout_marginEnd="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:hint="#string/years_of_tobacco_use"
app:layout_constraintTop_toBottomOf="#+id/tobacoCheckBox" android:id="#+id/yearsOfTobacoUseField"
android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:text="#string/uses_alcohol"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/alcoholCheckbox" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/yearsOfTobacoUseField"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/alcoholYearsField" app:layout_constraintStart_toEndOf="#+id/drinksPerWeek"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="#+id/drinksPerWeek"
app:layout_constraintBottom_toBottomOf="#+id/drinksPerWeek" android:hint="#string/years_alcohol_used"
android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/drinksPerWeek" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toStartOf="#+id/alcoholYearsField"
android:layout_marginStart="32dp" android:layout_marginEnd="8dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/alcoholCheckbox" android:hint="#string/drinks_per_week"
android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:text="#string/history_of_cancer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/cancerCheckBox" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="32dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/drinksPerWeek"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/cancerCountField" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toStartOf="#+id/cancerYearsField"
app:layout_constraintTop_toTopOf="#+id/cancerYearsField"
app:layout_constraintBottom_toBottomOf="#+id/cancerYearsField" android:layout_marginStart="32dp"
android:layout_marginEnd="8dp" android:visibility="gone"
android:hint="#string/cancer_count">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/cancerYearsField"
app:layout_constraintStart_toEndOf="#+id/cancerCountField" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/cancerCheckBox" android:layout_marginStart="8dp"
android:layout_marginEnd="32dp" android:visibility="gone"
android:hint="#string/years_since_cancer">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:text="#string/history_of_oral_pain"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/oralPainCheckBox" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/cancerCountField"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="32dp"
android:onClick="onCheckboxClicked"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/painSeverityField" android:hint="#string/pain_severity"
app:layout_constraintStart_toEndOf="#+id/yearsOfOralPainField"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" app:layout_constraintTop_toBottomOf="#+id/oralPainCheckBox"
android:layout_marginStart="8dp" android:layout_marginEnd="32dp" android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/yearsOfOralPainField"
android:hint="#string/years_of_oral_pain" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toStartOf="#+id/painSeverityField"
app:layout_constraintTop_toTopOf="#+id/painSeverityField"
app:layout_constraintBottom_toBottomOf="#+id/painSeverityField" android:layout_marginStart="32dp"
android:layout_marginEnd="8dp" android:visibility="gone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:text="#string/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nextPatientButton" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="#+id/painSeverityField" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="32dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The TextInputLayouts are shown and hidden based on if cirtan checkboxes are clicked or not. Here is the code that handles that:
fun onCheckboxClicked(view: View) {
if (view is CheckBox) {
val checked: Boolean = view.isChecked
val test = view.id
when (view.id) {
R.id.hasLeassionCheckBox->{
if(checked){
lesionCommentsField.visibility = View.VISIBLE
}
else{
lesionCommentsField.visibility = View.GONE
}
}
R.id.tobacoCheckBox->{
if(checked){
yearsOfTobacoUseField.visibility = View.VISIBLE
}
else{
yearsOfTobacoUseField.visibility = View.GONE
}
}
R.id.alcoholCheckbox->{
if(checked){
drinksPerWeek.visibility = View.VISIBLE
alcoholYearsField.visibility = View.VISIBLE
Log.i("PATIENT ACTIVITY", "DRINKS PER WEEK VIEW: ${drinksPerWeek.visibility}")
}
else{
drinksPerWeek.visibility = View.GONE
alcoholYearsField.visibility = View.GONE
}
}
R.id.cancerCheckBox-> {
if (checked) {
cancerYearsField.visibility = View.VISIBLE
cancerCountField.visibility = View.VISIBLE
} else {
cancerYearsField.visibility = View.GONE
cancerCountField.visibility = View.GONE
}
}
R.id.oralPainCheckBox->{
if(checked){
yearsOfOralPainField.visibility = View.VISIBLE
painSeverityField.visibility = View.VISIBLE
}
else{
yearsOfOralPainField.visibility = View.GONE
painSeverityField.visibility = View.GONE
}
}
}
}
}
The way this is supposed to work is that certain TextInputLayouts are displayed when the user selects certain checkboxes. For example: if the user selects the has_lession checkbox, it is supposed to display the lesionCommentsField TextInputLayout.
The first time the user goes through this form, this code worked perfectly. For all the checkboxes and TextInputLayouts. The second time the user is on this form, the checkboxes that were clicked the last time around don't make their corresponding TextInputLayouts visible. For example: if the user selects the has_lession checkbox last time they used the form and selects the same checkbox this time around the lesionCommentsField is not displayed.
The other checkboxes that were not clicked in the previous use of the form work just fine, it is only the checkboxes that were used in previous use of the form that misbehaves.
The issue goes away when the app is restarted.
I have debugged and checked that the fields do get set to visible, they are just not displayed on the screen.
I have this inside a fragment and the checkbox code is in its parent activity. Not sure if this makes a difference.
How would I go about debugging and resolving this?
Editing my answer, I just understood multiple views can be visible at the same time.
Why are you not writing this separately for all the checkbox onCheckChange method instead of onClick?
hasLeassionCheckBox.setOnCheckedChangeListener { compoundButton, isChecked ->
if (isChecked) {
lesionCommentsField.visibility = View.VISIBLE
} else {
lesionCommentsField.visibility = View.GONE
}
}

Categories

Resources