Reputation: 7932
I have a Layout that is used in couple different places.
This layout itself has a textview, some buttons and a progressbar.
<LinearLayout id="reuse">
<TextView/>
<ProgressBar/>
<Buttons/>
</LinearLayout>
// in other places
<include layout="reuse"/> // text color blue here
// in other places
<include layout="reuse" /> //text color gree here
Now, depending on where the layout is included, i want to specify a different text color for the textview.
How can this be done? I tried specifying textcolor in the include that doesn't seem to help?
Upvotes: 1
Views: 275
Reputation: 7936
You cannot make it via xml files but you can indirectly achieve this.
First Option:
You can do it programmatically. For example:
private void setTextColors(ViewGroup viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof TextView) {
((TextView) view).setTextColor(ContextCompat.getColor(this, android.R.color.holo_blue_bright));
} else if (view instanceof Button) {
((Button) view).setTextColor(ContextCompat.getColor(this, android.R.color.holo_blue_bright));
}else if (view instanceof EditText) {
((EditText) view).setTextColor(ContextCompat.getColor(this, android.R.color.holo_blue_bright));
} else if (view instanceof LinearLayout) {
setTextColors((LinearLayout) view);
}else if (view instanceof RelativeLayout) {
setTextColors((RelativeLayout) view);
}else if (view instanceof FrameLayout) {
setTextColors((FrameLayout) view);
}
}
}
NOTE: This method is a simple working example. You can modify it according to your needs.
When you called this method, you give parameter as your parent layout(like LinearLayout). And in the loop, checks every child view of your Parent View and if they are instance of Button, TextView or EditText, sets the color you wanted. Also recursively sets the color of your Child view groups such as:
<LinearLayout >
<TextView/>
<ProgressBar/>
<Button/>
<RelativeLayout>
<TextView/>
<ProgressBar/>
<Button/>
</LinearLayout>
</LinearLayout>
Second Option:
You can create a style in your styles.xml file for textViews and give your text color into this style. After that, all you need to do set your style to your TextViews in your layout xml.
For example:
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
Upvotes: 0
Reputation: 36
The only way I can think of doing this is programmatically, just use the reference of linear layout "reuse" and get the textview from it using reuse.findViewById() and manipulating the properties yourself.
Upvotes: 1