Question

As a programmer for a year or two now (newer to android and XML however), there has to be an easier way to do this, or, optimize it.

Basically, I want to make the color of my TextViews in my TableLayout color black, and apparently you have to do it through the JAVA rather than in XML. Heres my code, which works, but it seems like a LOT. Is there a way to shrink this with a loop or anything?

CLASS FILE

public class TabsSterile extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sterileconditioners);

    TextView tv1 = (TextView) findViewById(R.id.tv1);
    tv1.setTextColor(0xFF000000);

    TextView tv2 = (TextView) findViewById(R.id.tv2);
    tv2.setTextColor(0xFF000000);

    TextView tv3 = (TextView) findViewById(R.id.tv3);
    tv3.setTextColor(0xFF000000);

    TextView tv4 = (TextView) findViewById(R.id.tv4);
    tv4.setTextColor(0xFF000000);

    TextView tv5 = (TextView) findViewById(R.id.tv5);
    tv5.setTextColor(0xFF000000);

    TextView tv6 = (TextView) findViewById(R.id.tv6);
    tv6.setTextColor(0xFF000000);

    TextView tv7 = (TextView) findViewById(R.id.tv7);
    tv7.setTextColor(0xFF000000);

    TextView tv8 = (TextView) findViewById(R.id.tv8);
    tv8.setTextColor(0xFF000000);

    TextView tv9 = (TextView) findViewById(R.id.tv9);
    tv9.setTextColor(0xFF000000);

    TextView tv10 = (TextView) findViewById(R.id.tv10);
    tv10.setTextColor(0xFF000000);

    TextView tv11 = (TextView) findViewById(R.id.tv11);
    tv11.setTextColor(0xFF000000);

    TextView tv12 = (TextView) findViewById(R.id.tv12);
    tv12.setTextColor(0xFF000000);

    TextView tv13 = (TextView) findViewById(R.id.tv13);
    tv13.setTextColor(0xFF000000);

    TextView tv14 = (TextView) findViewById(R.id.tv14);
    tv14.setTextColor(0xFF000000);

    TextView tv15 = (TextView) findViewById(R.id.tv15);
    tv15.setTextColor(0xFF000000);

    TextView tv16 = (TextView) findViewById(R.id.tv16);
    tv16.setTextColor(0xFF000000);

    TextView tv17 = (TextView) findViewById(R.id.tv17);
    tv17.setTextColor(0xFF000000);

    TextView tv18 = (TextView) findViewById(R.id.tv18);
    tv18.setTextColor(0xFF000000);

    TextView tv19 = (TextView) findViewById(R.id.tv19);
    tv19.setTextColor(0xFF000000);

    TextView tv20 = (TextView) findViewById(R.id.tv20);
    tv20.setTextColor(0xFF000000);

    TextView tv21 = (TextView) findViewById(R.id.tv21);
    tv21.setTextColor(0xFF000000);

    TextView tv22 = (TextView) findViewById(R.id.tv22);
    tv22.setTextColor(0xFF000000);

    TextView tv23 = (TextView) findViewById(R.id.tv23);
    tv23.setTextColor(0xFF000000);

    TextView tv24 = (TextView) findViewById(R.id.tv24);
    tv24.setTextColor(0xFF000000);

    TextView tv25 = (TextView) findViewById(R.id.tv25);
    tv25.setTextColor(0xFF000000);

    TextView tv26 = (TextView) findViewById(R.id.tv26);
    tv26.setTextColor(0xFF000000);

    TextView tv27 = (TextView) findViewById(R.id.tv27);
    tv27.setTextColor(0xFF000000);

    TextView tv28 = (TextView) findViewById(R.id.tv28);
    tv28.setTextColor(0xFF000000);

    TextView tv29 = (TextView) findViewById(R.id.tv29);
    tv29.setTextColor(0xFF000000);

    TextView tv30 = (TextView) findViewById(R.id.tv30);
    tv30.setTextColor(0xFF000000);

    }
}

XML FILE

<TableLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#1155ff" >
    <TableRow android:layout_margin="1dp" android:background="#77bbff">
        <TextView android:text="Model"
            android:id="@+id/tv1" />
        <TextView android:text="Tank Capacity (gal)"
            android:id="@+id/tv2" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2010 Sterile Conditioner"
            android:id="@+id/tv3" />
        <TextView android:text="Up to 5,000"
            android:id="@+id/tv4" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2030 Sterile Conditioner"
            android:id="@+id/tv5" />
        <TextView android:text="Up to 5,000 w/ HEPA"
            android:id="@+id/tv6" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2036 Sterile Conditioner"
            android:id="@+id/tv7" />
        <TextView android:text="5,000 to 7,000"
            android:id="@+id/tv8" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2072HF Sterile Conditioner"
            android:id="@+id/tv9" />
        <TextView android:text="7,000 to 18,000"
            android:id="@+id/tv10" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2108-2020 Sterile Conditioner"
            android:id="@+id/tv11" />
        <TextView android:text="18,000 to 40,000"
            android:id="@+id/tv12" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2144-2020 Sterile Conditioner"
            android:id="@+id/tv13" />
        <TextView android:text="40,000 to 100,000"
            android:id="@+id/tv14" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2200 Sterile Conditioner"
            android:id="@+id/tv15" />
        <TextView android:text="100,000 +"
            android:id="@+id/tv16" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="Consult Fuller"
            android:id="@+id/tv17" />
        <TextView android:text="100,000 +"
            android:id="@+id/tv18" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2001 Air Vent"
            android:id="@+id/tv19" />
        <TextView android:text="Up to 18,000"
            android:id="@+id/tv20" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2002 Air Vent"
            android:id="@+id/tv21" />
        <TextView android:text="40,000 +"
            android:id="@+id/tv22" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="FG 2003 Air Vent"
            android:id="@+id/tv23" />
        <TextView android:text="40,000 +"
            android:id="@+id/tv24" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="Central Systems"
            android:id="@+id/tv25" />
        <TextView android:text="Consult Fuller"
            android:id="@+id/tv26" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="Air Circulators"
            android:id="@+id/tv27" />
        <TextView android:text="Consult Fuller"
            android:id="@+id/tv28" />
    </TableRow>
    <TableRow android:layout_margin="1dp" android:background="#ffffff">
        <TextView android:text="Tank Head Space Fixtures"
            android:id="@+id/tv29" />
        <TextView android:text="Consult Fuller"
            android:id="@+id/tv30" />
    </TableRow>
</TableLayout>

</ScrollView>
Was it helpful?

Solution

One way of doing is by using array like this, suppose if i have 3 textviews-

// Declared array at class level

    textArray = new TextView[3];
    textView[0] = (TextView) findViewById(R.id.tv1);
    textView[1] = (TextView) findViewById(R.id.tv2);
    textView[2] = (TextView) findViewById(R.id.tv2);

for (int i = 0; i < textArray .length; i++) 
{
    textView[i].setTextColor(0xFF000000);   
}

OTHER TIPS

You can simply set the text color in xml using android:textColor="#000000". See TextView docs for a list of all the attributes.

I'd recommend using a custom style or theme.

This will allow you to only define the text attributes once, and either automatically apply them to all views (theme) or selectively via the style. All this can be done in XML, without the need to write additionak Java code.

This will get you started: http://www.vogella.com/tutorials/AndroidStylesThemes/article.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top