Luke Markets
Luke Markets

Reputation: 11

custom theme not applied on first SwitchCompat item on android listview

I have a listview with each items containing a switchcompat widget with custom switch style. The first time the listview was populated, the custom theme was not applying on the first item's switchcompat widget on the list. But it works fine the second time the list was populated, (list adapter being cleared and repopulated)

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_manage_reward_active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="false"
android:text="active"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#fff"
android:theme="@style/RewardSwitch" />

<style name="RewardSwitch" parent="Theme.AppCompat.Light">
        <item name="colorControlActivated">#E6E329</item>
        <item name="colorSwitchThumbNormal">#f1f1f1</item>
        <item name="android:colorForeground">#ffcccccc</item>
</style>

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder v;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(R.layout.manage_reward_item, null);
        v = new ViewHolder();
        v.switchRewardStatus = (SwitchCompat) convertView
                .findViewById(R.id.switch_manage_reward_active);
        convertView.setTag(v);
    } else {
        v = (ViewHolder) convertView.getTag();
    }
    final Reward currentReward = getItem(position);
    v.switchRewardStatus.setChecked(currentReward.getActive());
    return convertView;
}

SEE SCREENSHOT switchcompat list item

Upvotes: 1

Views: 105

Answers (2)

Jalalkun
Jalalkun

Reputation: 181

try to recreate the activity. i do that for change theme to dark theme

Upvotes: 0

Autocrab
Autocrab

Reputation: 3787

Because you inflate with null parent parameter

convertView = inflater.inflate(R.layout.manage_reward_item, null);

Instead you should inflate with parent parameter, which has information about theme:

convertView = inflater.inflate(R.layout.manage_reward_item, parent, false);

Upvotes: 0

Related Questions