b10hazard b10hazard - 23 days ago 7
Java Question

How do I change the background color for a column of my Android app

My Android app has a table with seven columns. I'd like to change the background color of these columns when the content in the table is updated. How do I specify columns in the main.xml and how do I change their background color? Thanks.

Edit main.xml posted...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#CDCDCD" android:orientation="vertical">
<TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="100" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1"></TextView>
<TextView android:text="100" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2"></TextView>
<TextView android:text="100" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3"></TextView>
<TextView android:text="100" android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="4"></TextView>
<TextView android:text="100" android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="5"></TextView>
<TextView android:text="100" android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="6"></TextView>
<TextView android:text="100" android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="7"></TextView>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="100" android:id="@+id/textView11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1"></TextView>
<TextView android:text="100" android:id="@+id/textView12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2"></TextView>
<TextView android:text="100" android:id="@+id/textView13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3"></TextView>
<TextView android:text="100" android:id="@+id/textView14" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="4"></TextView>
<TextView android:text="100" android:id="@+id/textView15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="5"></TextView>
<TextView android:text="100" android:id="@+id/textView16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="6"></TextView>
<TextView android:text="100" android:id="@+id/textView17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="7"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>


EDIT:

This is an example of why I want to highlight the entire column instead of the individual textviews. When I highlight the individual textviews they are different sizes so they do not expand to the width of the widest textview in that column. Unless anyone knows how to change the size of my text views to fit the size of the widest textview in each column. If anyone does know this, please post.

uneven highlighting

EDIT:
Results of adding layout_weight properties to the TableRow tag...

enter image description here

I also tried the same thing but added the layout_weight property to the TextView tag instead. Here is the result...

enter image description here

Answer

To give your textviews equal width and therefore fix your higlighting issue you should look at the layout_weight property

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     android:background="#CDCDCD" 
     android:orientation="vertical">
  <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent"              android:layout_height="wrap_content">
     <TableRow 
        android:id="@+id/tableRow1"
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:layout_weight="0.5">
        <TextView android:text="100" android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_column="1"></TextView>
    </TableRow>
    <TableRow   
       android:id="@+id/tableRow2" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.5">
        <TextView android:text="100" android:id="@+id/textView11" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_column="1"></TextView>            
    </TableRow>
  </TableLayout>
</LinearLayout>

Now I'm not at my development PC so can't verify this (and fix my formatting), but it should work I'll check it later.