I have several TextView
s inside ConstraintLayout
. The visibility of those TextView
s are set at runtime based upon the data availability.
I need to bold the text of the first visible TextView
.
I have tried many things but couldn't resolve the issue.
I have tried:
- Looping through the child inside the parent layout and finding the first child and making it bold. This approach always finds the first child inside the parent layout regardless of it's visiblility.
- I put a check before retrieving the first element if
view.visibility
==
View.VISIBLE
and also I checkedview.isShown
, both way again returns the first child in the view hierarchy irrespective of the visibility. So,view.visibility
always returnsView.VISIBLE
.
What am I missing and how can I make it work?
Providing sample codes:
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">
<data>
<variable
name="viewModel"
type="MyViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First textview"
android:textColor="@color/black"
android:visibility="@{viewModel.textView1Visibility}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second textview"
android:textColor="@color/black"
android:visibility="@{viewModel.textView2Visibility}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView1" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Third textview"
android:textColor="@color/black"
android:visibility="@{viewModel.textView3Visibility}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
CODE
binding.apply {
/** We have three textviews.
* textView1 doesn't have data so we are hiding it.
* textView2 has data so, we need to show it.
* and textView2 will be the first item, we need to bold it as well. */
textView1.visibility = View.GONE
/** looping through the children of root layout and bolding the first visible */
rootLayout.forEach exit@ { view ->
if(view.isVisible || view.isShown) {
(view as TextView).typeface = Typeface.DEFAULT_BOLD
return@exit
}
}
}