Skip to content

Commit

Permalink
Merge pull request #893
Browse files Browse the repository at this point in the history
Dynamically set background of header preferences
  • Loading branch information
siavash79 authored Sep 29, 2024
2 parents ffe9e2f + 9af9a12 commit c3991c0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,59 +1,40 @@
package sh.siava.pixelxpert.ui.preferences;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceViewHolder;

import sh.siava.pixelxpert.R;

public class MaterialPreferenceMain extends Preference {

private static final int POSITION_TOP = 0;
private static final int POSITION_DEFAULT = 1;
private static final int POSITION_SINGLE = 2;
private static final int POSITION_BOTTOM = 3;

private int position = POSITION_DEFAULT;

public MaterialPreferenceMain(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
initResource();
}

public MaterialPreferenceMain(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
initResource();
}

public MaterialPreferenceMain(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
initResource();
}

public MaterialPreferenceMain(@NonNull Context context) {
super(context);
initResource();
}

private void init(Context context, @Nullable AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MaterialPreferenceMain, 0, 0);
try {
position = a.getInt(R.styleable.MaterialPreferenceMain_position, POSITION_DEFAULT);
} finally {
a.recycle();
}
}
initResource();
}

private void initResource() {
setLayoutResource(R.layout.custom_preference_main);
}
Expand All @@ -78,21 +59,27 @@ public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
}
}

// Set background drawable based on the position attribute
switch (position) {
case POSITION_TOP:
holder.itemView.setBackgroundResource(R.drawable.container_top);
break;
case POSITION_SINGLE:
setBackgroundResource(holder);
}

private void setBackgroundResource(PreferenceViewHolder holder) {
PreferenceGroup parent = getParent();

if (parent != null) {
int itemCount = parent.getPreferenceCount();
int position = getOrder();

if (itemCount == 1) {
holder.itemView.setBackgroundResource(R.drawable.container_single);
break;
case POSITION_BOTTOM:
holder.itemView.setBackgroundResource(R.drawable.container_bottom);
break;
case POSITION_DEFAULT:
default:
holder.itemView.setBackgroundResource(R.drawable.container_mid);
break;
} else if (itemCount > 1) {
if (position == 0) {
holder.itemView.setBackgroundResource(R.drawable.container_top);
} else if (position == itemCount - 1) {
holder.itemView.setBackgroundResource(R.drawable.container_bottom);
} else {
holder.itemView.setBackgroundResource(R.drawable.container_mid);
}
}
}
}
}
9 changes: 0 additions & 9 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,4 @@
<attr name="textNoResults" format="string" />
<attr name="textClearHistory" format="string" />
</declare-styleable>

<declare-styleable name="MaterialPreferenceMain">
<attr name="position" format="string">
<enum name="topItem" value="0" />
<enum name="defaultItem" value="1" />
<enum name="singleItem" value="2" />
<enum name="bottomItem" value="3" />
</attr>
</declare-styleable>
</resources>
6 changes: 0 additions & 6 deletions app/src/main/res/xml/header_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
app:fragment="sh.siava.pixelxpert.ui.activities.SettingsActivity$QuickSettingsFragment"
app:icon="@drawable/ic_settings_quicksettings"
app:key="quicksettings_header"
app:position="topItem"
app:summary="@string/qs_panel_category_summary"
app:title="@string/qs_panel_category_title"
search:ignore="true" />
Expand All @@ -40,7 +39,6 @@
app:fragment="sh.siava.pixelxpert.ui.activities.SettingsActivity$ThemingFragment"
app:icon="@drawable/ic_settings_themes"
app:key="theming_header"
app:position="bottomItem"
app:summary="@string/theme_customization_summary"
app:title="@string/theme_customization_category"
search:ignore="true" />
Expand All @@ -58,7 +56,6 @@
app:fragment="sh.siava.pixelxpert.ui.activities.SettingsActivity$StatusbarFragment"
app:icon="@drawable/ic_settings_statusbar"
app:key="statusbar_header"
app:position="topItem"
app:summary="@string/statusbar_header_summary"
app:title="@string/statusbar_header"
search:ignore="true" />
Expand All @@ -68,7 +65,6 @@
app:fragment="sh.siava.pixelxpert.ui.activities.SettingsActivity$NavFragment"
app:icon="@drawable/ic_settings_navigation"
app:key="nav_header"
app:position="bottomItem"
app:summary="@string/nav_header_summary"
app:title="@string/nav_header"
search:ignore="true" />
Expand All @@ -86,7 +82,6 @@
app:fragment="sh.siava.pixelxpert.ui.activities.SettingsActivity$DialerFragment"
app:icon="@drawable/ic_phone"
app:key="dialer_header"
app:position="topItem"
app:summary="@string/dialer_header_summary"
app:title="@string/dialer_header"
search:ignore="true" />
Expand Down Expand Up @@ -114,7 +109,6 @@
app:fragment="sh.siava.pixelxpert.ui.activities.SettingsActivity$MiscFragment"
app:icon="@drawable/ic_settings_misc"
app:key="misc_header"
app:position="bottomItem"
app:summary="@string/misc_header_summary"
app:title="@string/misc_header"
search:ignore="true" />
Expand Down

0 comments on commit c3991c0

Please sign in to comment.