Reputation: 597
I would like to change the textColor of a lot of textViews, and set in to red. For now, here is my xml code, with the first textView changed to red :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"
android:orientation="vertical" >
<TextView
android:id="@+id/data1_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude"
android:textColor="#FF0000"/>
<TextView
android:id="@+id/data1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude_val" />
<TextView
android:id="@+id/data2_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude" />
<TextView
android:id="@+id/data2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude_val" />
<TextView
android:id="@+id/data3_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Altitude" />
<TextView
android:id="@+id/data3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Altitude_val" />
<TextView
android:id="@+id/data4_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vitesse GPS" />
<TextView
android:id="@+id/data4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vitesse GPS_val" />
</LinearLayout>
As you can see, the textViews are inside a Linear Layout, but sadly, you can't just use android:textColor
in the Layout parameters.
Here I displayed only 4 textViews, but there are a lot more, and I wouldn't like to give the parameter to each one by one (imagine if I want to change the color after that...)
Is there some sort of tag to indicate a style to a bunch of textViews?
Upvotes: 3
Views: 2335
Reputation: 51
Something like this will work for you.
private void setTextColor()
{
/*put id(s) of all the TextView you want to change the color of,
In an array and call setTextColor() for all the array items*/
int textViews[]={R.id.data1_text,R.id.data1,R.id.data2_text,R.id.data2,R.id.data3_text,R.id.data3};
for(int i=0;i<textViews.length;i++)
((TextView)findViewById(textViews[i])).setTextColor(Color.RED);
}
Now,call setTextColor()
from your onCreate()
method.
You also can create similar functions to set text Size and other good stuffs.
Upvotes: 4
Reputation: 71
You can add color to your edit text programatically like:
EditText et = (EditText) findViewById(R.id.edit1);
// to set text color using RGB code
et.setTextColor(Color.parseColor("#FF0000"));
You can do it in XML as well like:
android:textColor="#FF0000"
Or better if you first create a xml definition for colors in /res/values/mycolor.xml like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
</resources>
and set color of editText like :
android:textColor="@android:color/white"
and programatically like :
et.setTextColor(R.color.white);
Hope it helps;
Upvotes: 0