Skip to content

Commit 0d023d9

Browse files
committed
Add preference to the example app.
1 parent bb487b3 commit 0d023d9

File tree

4 files changed

+74
-31
lines changed

4 files changed

+74
-31
lines changed

example/src/main/java/de/mrapp/android/dialog/example/PreferenceFragment.java

+64-30
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import androidx.preference.PreferenceFragmentCompat;
3636
import androidx.recyclerview.widget.RecyclerView;
3737
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
38+
3839
import de.mrapp.android.dialog.EditTextDialog;
3940
import de.mrapp.android.dialog.MaterialDialog;
4041
import de.mrapp.android.dialog.ProgressDialog;
@@ -177,7 +178,16 @@ private void initializeListDialog() {
177178
MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity());
178179
configureHeaderDialogBuilder(builder);
179180
configureButtonBarDialogBuilder(builder);
180-
builder.setItems(R.array.list_items, createSingleChoiceListener());
181+
182+
if (shouldListItemIconsBeShown()) {
183+
int[] iconResourceIds = new int[]{R.drawable.ic_info_24dp, R.drawable.ic_info_24dp,
184+
R.drawable.ic_info_24dp};
185+
builder.setItems(R.array.list_items, iconResourceIds, createSingleChoiceListener());
186+
builder.setItemIconTint(ContextCompat.getColor(getActivity(), R.color.color_accent));
187+
} else {
188+
builder.setItems(R.array.list_items, createSingleChoiceListener());
189+
}
190+
181191
listDialog = builder.create();
182192
}
183193

@@ -188,7 +198,17 @@ private void initializeSingleChoiceListDialog() {
188198
MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity());
189199
configureHeaderDialogBuilder(builder);
190200
configureButtonBarDialogBuilder(builder);
191-
builder.setSingleChoiceItems(R.array.list_items, 0, createSingleChoiceListener());
201+
202+
if (shouldListItemIconsBeShown()) {
203+
int[] iconResourceIds = new int[]{R.drawable.ic_info_24dp, R.drawable.ic_info_24dp,
204+
R.drawable.ic_info_24dp};
205+
builder.setSingleChoiceItems(R.array.list_items, iconResourceIds, 0,
206+
createSingleChoiceListener());
207+
builder.setItemIconTint(ContextCompat.getColor(getActivity(), R.color.color_accent));
208+
} else {
209+
builder.setSingleChoiceItems(R.array.list_items, 0, createSingleChoiceListener());
210+
}
211+
192212
singleChoiceListDialog = builder.create();
193213
}
194214

@@ -199,8 +219,18 @@ private void initializeMultipleChoiceListDialog() {
199219
MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity());
200220
configureHeaderDialogBuilder(builder);
201221
configureButtonBarDialogBuilder(builder);
202-
builder.setMultiChoiceItems(R.array.list_items, new boolean[]{true, false, false},
203-
createMultiChoiceListener());
222+
223+
if (shouldListItemIconsBeShown()) {
224+
int[] iconResourceIds = new int[]{R.drawable.ic_info_24dp, R.drawable.ic_info_24dp,
225+
R.drawable.ic_info_24dp};
226+
builder.setMultiChoiceItems(R.array.list_items, iconResourceIds,
227+
new boolean[]{true, false, false}, createMultiChoiceListener());
228+
builder.setItemIconTint(ContextCompat.getColor(getActivity(), R.color.color_accent));
229+
} else {
230+
builder.setMultiChoiceItems(R.array.list_items, new boolean[]{true, false, false},
231+
createMultiChoiceListener());
232+
}
233+
204234
multipleChoiceListDialog = builder.create();
205235
}
206236

@@ -435,9 +465,8 @@ public boolean onPreferenceClick(final Preference preference) {
435465
* Creates and returns the rectangular reveal animation, which should be used to show or hide
436466
* dialogs.
437467
*
438-
* @param preference
439-
* The preference, which is used to show the dialog, as an instance of the class {@link
440-
* Preference}
468+
* @param preference The preference, which is used to show the dialog, as an instance of the class {@link
469+
* Preference}
441470
* @return The animation, which has been created, as an instance of the type {@link
442471
* DialogAnimation}
443472
*/
@@ -468,8 +497,7 @@ private DialogAnimation createRectangularRevealAnimation(@NonNull final Preferen
468497
* Creates and returns the circle reveal animation, which should be used to show or hide
469498
* dialogs.
470499
*
471-
* @param view
472-
* The view, which is used to show the dialog, as an instance of the class {@link View}
500+
* @param view The view, which is used to show the dialog, as an instance of the class {@link View}
473501
* @return The animation, which has been created, as an instance of the type {@link
474502
* DialogAnimation}
475503
*/
@@ -576,9 +604,8 @@ public boolean onPreferenceClick(final Preference preference) {
576604
* Creates and returns a listener, which allows to change the background color of the wizard
577605
* dialog, when its view pager is scrolled.
578606
*
579-
* @param wizardDialog
580-
* The wizard dialog as an instance of the class {@link WizardDialog}. The wizard dialog
581-
* may not be null
607+
* @param wizardDialog The wizard dialog as an instance of the class {@link WizardDialog}. The wizard dialog
608+
* may not be null
582609
* @return The listener, which has been created, as an instance of the type {@link
583610
* OnPageChangeListener}
584611
*/
@@ -626,8 +653,7 @@ public void onPageScrollStateChanged(final int state) {
626653
* Creates and returns a listener, which allows to reset a wizard dialog's header background to
627654
* default, when its animation is finished.
628655
*
629-
* @param wizardDialog
630-
* The wizard dialog as an instance of the class {@link WizardDialog}
656+
* @param wizardDialog The wizard dialog as an instance of the class {@link WizardDialog}
631657
* @return The listener, which has been created, as an instance of the type {@link
632658
* AnimationListener}
633659
*/
@@ -655,11 +681,9 @@ public void onAnimationEnd() {
655681
/**
656682
* Adds a new fragment to a builder, which allows to create wizard dialogs.
657683
*
658-
* @param builder
659-
* The builder, the fragment should be added to, as an instance of the class {@link
660-
* WizardDialog.Builder}
661-
* @param index
662-
* The index of the fragment, which should be added
684+
* @param builder The builder, the fragment should be added to, as an instance of the class {@link
685+
* WizardDialog.Builder}
686+
* @param index The index of the fragment, which should be added
663687
*/
664688
private void addFragment(@NonNull final WizardDialog.Builder builder, final int index) {
665689
Bundle arguments = new Bundle();
@@ -672,9 +696,8 @@ private void addFragment(@NonNull final WizardDialog.Builder builder, final int
672696
/**
673697
* Shows a specific toast and cancels a previous one, if existing.
674698
*
675-
* @param text
676-
* The text of the toast, which should be shown, as a {@link String}, or null, if no
677-
* toast should be shown
699+
* @param text The text of the toast, which should be shown, as a {@link String}, or null, if no
700+
* toast should be shown
678701
*/
679702
private void showToast(@Nullable final String text) {
680703
if (toast != null) {
@@ -689,9 +712,8 @@ private void showToast(@Nullable final String text) {
689712
* Configures a builder, which allows to create header dialogs, depending on the app's
690713
* settings.
691714
*
692-
* @param builder
693-
* The builder, which should be configured, as an instance of the class {@link
694-
* AbstractHeaderDialogBuilder}
715+
* @param builder The builder, which should be configured, as an instance of the class {@link
716+
* AbstractHeaderDialogBuilder}
695717
*/
696718
private void configureHeaderDialogBuilder(@NonNull final AbstractHeaderDialogBuilder builder) {
697719
builder.setFullscreen(shouldUseFullscreen());
@@ -721,9 +743,8 @@ private void configureHeaderDialogBuilder(@NonNull final AbstractHeaderDialogBui
721743
* Configures a builder, which allows to create button bar dialogs, depending on the app's
722744
* settings.
723745
*
724-
* @param builder
725-
* The builder, which should be configured as an instance of the class {@link
726-
* AbstractButtonBarDialogBuilder}
746+
* @param builder The builder, which should be configured as an instance of the class {@link
747+
* AbstractButtonBarDialogBuilder}
727748
*/
728749
private void configureButtonBarDialogBuilder(
729750
@NonNull final AbstractButtonBarDialogBuilder builder) {
@@ -841,6 +862,20 @@ private boolean shouldIconBeShown() {
841862
return sharedPreferences.getBoolean(key, defaultValue);
842863
}
843864

865+
/**
866+
* Returns, whether the list items of the example dialogs should have an icon, or not.
867+
*
868+
* @return True, if the list items should have an icon, false otherwise
869+
*/
870+
private boolean shouldListItemIconsBeShown() {
871+
SharedPreferences sharedPreferences =
872+
PreferenceManager.getDefaultSharedPreferences(getActivity());
873+
String key = getString(R.string.show_list_item_icon_preference_key);
874+
boolean defaultValue =
875+
getResources().getBoolean(R.bool.show_list_item_icon_preference_default_value);
876+
return sharedPreferences.getBoolean(key, defaultValue);
877+
}
878+
844879
/**
845880
* Returns, whether the message of the example dialog should be shown, or not.
846881
*
@@ -1022,8 +1057,7 @@ private boolean shouldUseFullscreen() {
10221057
/**
10231058
* Shows an alert dialog using a circle reveal animation.
10241059
*
1025-
* @param view
1026-
* The view, which is used to show the dialog, as an instance of the class {@link View}
1060+
* @param view The view, which is used to show the dialog, as an instance of the class {@link View}
10271061
*/
10281062
public final void showAlertDialog(@NonNull final View view) {
10291063
initializeAlertDialog();

example/src/main/res/values/bools.xml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ License.
1818
<bool name="show_dialog_title_preference_default_value">true</bool>
1919
<bool name="show_dialog_header_preference_default_value">false</bool>
2020
<bool name="show_dialog_icon_preference_default_value">false</bool>
21+
<bool name="show_list_item_icon_preference_default_value">false</bool>
2122
<bool name="show_dialog_message_preference_default_value">true</bool>
2223
<bool name="show_button_bar_divider_preference_default_value">false</bool>
2324
<bool name="show_content_divider_preference_default_value">false</bool>

example/src/main/res/values/strings.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ License.
3535
<string name="edit_text_dialog_text">Text</string>
3636
<string name="show_wizard_dialog_preference_key">show_wizard_dialog_preference_key</string>
3737
<string name="show_wizard_dialog_preference_title">Show wizard dialog</string>
38-
<string name="dialog_title_preference_category_title">Title and icon</string>
38+
<string name="dialog_title_preference_category_title">Title and icons</string>
3939
<string name="show_dialog_title_preference_key">show_dialog_title_preference_key</string>
4040
<string name="show_dialog_title_preference_title">Show dialog title</string>
4141
<string name="show_dialog_title_preference_summary">Allows to set, whether the example dialog should contain a title, or not</string>
@@ -58,6 +58,9 @@ License.
5858
<string name="show_dialog_icon_preference_key">show_dialog_icon_preference_key</string>
5959
<string name="show_dialog_icon_preference_title">Show dialog icon</string>
6060
<string name="show_dialog_icon_preference_summary">Allows to set, whether the example dialog should contain an icon, or not</string>
61+
<string name="show_list_item_icon_preference_key">show_list_item_icon_preference_key</string>
62+
<string name="show_list_item_icon_preference_title">Show list item icons</string>
63+
<string name="show_list_item_icon_preference_summary">Allows to set, whether list items should have an icon, or not</string>
6164
<string name="dialog_content_preference_category_title">Content</string>
6265
<string name="dialog_buttons_preference_category_title">Buttons</string>
6366
<string name="positive_button_text_preference_key">positive_button_text_preference_key</string>

example/src/main/res/xml/preferences.xml

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ License.
6767
android:key="@string/show_dialog_icon_preference_key"
6868
android:summary="@string/show_dialog_icon_preference_summary"
6969
android:title="@string/show_dialog_icon_preference_title"/>
70+
<CheckBoxPreference
71+
android:defaultValue="@bool/show_list_item_icon_preference_default_value"
72+
android:key="@string/show_list_item_icon_preference_key"
73+
android:summary="@string/show_list_item_icon_preference_summary"
74+
android:title="@string/show_list_item_icon_preference_title"/>
7075
</PreferenceCategory>
7176

7277
<PreferenceCategory android:title="@string/dialog_message_preference_category_title">

0 commit comments

Comments
 (0)