From 79ecffc91c8856e7ceb6a65441d19a6272195426 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Mon, 20 Feb 2017 08:42:25 +1100 Subject: [PATCH] Use AppCompatListPreference from QuickLyric to fix dialog themes. Fixes Issue #750. This new class makes sure to use the correct `AlertDialog.Builder` from the support lib. This in turn ensures the correct styles get applied to the result alert dialog. --- .../view/AppCompatListPreference.java | 97 +++++++++++++++++++ app/src/main/res/xml/preferences.xml | 8 +- 2 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/geecko/QuickLyric/view/AppCompatListPreference.java diff --git a/app/src/main/java/com/geecko/QuickLyric/view/AppCompatListPreference.java b/app/src/main/java/com/geecko/QuickLyric/view/AppCompatListPreference.java new file mode 100644 index 000000000..7e3b63a11 --- /dev/null +++ b/app/src/main/java/com/geecko/QuickLyric/view/AppCompatListPreference.java @@ -0,0 +1,97 @@ +/* + * * + * * This file is part of QuickLyric + * * Created by geecko + * * + * * QuickLyric is free software: you can redistribute it and/or modify + * * it under the terms of the GNU General Public License as published by + * * the Free Software Foundation, either version 3 of the License, or + * * (at your option) any later version. + * * + * * QuickLyric is distributed in the hope that it will be useful, + * * but WITHOUT ANY WARRANTY; without even the implied warranty of + * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * * GNU General Public License for more details. + * * You should have received a copy of the GNU General Public License + * * along with QuickLyric. If not, see . + * + */ + +package com.geecko.QuickLyric.view; + +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.PreferenceManager; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatDialog; +import android.util.AttributeSet; + +import java.lang.reflect.Method; + +public class AppCompatListPreference extends ListPreference { + + private AppCompatDialog mDialog; + + public AppCompatListPreference(Context context) { + super(context); + } + + public AppCompatListPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + public AppCompatDialog getDialog() { + return mDialog; + } + + @Override + protected void showDialog(Bundle state) { + if (getEntries() == null || getEntryValues() == null) { + throw new IllegalStateException( + "ListPreference requires an entries array and an entryValues array."); + } + + int preselect = findIndexOfValue(getValue()); + AlertDialog.Builder builder = new AlertDialog.Builder(getContext()) + .setTitle(getDialogTitle()) + .setIcon(getDialogIcon()) + .setSingleChoiceItems(getEntries(), preselect, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (which >= 0 && getEntryValues() != null) { + String value = getEntryValues()[which].toString(); + if (callChangeListener(value) && isPersistent()) + setValue(value); + } + dialog.dismiss(); + } + }); + + PreferenceManager pm = getPreferenceManager(); + try { + Method method = pm.getClass().getDeclaredMethod( + "registerOnActivityDestroyListener", + PreferenceManager.OnActivityDestroyListener.class); + method.setAccessible(true); + method.invoke(pm, this); + } catch (Exception e) { + e.printStackTrace(); + } + + mDialog = builder.create(); + if (state != null) + mDialog.onRestoreInstanceState(state); + mDialog.show(); + } + + @Override + public void onActivityDestroy() { + super.onActivityDestroy(); + if (mDialog != null && mDialog.isShowing() && + mDialog.getWindow() != null && mDialog.getWindow().getWindowManager() != null) + mDialog.dismiss(); + } +} \ No newline at end of file diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 96ae03aaa..da67b38da 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -1,7 +1,7 @@ - - - -