Resize android layout above soft keyboard input - android

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

Related

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

Recyclerview items not covering entire screen

I am struggling with a bug for a couple of days. For some reason, my recycler view items don't cover the entire screen.
Here is the code :
feed_fragment.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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.example.bookally.ui.posts.feed.FeedViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<TextView
style="#style/MainHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/feed"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/feeds_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/white"
android:clipToPadding="false"
android:paddingLeft="16dp"
android:paddingTop="28dp"
android:paddingRight="16dp"
android:paddingBottom="28dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
tools:listitem="#layout/item_feed" />
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_baseline_add_24"
android:onClick="#{() -> viewModel.addPosts()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:contentDescription="#string/create_post" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
item_feed.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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="post"
type="com.example.bookally.firebase.PostContent" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#drawable/post_border">
<ImageView
android:id="#+id/author_profile_picture"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:contentDescription="#string/user_image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:setProfilePhoto="#{post.authorImageUrl}" />
<TextView
android:id="#+id/author_name"
style="#style/MainHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="#{post.author}"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="#+id/author_profile_picture"
app:layout_constraintTop_toTopOf="parent"
tools:text="Iqbal Singh" />
<TextView
android:id="#+id/username"
style="#style/MainHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="4dp"
android:text="#{post.username}"
android:textColor="#color/colorGrey"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="#+id/author_profile_picture"
app:layout_constraintTop_toBottomOf="#+id/author_name"
tools:text="\#just_another_boy" />
<TextView
android:id="#+id/post_content"
style="#style/SubHeading"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:gravity="start"
android:text="#{post.content}"
android:textAlignment="viewStart"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/username"
tools:text="Anything random ...." />
<ImageButton
android:id="#+id/like_btn"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#android:color/transparent"
android:contentDescription="#string/likes"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image"
app:likeIcon="#{post.likedBy}" />
<TextView
android:id="#+id/likes_no"
style="#style/MainHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="#+id/like_btn"
app:layout_constraintStart_toEndOf="#+id/like_btn"
app:layout_constraintTop_toTopOf="#+id/like_btn"
app:likesText="#{post.likes}"
tools:text="7 Likes" />
<TextView
android:id="#+id/comments_no"
style="#style/MainHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:textSize="16sp"
app:commentsText="#{post.commentsNumber}"
app:layout_constraintStart_toEndOf="#+id/comment_btn"
app:layout_constraintTop_toTopOf="#+id/comment_btn"
tools:text="0 Comments" />
<ImageButton
android:id="#+id/comment_btn"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#android:color/transparent"
android:contentDescription="#string/comment"
android:src="#drawable/ic_comment"
app:layout_constraintStart_toEndOf="#+id/likes_no"
app:layout_constraintTop_toBottomOf="#+id/post_image" />
<ImageButton
android:id="#+id/save_post_btn"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:background="#android:color/transparent"
android:contentDescription="#string/save_post"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_image"
app:saveIcon="#{post.savedBy}" />
<ImageButton
android:id="#+id/add_friend_btn"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="#android:color/transparent"
android:contentDescription="#string/add_friend"
android:src="#drawable/ic_add_friend"
app:layout_constraintBottom_toBottomOf="#+id/username"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/author_name" />
<ImageView
android:id="#+id/post_image"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:contentDescription="#string/post_imageview_content_description"
app:layout_constraintBottom_toTopOf="#+id/comments_no"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_content"
app:layout_constraintVertical_bias="0.0"
app:loadImagesFromFirebase="#{post.postImageUrl}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
FeedAdapter
class FeedAdapter(private val onClickListener: ClickListener) : PagingDataAdapter<Post, FeedAdapter.FeedViewHolder>(
DiffCallback
) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedViewHolder {
return FeedViewHolder(ItemFeedBinding.inflate(LayoutInflater.from(parent.context)))
}
override fun onBindViewHolder(holder: FeedViewHolder, position: Int) {
val post = getItem(position)
holder.itemView.findViewById<ImageButton>(R.id.like_btn).setOnClickListener { if (post != null) onClickListener.onLikeClicked(post) }
holder.itemView.findViewById<ImageButton>(R.id.comment_btn).setOnClickListener { if (post != null) onClickListener.onCommentClicked(post) }
holder.itemView.findViewById<ImageButton>(R.id.save_post_btn).setOnClickListener { if (post != null) onClickListener.onSavePostClicked(post) }
holder.itemView.findViewById<ImageButton>(R.id.add_friend_btn).setOnClickListener { if (post != null) onClickListener.onAddFriendClicked(post) }
if (post != null) holder.bind(post.postContent)
}
class FeedViewHolder(private val binding : ItemFeedBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(feed: PostContent) {
binding.post = feed
binding.executePendingBindings()
}
}
In the design tab, everything looks fine :
However, the problem arises in real devices :
However, if I add a view like this, the items cover the entire screen :
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/like_btn" />
Any help would be really appreciated. Thanks in advance !!
I believe the problem is here:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedViewHolder {
return FeedViewHolder(ItemFeedBinding.inflate(LayoutInflater.from(parent.context)))
}
Because you are calling the overload of inflate() that does not specify a parent view, your item view's layout params are being ignored. Change it to this instead:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedViewHolder {
return FeedViewHolder(ItemFeedBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
you problem is
android:layout_height="wrap_content"
<TextView
android:id="#+id/post_content"
style="#style/SubHeading"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:gravity="start"
android:text="#{post.content}"
android:textAlignment="viewStart"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/username"
tools:text="Anything random ...." />
you must set fixedHeight or use maxLines and set width to match_parent
also remove this line android:textAlignment="viewStart"

getInfoContents not show my custom marker's view (ConstraintLayout)

android studio 3.6
I need to show custom view when click on map's marker:
implementation 'com.google.android.gms:play-services-maps:17.0.0'
snippet to show marker info window
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
mMap.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
override fun getInfoWindow(marker: Marker): View? {
return null
}
override fun getInfoContents(marker: Marker): View? {
val markerLatLng = marker.position
val selectGazStattion =
gazStationsList.first { it.latitude == markerLatLng.latitude && it.longitude == markerLatLng.longitude }
val customView = layoutInflater.inflate(
R.layout.map_marker_info_content_layout, null
)
val textView : TextView = customView.findViewById(R.id.addressValueTextView)
textView.setText(selectGazStattion.address)
return customView
}
})
here map_marker_info_content_layout.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="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/titleContainerLayout"
android:layout_width="0dp"
android:layout_height="#dimen/min_height"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginTop="#dimen/default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:background="#drawable/bottom_border_bg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/titleTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:gravity="center|start"
android:text="Title"
android:textColor="#android:color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/stateTtextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/open"
android:textColor="#android:color/holo_green_light"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/titleTextView"
app:layout_constraintStart_toStartOf="#+id/titleTextView"
app:layout_constraintTop_toBottomOf="#+id/titleTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/containerAgentInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/half_default_margin"
android:background="#drawable/bottom_border_bg"
app:layout_constraintEnd_toEndOf="#+id/titleContainerLayout"
app:layout_constraintStart_toStartOf="#+id/titleContainerLayout"
app:layout_constraintTop_toBottomOf="#+id/titleContainerLayout">
<TextView
android:id="#+id/addressLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/address_colon"
app:layout_constraintEnd_toStartOf="#+id/addressValueTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/addressValueTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="end"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/addressLabelTextView"
app:layout_constraintTop_toTopOf="#+id/addressLabelTextView" />
<TextView
android:id="#+id/workingHoursLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/half_default_margin"
android:text="#string/working_hours_colon"
app:layout_constraintEnd_toEndOf="#+id/addressLabelTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/addressLabelTextView"
app:layout_constraintTop_toBottomOf="#+id/addressLabelTextView" />
<TextView
android:id="#+id/workingHoursValueTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="end"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/addressValueTextView"
app:layout_constraintTop_toTopOf="#+id/workingHoursLabelTextView" />
<TextView
android:id="#+id/phoneLabelTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/half_default_margin"
android:text="#string/phone_colon"
app:layout_constraintEnd_toEndOf="#+id/addressLabelTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/workingHoursLabelTextView"
app:layout_constraintTop_toBottomOf="#+id/workingHoursLabelTextView" />
<TextView
android:id="#+id/phoneValueTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="end"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/addressValueTextView"
app:layout_constraintTop_toTopOf="#+id/phoneLabelTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/containerServices"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/half_default_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/titleContainerLayout"
app:layout_constraintStart_toStartOf="#+id/titleContainerLayout"
app:layout_constraintTop_toBottomOf="#+id/containerAgentInfo">
<TextView
android:id="#+id/servicesTextView"
style="#style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_margin"
android:layout_marginEnd="#dimen/half_default_margin"
android:text="#string/services_colon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.yarolegovich.discretescrollview.DiscreteScrollView
android:id="#+id/agetServiceDiscreteScrollView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:dsv_orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/servicesTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
But when I click on marker I get this:
Why not show my custom view?
P.S. If I use LinearLayout then success show info window. But with androidx.constraintlayout.widget.ConstraintLayout not work

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
}
}

Android Kotlin pass value from textfields to numberPicker

So I have these values (Long and Double) in three textfields. They represent time, pace, and distance.
Now, to prevent user error I want to make them choose the values using numberPickers. I want to replace those textfields with those pickers.
This is what I have:
val time = timeTxtField as TextView
val distance = distanceTxtField as TextView
val pace = paceTxtField as TextView
var resetRunningBtn = clearBtn
val pickerMinutes = numberPicker as NumberPicker
val pickerSeconds = numberPickerSeconds as NumberPicker
pickerMinutes.minValue = 0
pickerMinutes.maxValue = 59
pickerMinutes.wrapSelectorWheel = false
pickerSeconds.minValue = 0
pickerSeconds.maxValue = 60
pickerSeconds.wrapSelectorWheel = false
calculateBtn.setOnClickListener {
when {
time.text.isEmpty() && (distance.text.isNotEmpty() && pace.text.isNotEmpty()) ->
calculatePace(null, distance.text.toString().toDouble(), pace.text.toString())
distance.text.isEmpty() && (time.text.isNotEmpty() && pace.text.isNotEmpty()) ->
calculatePace(time.text.toString(), null, pace.text.toString())
pace.text.isEmpty() && (time.text.isNotEmpty() && distance.text.isNotEmpty()) ->
calculatePace(time.text.toString(), distance.text.toString().toDouble(), null)
else -> {
Toast.makeText(this, "Please check fields",
Toast.LENGTH_SHORT).show()
}
}
}
As you can see I have the pickers set and all that. I tried to call something like .value but it didn't work.
This is my layout, so you can have a bit of understanding of the concept.
Any tips? :)
Thanks
EDIT XML:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.reecreate.woderator2.Controller.RunningCalculatorActivity">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Running Pace Calculator"
android:textSize="24sp"
android:textStyle="bold"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/distanceTxtField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="distance..."
android:inputType="number|numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioType" />
<EditText
android:id="#+id/timeTxtField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="time mm:ss"
android:inputType="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/distanceTxtField" />
<EditText
android:id="#+id/paceTxtField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="pace mm:ss 10:21"
android:inputType="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timeTxtField" />
<RadioGroup
android:id="#+id/radioType"
android:layout_width="280dp"
android:layout_height="56dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5">
<RadioButton
android:id="#+id/milesDistanceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="140dp"
android:layout_marginTop="16dp"
android:text="Miles"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#id/kmDistanceRadioBtn"
app:layout_constraintTop_toBottomOf="#id/textView5" />
<RadioButton
android:id="#+id/kmDistanceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="KM"
app:layout_constraintEnd_toStartOf="#+id/milesRadioBtn"
app:layout_constraintHorizontal_bias="0.694"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RadioGroup>
<RadioGroup
android:id="#+id/paceRadioGroup"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.294"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/paceTxtField">
<RadioButton
android:id="#+id/milesPaceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="140dp"
android:layout_marginTop="16dp"
android:text="MILES PACE"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#id/kmDistanceRadioBtn"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/kmPaceRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="KM PACE"
app:layout_constraintEnd_toStartOf="#+id/milesDistanceRadioBtn"
app:layout_constraintHorizontal_bias="0.694"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RadioGroup>
<Button
android:id="#+id/calculateBtn"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:text="Calculate"
android:textColor="#android:color/background_light"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/result"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:background="#drawable/border"
android:hint="Result"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/paceRadioGroup" />
<Button
android:id="#+id/clearBtn"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:background="#color/colorAccent"
android:text="CLEAR"
android:textColor="#android:color/background_light"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/result" />
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="64dp"
android:layout_height="119dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.407"
app:layout_constraintTop_toBottomOf="#+id/radioType" />
<NumberPicker
android:id="#+id/numberPickerSeconds"
android:layout_width="64dp"
android:layout_height="119dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/numberPicker" />
EDIT VALUE LISTENER
I added this, but not doing anything
var pickedValue: Int = pickerMinutes.value
time.setText(Integer.toString(pickedValue))
................
when {
pickerMinutes.isClickable -> time.setText(Integer.toString(pickedValue))
}
numberPicker.setOnValueChangedListener { pickerMinutes, oldVal, newVal ->
//store values in variable for use later
}
A few general Kotlin things to note: I see you are setting variables and casting the the number pickers. You don't need to do this. In Kotlin, with the correct import, something like: kotlinx.android.synthetic.main.my_layout_file.*, you can simply call the xml by its id without casting, like so: numberPicker. That goes for your other items as well, such as the TextViews.
The other thing is that you shouldn't have to call Integer.toString(pickedValue). You can just call pickedValue.toString().
Lastly, since I am unable to see exactly how you are testing the app, my suggestion would be to do the following:
Create variables to store the picker values:
var numPickerVal = 0;
var secondsPickerVal = 0;
Then set a change listener for each of the pickers:
numberPicker.setOnValueChangedListener { picker, oldVal, newVal ->
numPickerVal = newVal
}
numberPickerSeconds.setOnValueChangedListener { picker, oldVal, newVal ->
secondsPickerVal = newVal
}

Categories

Resources