|
| 1 | +package it.feio.android.omninotes; |
| 2 | + |
| 3 | +import android.app.AlertDialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.DialogInterface; |
| 6 | +import android.preference.ListPreference; |
| 7 | +import android.support.annotation.NonNull; |
| 8 | +import android.util.AttributeSet; |
| 9 | +import android.view.View; |
| 10 | +import android.view.ViewGroup; |
| 11 | +import android.widget.ArrayAdapter; |
| 12 | +import android.widget.CheckedTextView; |
| 13 | +import android.widget.TextView; |
| 14 | + |
| 15 | +import it.feio.android.checklistview.utils.DensityUtil; |
| 16 | +import it.feio.android.omninotes.utils.Fonts; |
| 17 | + |
| 18 | +public class FontSizeListPreference extends ListPreference { |
| 19 | + |
| 20 | + private int clickedDialogEntryIndex; |
| 21 | + |
| 22 | + |
| 23 | + public FontSizeListPreference(Context context, AttributeSet attrs) { |
| 24 | + super(context, attrs); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void onBindView(View view) { |
| 29 | + super.onBindView(view); |
| 30 | + TextView summary = (TextView) view.findViewById(android.R.id.summary); |
| 31 | + Fonts.overrideTextSize(getContext(),getSharedPreferences(),summary); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { |
| 36 | + |
| 37 | + ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), |
| 38 | + R.layout.settings_font_size_dialog_item, getEntries()) { |
| 39 | + @NonNull |
| 40 | + @Override |
| 41 | + public View getView(int position, View convertView, @NonNull ViewGroup parent) { |
| 42 | + CheckedTextView view = (CheckedTextView) convertView; |
| 43 | + if (view == null) { |
| 44 | + view = (CheckedTextView) View.inflate(getContext(), |
| 45 | + R.layout.settings_font_size_dialog_item, null); |
| 46 | + } |
| 47 | + view.setText(getEntries()[position]); |
| 48 | + Context privateContext = getContext().getApplicationContext(); |
| 49 | + float currentSize = DensityUtil.pxToDp(((TextView) View.inflate(getContext(), |
| 50 | + R.layout.settings_font_size_dialog_item, null)).getTextSize(), privateContext); |
| 51 | + float offset = privateContext.getResources().getIntArray( |
| 52 | + R.array.text_size_offset)[position]; |
| 53 | + view.setTextSize(currentSize + offset); |
| 54 | + return view; |
| 55 | + } |
| 56 | + }; |
| 57 | + clickedDialogEntryIndex = findIndexOfValue(getValue()); |
| 58 | + builder.setSingleChoiceItems(adapter, clickedDialogEntryIndex, |
| 59 | + (dialog, which) -> { |
| 60 | + clickedDialogEntryIndex = which; |
| 61 | + FontSizeListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE); |
| 62 | + dialog.dismiss(); |
| 63 | + }); |
| 64 | + builder.setPositiveButton(null, null); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void onDialogClosed(boolean positiveResult) { |
| 69 | + super.onDialogClosed(positiveResult); |
| 70 | + |
| 71 | + if (positiveResult && clickedDialogEntryIndex >= 0 && getEntryValues() != null) { |
| 72 | + String val = getEntryValues()[clickedDialogEntryIndex].toString(); |
| 73 | + if (callChangeListener(val)) { |
| 74 | + setValue(val); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments