I need to access and changue color data in rows. I need to access the rows that are clicked and change their color
TableLAyout.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"
tools:context=".Views.MarcadoXLoteActivity">
<Spinner
android:id="@+id/spinner3"
android:layout_width="105dp"
android:background="@drawable/green_border"
android:layout_height="36dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@+id/textView73"
app:layout_constraintTop_toBottomOf="@+id/textView73" />
<ImageView
android:id="@+id/imageView26x"
android:layout_width="33dp"
android:layout_height="46dp"
android:layout_marginStart="-34dp"
app:layout_constraintBottom_toBottomOf="@+id/spinner3"
app:layout_constraintStart_toEndOf="@+id/spinner3"
app:layout_constraintTop_toTopOf="@+id/spinner3"
app:srcCompat="@drawable/baseline_arrow_drop_down_24" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbarM"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/white"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:subtitleTextColor="@color/white"
app:title=" Marcado de Avances Por Lote"
app:titleTextAppearance="@style/TextAppearance.AppCompat.Display1"
app:titleTextColor="@color/black">
</androidx.appcompat.widget.Toolbar>
<androidx.cardview.widget.CardView
android:id="@+id/cardView2"
android:layout_width="798dp"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="1dp"
app:cardBackgroundColor="#DEF1DE"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbarM">
<TextView
android:id="@+id/tvNomMarcado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=" CIMENTACION"
android:textColor="#11B818"
android:textSize="24sp"
android:textStyle="bold|italic" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/textView72"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="24dp"
android:text="Manzana"
android:textColor="#000000"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="@+id/cardView2"
app:layout_constraintTop_toBottomOf="@+id/cardView2" />
<TextView
android:id="@+id/textView73"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="220dp"
android:text="Rango de Lotes"
android:textColor="#000000"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="@+id/cardView2"
app:layout_constraintTop_toBottomOf="@+id/cardView2" />
<Spinner
android:id="@+id/spinnerMan"
android:layout_width="206dp"
android:layout_height="36dp"
android:layout_marginTop="8dp"
android:background="@drawable/green_border"
app:layout_constraintStart_toStartOf="@+id/textView72"
app:layout_constraintTop_toBottomOf="@+id/textView72" />
<ImageView
android:id="@+id/imageView26"
android:layout_width="33dp"
android:layout_height="46dp"
android:layout_marginStart="-34dp"
app:layout_constraintBottom_toBottomOf="@+id/spinnerMan"
app:layout_constraintStart_toEndOf="@+id/spinnerMan"
app:layout_constraintTop_toTopOf="@+id/spinnerMan"
app:srcCompat="@drawable/baseline_arrow_drop_down_24" />
<TableLayout
android:id="@+id/tlMarcado"
android:layout_width="700dp"
android:layout_height="789dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner3">
<TableRow
android:id="@+id/tableRow2"
android:background="#51B435"
android:padding="10dp">
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="DESCRIPCIÓN"
android:textColor="@color/white" />
<TextView
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:singleLine="false"
android:text="PRECIO
CONTRATISTA"
android:textColor="@color/white" />
<TextView
android:id="@+id/tvTarjetaML"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:text="015004001"
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:text="015004001"
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:text="015004001"
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:text="015004001"
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_gravity="center"
android:layout_weight="0.5"
android:gravity="center"
android:text="015004001"
android:textColor="@color/white" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/btnguardarmercado"
android:layout_width="562dp"
android:layout_height="72dp"
android:layout_marginStart="352dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="352dp"
android:background="@drawable/btn_gray"
android:text="GUARDAR AVANCE"
android:textColor="#FFFEFE"
android:textSize="30sp"
app:backgroundTint="@null"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tlMarcado" />
<Spinner
android:id="@+id/spinner4"
android:layout_width="105dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:background="@drawable/green_border"
app:layout_constraintStart_toEndOf="@+id/textView71"
app:layout_constraintTop_toTopOf="@+id/spinner3" />
<ImageView
android:id="@+id/imageView88"
android:layout_width="33dp"
android:layout_height="46dp"
android:layout_marginStart="-34dp"
app:layout_constraintBottom_toBottomOf="@+id/spinner4"
app:layout_constraintStart_toEndOf="@+id/spinner4"
app:layout_constraintTop_toTopOf="@+id/spinner4"
app:srcCompat="@drawable/baseline_arrow_drop_down_24" />
<TextView
android:id="@+id/textView71"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:text="Al"
android:textColor="#040404"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/spinner3"
app:layout_constraintStart_toEndOf="@+id/spinner3"
app:layout_constraintTop_toBottomOf="@+id/textView73" />
</androidx.constraintlayout.widget.ConstraintLayout>
Second Layout rows:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvDescML"
android:layout_width="120dp"
android:layout_height="60dp"
android:background="@drawable/tableborder"
android:gravity="center"
android:text="FAB. DE REGISTRO PLUBIAL" />
<TextView
android:id="@+id/tvPrecioML"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:background="@drawable/tableborder"
android:text=" $129.00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@color/blueM"
android:padding="6dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@color/blueM"
android:padding="6dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@color/blueM"
android:padding="6dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@color/blueM"
android:padding="6dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="#2A42C6"
android:padding="6dp" />
</TableRow>
Code to access row with click and changue color:
private fun marcartablerocolor() {
val tableLayout = findViewById<TableLayout>(R.id.tlMarcado)
val tablaMarcados2 = layoutInflater.inflate(R.layout.table_layoutmarcado, tableLayout, false) as TableRow
for (rowIndex in 0 until tablaMarcados2.childCount) {
val tableRow = tablaMarcados2.getChildAt(rowIndex) as TableRow
for (cellIndex in 0 until tableRow.childCount) {
val cellView = tableRow.getChildAt(cellIndex)
cellView.setOnClickListener {
// Handle click event
onTableCellClicked(rowIndex, cellIndex)
}
}
}
}
changue color function:
private fun onTableCellClicked(rowIndex: Int, cellIndex: Int) {
val tablaMarcados: TableLayout = findViewById(R.id.tlMarcado)
val verde = resources.getColor(R.color.greenM)
val clickedColor = resources.getColor(R.color.blueM)
val tableRow = tablaMarcados.getChildAt(rowIndex) as TableRow
val cellView = tableRow.getChildAt(cellIndex)
if (cellView is TextView) {
val cellValue = cellView.text.toString()
val currentColor = (cellView.background as ColorDrawable).color
if (currentColor == clickedColor){
cellView.setBackgroundColor(verde)
}
if (currentColor == verde){
cellView.setBackgroundColor(clickedColor)
}
}
}
The result is error. I already looked for how to do it and I couldn't, could you help me
I'm solved with this function
private fun marcartablerocolor() {
val tableLayout = findViewById<TableLayout>(R.id.tlMarcado)
for (rowIndex in 0 until tableLayout.childCount) {
val tableRow = tableLayout.getChildAt(rowIndex) as TableRow
tableRow.setOnClickListener {
val rowIndex = tableLayout.indexOfChild(tableRow)
// Handle row click event with rowIndex
onTableRowClicked(rowIndex)
}
for (cellIndex in 0 until tableRow.childCount) {
val cellView = tableRow.getChildAt(cellIndex)
cellView.setOnClickListener {
val rowIndex = tableLayout.indexOfChild(tableRow)
// Handle cell click event with rowIndex and cellIndex
onTableCellClicked(rowIndex, cellIndex)
}
}
}
}