Skip to content

Commit 58a0e29

Browse files
alperozturk96tobiasKaminsky
authored andcommitted
Use M3 Switch
Signed-off-by: alperozturk <[email protected]>
1 parent dbfaffa commit 58a0e29

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSwitchPreference.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,51 @@
66
*/
77
package it.niedermann.owncloud.notes.branding;
88

9-
import android.annotation.SuppressLint;
109
import android.content.Context;
1110
import android.util.AttributeSet;
1211
import android.view.View;
1312
import android.view.ViewGroup;
14-
import android.widget.Switch;
1513

1614
import androidx.annotation.ColorInt;
15+
import androidx.annotation.NonNull;
1716
import androidx.annotation.Nullable;
1817
import androidx.preference.PreferenceViewHolder;
19-
import androidx.preference.SwitchPreference;
18+
import androidx.preference.SwitchPreferenceCompat;
2019

21-
public class BrandedSwitchPreference extends SwitchPreference implements Branded {
20+
import com.google.android.material.materialswitch.MaterialSwitch;
21+
22+
import it.niedermann.owncloud.notes.R;
23+
24+
public class BrandedSwitchPreference extends SwitchPreferenceCompat implements Branded {
2225

2326
@ColorInt
2427
private Integer mainColor = null;
2528

26-
@SuppressLint("UseSwitchCompatOrMaterialCode")
2729
@Nullable
28-
private Switch switchView;
30+
private MaterialSwitch switchView;
2931

3032
public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
3133
super(context, attrs, defStyleAttr, defStyleRes);
34+
setWidgetLayoutResource(R.layout.preference_switch);
3235
}
3336

3437
public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
3538
super(context, attrs, defStyleAttr);
39+
setWidgetLayoutResource(R.layout.preference_switch);
3640
}
3741

3842
public BrandedSwitchPreference(Context context, AttributeSet attrs) {
3943
super(context, attrs);
44+
setWidgetLayoutResource(R.layout.preference_switch);
4045
}
4146

4247
public BrandedSwitchPreference(Context context) {
4348
super(context);
49+
setWidgetLayoutResource(R.layout.preference_switch);
4450
}
4551

4652
@Override
47-
public void onBindViewHolder(PreferenceViewHolder holder) {
53+
public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
4854
super.onBindViewHolder(holder);
4955

5056
if (holder.itemView instanceof ViewGroup) {
@@ -65,7 +71,7 @@ public void applyBrand(@ColorInt int color) {
6571
private void applyBrand() {
6672
if (switchView != null) {
6773
final var util = BrandingUtil.of(mainColor, getContext());
68-
util.platform.colorSwitch(switchView);
74+
util.material.colorMaterialSwitch(switchView);
6975
}
7076
}
7177

@@ -76,19 +82,19 @@ private void applyBrand() {
7682
* @return A Switch class or null
7783
* @see <a href="https://gist.github.com/marchold/45e22839eb94aa14dfb5">Source</a>
7884
*/
79-
private Switch findSwitchWidget(View view) {
80-
if (view instanceof Switch) {
81-
return (Switch) view;
85+
private MaterialSwitch findSwitchWidget(View view) {
86+
if (view instanceof MaterialSwitch) {
87+
return (MaterialSwitch) view;
8288
}
8389
if (view instanceof ViewGroup viewGroup) {
8490
for (int i = 0; i < viewGroup.getChildCount(); i++) {
8591
final var child = viewGroup.getChildAt(i);
8692
if (child instanceof ViewGroup) {
87-
@SuppressLint("UseSwitchCompatOrMaterialCode") final var result = findSwitchWidget(child);
93+
final var result = findSwitchWidget(child);
8894
if (result != null) return result;
8995
}
90-
if (child instanceof Switch) {
91-
return (Switch) child;
96+
if (child instanceof MaterialSwitch) {
97+
return (MaterialSwitch) child;
9298
}
9399
}
94100
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Nextcloud - Android Client
4+
~
5+
~ SPDX-FileCopyrightText: 2024 Alper Ozturk <[email protected]>
6+
~ SPDX-License-Identifier: AGPL-3.0-or-later
7+
-->
8+
<com.google.android.material.materialswitch.MaterialSwitch
9+
xmlns:android="http://schemas.android.com/apk/res/android"
10+
android:id="@+id/switchWidget"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:focusable="false"
14+
android:clickable="false"
15+
android:background="@null" />

app/src/main/res/values/styles.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
~ SPDX-FileCopyrightText: 2022 Kévin Cocchi <[email protected]>
88
~ SPDX-License-Identifier: GPL-3.0-or-later
99
-->
10-
<resources xmlns:android="http://schemas.android.com/apk/res/android">
10+
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
1111

1212
<style name="BaseTheme" parent="Theme.Material3.DayNight.NoActionBar">
1313
<item name="colorPrimary">@color/primary</item>
@@ -99,4 +99,9 @@
9999
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
100100
<item name="postSplashScreenTheme">@style/AppTheme</item>
101101
</style>
102+
103+
<style name="Preference.SwitchPreferenceCompat" parent="@style/Preference.SwitchPreferenceCompat.Material"
104+
tools:ignore="ResourceCycle">
105+
<item name="widgetLayout">@layout/preference_switch</item>
106+
</style>
102107
</resources>

0 commit comments

Comments
 (0)