aboutsummaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorPo Lu2023-06-12 20:09:09 +0800
committerPo Lu2023-06-12 20:09:09 +0800
commit1cd514c297fb570b35b257ee9880b729e35a1068 (patch)
tree744f3d6fca8a5ed1d66af7d868c39297b3b53fd6 /java
parent3b08bb1318cd0bf6bc1811b520f9c6934b1aa3bd (diff)
downloademacs-1cd514c297fb570b35b257ee9880b729e35a1068.tar.gz
emacs-1cd514c297fb570b35b257ee9880b729e35a1068.zip
Improve appearance of custom dialog buttons on Android
* java/org/gnu/emacs/EmacsDialog.java (toAlertDialog): Resolve dialog button style and use it instead.
Diffstat (limited to 'java')
-rw-r--r--java/org/gnu/emacs/EmacsDialog.java72
1 files changed, 60 insertions, 12 deletions
diff --git a/java/org/gnu/emacs/EmacsDialog.java b/java/org/gnu/emacs/EmacsDialog.java
index 3f8fe0109c0..5f48a9a5f9f 100644
--- a/java/org/gnu/emacs/EmacsDialog.java
+++ b/java/org/gnu/emacs/EmacsDialog.java
@@ -27,6 +27,10 @@ import android.app.AlertDialog;
27import android.content.Context; 27import android.content.Context;
28import android.content.DialogInterface; 28import android.content.DialogInterface;
29 29
30import android.content.res.Resources.NotFoundException;
31import android.content.res.Resources.Theme;
32import android.content.res.TypedArray;
33
30import android.os.Build; 34import android.os.Build;
31 35
32import android.provider.Settings; 36import android.provider.Settings;
@@ -148,13 +152,17 @@ public final class EmacsDialog implements DialogInterface.OnDismissListener
148 toAlertDialog (Context context) 152 toAlertDialog (Context context)
149 { 153 {
150 AlertDialog dialog; 154 AlertDialog dialog;
151 int size; 155 int size, styleId;
156 int[] attrs;
152 EmacsButton button; 157 EmacsButton button;
153 EmacsDialogButtonLayout layout; 158 EmacsDialogButtonLayout layout;
154 Button buttonView; 159 Button buttonView;
155 ViewGroup.LayoutParams layoutParams; 160 ViewGroup.LayoutParams layoutParams;
161 Theme theme;
162 TypedArray attributes;
156 163
157 size = buttons.size (); 164 size = buttons.size ();
165 styleId = -1;
158 166
159 if (size <= 3) 167 if (size <= 3)
160 { 168 {
@@ -193,19 +201,10 @@ public final class EmacsDialog implements DialogInterface.OnDismissListener
193 else 201 else
194 { 202 {
195 /* There are more than 3 buttons. Add them all to a special 203 /* There are more than 3 buttons. Add them all to a special
196 container widget that handles wrapping. */ 204 container widget that handles wrapping. First, create the
205 layout. */
197 206
198 layout = new EmacsDialogButtonLayout (context); 207 layout = new EmacsDialogButtonLayout (context);
199
200 for (EmacsButton emacsButton : buttons)
201 {
202 buttonView = new Button (context);
203 buttonView.setText (emacsButton.name);
204 buttonView.setOnClickListener (emacsButton);
205 buttonView.setEnabled (emacsButton.enabled);
206 layout.addView (buttonView);
207 }
208
209 layoutParams 208 layoutParams
210 = new FrameLayout.LayoutParams (ViewGroup.LayoutParams.MATCH_PARENT, 209 = new FrameLayout.LayoutParams (ViewGroup.LayoutParams.MATCH_PARENT,
211 ViewGroup.LayoutParams.WRAP_CONTENT); 210 ViewGroup.LayoutParams.WRAP_CONTENT);
@@ -223,6 +222,55 @@ public final class EmacsDialog implements DialogInterface.OnDismissListener
223 222
224 if (title != null) 223 if (title != null)
225 dialog.setTitle (title); 224 dialog.setTitle (title);
225
226 /* Now that the dialog has been created, set the style of each
227 custom button to match the usual dialog buttons found on
228 Android 5 and later. */
229
230 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
231 {
232 /* Obtain the Theme associated with the dialog. */
233 theme = dialog.getContext ().getTheme ();
234
235 /* Resolve the dialog button style. */
236 attrs
237 = new int [] { android.R.attr.buttonBarNeutralButtonStyle, };
238
239 try
240 {
241 attributes = theme.obtainStyledAttributes (attrs);
242
243 /* Look for the style ID. Default to -1 if it could
244 not be found. */
245 styleId = attributes.getResourceId (0, -1);
246
247 /* Now clean up the TypedAttributes object. */
248 attributes.recycle ();
249 }
250 catch (NotFoundException e)
251 {
252 /* Nothing to do here. */
253 }
254 }
255
256 /* Create each button and add it to the layout. Set the style
257 if necessary. */
258
259 for (EmacsButton emacsButton : buttons)
260 {
261 if (styleId == -1)
262 /* No specific style... */
263 buttonView = new Button (context);
264 else
265 /* Use the given styleId. */
266 buttonView = new Button (context, null, 0, styleId);
267
268 /* Set the text and on click handler. */
269 buttonView.setText (emacsButton.name);
270 buttonView.setOnClickListener (emacsButton);
271 buttonView.setEnabled (emacsButton.enabled);
272 layout.addView (buttonView);
273 }
226 } 274 }
227 275
228 return dialog; 276 return dialog;