Skip to content

Commit

Permalink
SingleSelectionPreferences : test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishan09811 authored Jan 26, 2024
1 parent 7026597 commit c701c4d
Showing 1 changed file with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ public class SingleSelectionPreferences extends PreferenceCategory implements Pr
private final Drawable transparent = new ColorDrawable(Color.TRANSPARENT);
private final Drawable doneDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_done);

// New attributes for handling entries and entryValues
private CharSequence[] entries;
private CharSequence[] entryValues;

public SingleSelectionPreferences(@NonNull Context context) {
super(context);
init();
}

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

public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
Expand All @@ -40,11 +46,13 @@ public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeS
super(context, attrs, defStyleAttr, defStyleRes);
}

{
private void init() {
try {
TypedArray color = getContext().obtainStyledAttributes(new int[]{
android.R.attr.textColorSecondary
});
entries = getEntries();
entryValues = getEntryValues();
doneDrawable.setTint(color.getColor(0, Color.RED));
color.recycle();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Expand All @@ -55,6 +63,19 @@ public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeS
}
}

@Override
public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
super.onBindViewHolder(holder);

// Set summary based on selected entry
if (entries != null && entryValues != null) {
CharSequence selectedEntry = getEntry();
if (selectedEntry != null) {
holder.setSummary(selectedEntry);
}
}
}

@Override
public void onAttached() {
super.onAttached();
Expand Down Expand Up @@ -96,7 +117,7 @@ private void showMaterialDialog(final int selectedIndex) {

new MaterialAlertDialogBuilder(getContext())
.setTitle(getTitle())
.setSingleChoiceItems(entriesArray, selectedIndex, (dialog, which) -> {
.setSingleChoiceItems(entries, selectedIndex, (dialog, which) -> {
updateSelectedPreference(which);
dialog.dismiss();
})
Expand All @@ -115,4 +136,22 @@ private void updateSelectedPreference(int selectedIndex) {

callChangeListener(selectedIndex);
}

// New method to get entries from the XML attribute
private CharSequence[] getEntries() {
if (this instanceof ListPreference) {
return ((ListPreference) this).getEntries();
} else {
return null; // Handle appropriately for other types of preferences
}
}

// New method to get entryValues from the XML attribute
private CharSequence[] getEntryValues() {
if (this instanceof ListPreference) {
return ((ListPreference) this).getEntryValues();
} else {
return null; // Handle appropriately for other types of preferences
}
}
}

0 comments on commit c701c4d

Please sign in to comment.