In my app I am having a ViewPager
in the main-screen out of which 2 fragments have custom ListViews. The app has different themes with different text colors in each theme.
I have implemented all the UI attributes from the XML itself under different themes and is working fine except for the TextColor in the custom ListViews inside ViewPager which I am controlling from the code.
Question : How can I control the TextColor from the XMl ?
I have tried setting the color attributes for the 3 themes as TextStyle
, TextAppearence
, TextColor
etc. But nothing worked while the same code was working when these custom ListViews were not within the ViewPager.
I am not adding any specific codes as I have tried many methods. But as an example, this is the code which I used for setting the TextColor.
android:textColor="?attr/listItemContentTextColor"
where the attribute is,
<attr name="listItemContentTextColor" format="color" />
which is specified in the themes as,
<item name="listItemContentTextColor">@android:color/white</item>
Is there any way I can control it from the XML itself?
Well, I found the answer to my Question.
I couldn't control it from the XML itself, but I could implement it dynamically instead of hardcoding it. :)
The problem was only with those custom listviews within the ViewPager which was not taking the attributes from the XML. This is how I solved it.
In /values/attr.xml
<attr name="listFontColor" format="color" />
And in each Theme
, I set the listFontColor attribute values accordingly.
<item name="listFontColor">@android:color/white</item>
Instead of checking with each theme in the code, I fetched the current theme and assigned the font color in the code in my Adapter
like this,
//dynamically fetching the theme's list font Color and setting it to textview
TypedArray a = ((MyActivity)context).getTheme().obtainStyledAttributes(((MyApplication)(context.getApplicationContext())).getcurrentTheme(), new int[] {R.attr.listFontColor});
int attributeResourceId = a.getResourceId(0, 0);
listTitle.setTextColor(context.getResources().getColor(attributeResourceId));
Thanks for the reference :)