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;
}
Because you inflate with
null
parent parameterInstead you should inflate with parent parameter, which has information about theme: