Home > OS >  Change the background of a table row onClick
Change the background of a table row onClick

Time:07-21

I have a table in Android Studio and when I click on a cell, it should turn green, or any other color, that is not important, but I don't know how to implement that. I only have the xml file now and I am unsure what to add exactly and where. It's the first time when I try to implement something like that and I don't know where I should start from or what I need for that.

Here is the xml:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:stretchColumns="0,1,2">

    <TableRow
        android:id="@ id/blank"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:background="#FFFFFF">

        <TableRow

            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#000000">

        </TableRow>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:background="#000000"

        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="0"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_column="2"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text="Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:background="#000000">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="0"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text=" Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text=" Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_column="2"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text=" Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>


    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:background="#000000">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="0"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text=" Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text=" Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_column="2"
            android:layout_margin="1dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text=" Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>


</TableLayout>

Could someone help, please? Many many thanks.

CodePudding user response:

Maybe it is useful for you to insert an EditText tag in every row so you can turn its color as you want like "editTextVar.setBackground('some-color-code')".

I hope the best try for you.

CodePudding user response:

Use it:

your_object.setBackgroundColor(getResources().getColor(R.color.your_color_in_colors));

Or it(Custom colors):

your_object.setBackgroundColor(Color.WHITE);
  • Related