0

I have this layout for my ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:orientation="horizontal"
    android:id="@+id/llRoot"
    android:theme="@style/GreyList"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
            android:id="@+id/tvData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:text="@string/ph_data">

    ...
</LinearLayout>

and this styles.xml:

<resources>
    <style name="GreyList" parent="android:Widget.TextView">
        <item name="android:textColorPrimary">@android:color/darker_gray</item>
        <item name="android:textColorSecondary">@android:color/darker_gray</item>
        <item name="android:textColorTertiary">@android:color/darker_gray</item>
        <item name="android:textColorHint">@android:color/darker_gray</item>
    </style>
</resources>

Which results in a list with grey items. However, I want to set / unset the theme based on data in the set:

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
     ...
     if (itemneedstobegray) {
          // this is where I am stuck
          convertView.findViewById(R.id.llRoot).setTheme("GreyList");
     }
     ...
}

But there seems to be no way to set a theme on a LinearLayout in code. How can I do that?

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Just so you know, it’s only possible to change the theme (of an Activity) in the `onCreate` method and only before `super` has been called. In your case I'm not sure if you can dynamically set a theme on a list item. – ChristianoBolla Jun 12 '20 at 09:04
  • @ChristianoBolla okay, thanks. Is there another way to achieve this then? Without having to call `setTextColor` on all `TextViews` in the `LinearLayout` ? – Bart Friederichs Jun 12 '20 at 09:05
  • When using separate layout XML files, I can get what I need, so I reckon different themes per listview item is supported. – Bart Friederichs Jun 12 '20 at 09:09
  • May be this can help you https://stackoverflow.com/questions/33987678/programmatically-change-the-value-of-a-color-resource-obtained-from-api-response – Jignesh Mayani Jun 12 '20 at 09:35
  • @ChristianoBolla Not true. The theme can be changed after super call too. – Mike087 Jun 03 '22 at 23:39

0 Answers0