|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Ratul Hasan |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +package io.github.ratul.topactivity.dialog; |
| 18 | + |
| 19 | +import android.widget.TextView; |
| 20 | +import android.app.AlertDialog; |
| 21 | +import android.content.Context; |
| 22 | +import android.graphics.Typeface; |
| 23 | + |
| 24 | +/** |
| 25 | + * Created by Ratul on 09/05/2022. |
| 26 | + */ |
| 27 | +public class DialogBeautifier { |
| 28 | + private String regular_font = "fonts/google_sans_regular.ttf"; |
| 29 | + private String bold_font = "fonts/google_sans_bold.ttf"; |
| 30 | + private AlertDialog dialog; |
| 31 | + private Context dialogContext; |
| 32 | + |
| 33 | + public DialogBeautifier(AlertDialog alert) { |
| 34 | + dialog = alert; |
| 35 | + dialogContext = alert.getContext(); |
| 36 | + } |
| 37 | + |
| 38 | + public boolean beautifyTitle() { |
| 39 | + try{ |
| 40 | + int titleId = dialogContext.getResources().getIdentifier( "alertTitle", "id", "android" ); |
| 41 | + TextView title = dialog.findViewById(titleId); |
| 42 | + Typeface typeface = Typeface.createFromAsset(dialogContext.getAssets(), bold_font); |
| 43 | + title.setTypeface(typeface); |
| 44 | + return true; |
| 45 | + } catch(Exception e) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void beautifyMessage() { |
| 51 | + TextView message = dialog.findViewById(android.R.id.message); |
| 52 | + message.setTypeface(Typeface.createFromAsset(dialogContext.getAssets(), regular_font), 0); |
| 53 | + } |
| 54 | + |
| 55 | + public void beautifyPositiveButton() { |
| 56 | + TextView positiveBtn = dialog.findViewById(android.R.id.button1); |
| 57 | + positiveBtn.setTypeface(Typeface.createFromAsset(dialogContext.getAssets(), regular_font), 1); |
| 58 | + positiveBtn.setAllCaps(false); |
| 59 | + } |
| 60 | + |
| 61 | + public void beautifyNegativeButton() { |
| 62 | + TextView negativeBtn = dialog.findViewById(android.R.id.button2); |
| 63 | + negativeBtn.setTypeface(Typeface.createFromAsset(dialogContext.getAssets(), regular_font), 1); |
| 64 | + negativeBtn.setAllCaps(false); |
| 65 | + } |
| 66 | + |
| 67 | + public void beautifyNeutralButton() { |
| 68 | + TextView neutralBtn = dialog.findViewById(android.R.id.button3); |
| 69 | + neutralBtn.setTypeface(Typeface.createFromAsset(dialogContext.getAssets(), regular_font), 1); |
| 70 | + neutralBtn.setAllCaps(false); |
| 71 | + } |
| 72 | + |
| 73 | + public void beautify() throws Exception { |
| 74 | + beautifyTitle(); |
| 75 | + beautifyMessage(); |
| 76 | + beautifyPositiveButton(); |
| 77 | + beautifyNegativeButton(); |
| 78 | + beautifyNeutralButton(); |
| 79 | + } |
| 80 | +} |
0 commit comments