I'm trying to check what type of childrens has a linearlayout, but i keep getting true that a textview is the same as a view. How can I make the difference between a textview and a view.
In my linear layout I have a view and a text view and in the code below when the child is textview i'm getting true in both sentences(I mean I'm getting true in view is View and view is TextView if the child in that moment is textview). And I want to know thow can I get false when the sentence is view is View if the child is textview.
Here is where I'm trying to check the type of child:
private fun selectFromLayout(ll: LinearLayout, select: Boolean) {
val childCount = ll.childCount
for (i in 0 until childCount) {
val view = ll.getChildAt(i)
if (view is View) {
if (select) {
view.background = ContextCompat.getDrawable(context, R.drawable.back_select)
} else {
view.setBackgroundResource(R.color.white)
}
}
if (view is TextView) {
if (select) {
view.setTextColor(ContextCompat.getColor(context, R.color.gray_dark))
view.typeface = ResourcesCompat.getFont(context, R.font.montserrat_medium)
} else {
view.setTextColor(ContextCompat.getColor(context, R.color.gray_medium))
view.typeface = ResourcesCompat.getFont(context, R.font.montserrat_regular)
}
}
}
}
Linear layout:
<LinearLayout
android:id="@ id/btn_tab1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:focusable="true"
android:gravity="bottom"
android:orientation="vertical">
<TextView
android:id="@ id/tv_tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
app:textSize="10sp"
app:textType="regular"
tools:text="Próximas" />
<View
android:id="@ id/tab1_shadow"
android:layout_width="match_parent"
android:layout_height="2dp" />
</LinearLayout>
CodePudding user response:
It's not necessary to check is View
as it always be true, since all views in android extends from View
class.
private fun selectFromLayout(ll: LinearLayout, select: Boolean) {
val childCount = ll.childCount
for (i in 0 until childCount) {
val view = ll.getChildAt(i)
if (view is TextView) {
if (select) {
view.setTextColor(ContextCompat.getColor(context, R.color.gray_dark))
view.typeface = ResourcesCompat.getFont(context, R.font.montserrat_medium)
} else {
view.setTextColor(ContextCompat.getColor(context, R.color.gray_medium))
view.typeface = ResourcesCompat.getFont(context, R.font.montserrat_regular)
}
} else {
if (select) {
view.background = ContextCompat.getDrawable(context, R.drawable.back_select)
} else {
view.setBackgroundResource(R.color.white)
}
}
}
}