Tutorial :How to allow users to choose themes in android list view?



Question:

I have a listview with two labels, title and subtitle. I want to have dark and light background as user options. Title has textAppearanceMedium and subtitle has textAppearanceSmall. I want the style, MyTheme.Dark to have white color text and MyTheme.Light to have black color text. Is there a way to define multiple textAppearance attribute for the same TextView widget?

<style name="MyTheme.Dark">      <item name="android:windowBackground">@color/black</item>      <item name="android:colorBackground">@color/black</item>      <item name="android:background">@color/black</item>      <item name="android:divider">@color/white</item>          --cannot put textAppearance here since it is different for title and subtitle      </style>  


Solution:1

For future reference...

In values/themes.xml:

<style name="MyTheme.Dark">      <item name="textColorTitle">@color/white</item>  </style>  <style name="MyTheme.Light">      <item name="textColorTitle">@color/black</item>  </style>  

In values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>  <resources>      <attr          name="textColorTitle"          format="reference|color" />  </resources>  

In layout/my_list_row.xml, use textColorTitle value for your text color, like this:

android:textColor="?textColorTitle"  

Let me know if it doesn't work. It works for me.


Solution:2

I'm a beginner with Android myself and I'm not sure if I'm not making an ass of myself here, but why don't you use the android:textColor property?

<style name="MyTheme.Dark">      <item name="android:windowBackground">@color/black</item>      <item name="android:colorBackground">@color/black</item>      <item name="android:background">@color/black</item>      <item name="android:divider">@color/white</item>      <item name="android:textColor">@color/white</item>  </style>  


Solution:3

I think best way is to have different layout xml for different colors and themes. Load the layout xml based on user choice. I do not like this but I have not found any alternatives.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »