Reputation: 393
I have 3 cards that are showing on a screen of my app. When a user clicks a card, the card should expand to show the text. I want to achieve this by toggling the visibility of the text view within the clicked card. The very first card on the page works as expected, but none of the other cards function properly. How do I achieve the desired behaviour?
here is my kotlin class:
private lateinit var thruHist: TextView
private lateinit var esHist: TextView
private lateinit var bayHist: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// this tells the app which layout to associate with this class
setContentView(R.layout.activity_workhistory)
// now connect each view to a variable for manipulation
thruHist = findViewById(R.id.thruHistory)
esHist = findViewById(R.id.esHistory)
bayHist = findViewById(R.id.bayHistory)
}
fun openBay(view: View) {
if (bayHist.visibility === View.VISIBLE) {
bayHist.visibility = View.GONE
} else bayHist.visibility = View.VISIBLE
}
fun openEs(view: View) {
if (esHist.visibility === View.VISIBLE) {
esHist.visibility = View.GONE
} else esHist.visibility = View.VISIBLE
}
fun openThru(view: View) {
if (thruHist.visibility === View.VISIBLE) {
thruHist.visibility = View.GONE
} else thruHist.visibility = View.VISIBLE
}
Here is my content layout XML file:
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openBay"
app:cardElevation="2dp"
app:cardCornerRadius="8dp"
>
<TextView
android:id="@+id/bayHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openEs"
app:cardElevation="2dp"
app:cardCornerRadius="8dp"
>
<TextView
android:id="@+id/esHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openThru"
app:cardElevation="2dp"
app:cardCornerRadius="8dp"
>
<TextView
android:id="@+id/thruHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah" />
</androidx.cardview.widget.CardView>
I've condensed the code for readability and to also highlight the issue that I am trying to resolve.
I am new to android so feedback would be appreciated. Thanks in advance.
Upvotes: 0
Views: 109
Reputation: 2617
I used a LinearLayout with vertical orientation, and works as expected, on click the texts are visible, I did not do any change to your activity, the gradle dependency for the cardview is implementation 'androidx.cardview:cardview:1.0.0'
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openBay"
app:cardElevation="2dp"
app:cardCornerRadius="8dp">
<TextView
android:id="@+id/bayHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openEs"
app:cardElevation="2dp"
app:cardCornerRadius="8dp">
<TextView
android:id="@+id/esHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="15dp"
android:onClick="openThru"
app:cardElevation="2dp"
app:cardCornerRadius="8dp">
<TextView
android:id="@+id/thruHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Worked here blabbity blah blah"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
Upvotes: 1