I've created a specific layout in order to display when any internet error appears in my android application.
The idea is to include this layout in the fragments where it can be a problem. This is the xml file:
<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:id="@+id/errorLayout">
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/imageView2"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_marginTop="150dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/error_24" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/textView24"
android:layout_width="@dimen/fit_space"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:gravity="center"
android:text="@string/error_loading"
android:textColor="?attr/colorOnPrimary"
android:textSize="26sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/textView26"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:gravity="center"
android:text="@string/try_again"
android:textColor="?attr/colorOnPrimary"
android:textSize="16sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView24" />
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonRefresh"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginTop="30dp"
android:drawableEnd="@drawable/refresh_24"
android:text="@string/refresh"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView26" />
</androidx.constraintlayout.widget.ConstraintLayout>
I'm trying unsuccessfully to use the button from this layout in my fragment, which I included like that:
<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:id="@+id/layoutContainerNews">
<include
android:id="@+id/errorLayoutNews"
layout="@layout/item_layout_error"
android:visibility="gone"/>
<...content...>
</androidx.constraintlayout.widget.ConstraintLayout>
I'm able to show and hide the layout normally, but I'm not able to reach successfully the button included in order to perform a click.
This is one of the many ways that I tried, unsuccessfully, to perform a click in the button.
private fun configButtonRefreshClickListener() = with(binding) {
val refreshButton = errorLayoutNews.buttonRefresh
refreshButton.setOnClickListener {
newsAdapter.refresh()
}
}
How could I access this button properly in order to perform the click?
Thanks in advance. \
EDIT: I'm able, from the function above, using the same structure, to change the text from the textView but I'm not able to perform a click in the button.
val errorLayout = errorLayoutNews
errorLayout.textView19.text = "Test"