quarta-feira, 21 de janeiro de 2015

How to center AlertDialog message

You follow some tutorials and stackoverflow answer to code something like this
         final AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setMessage(getString(R.string.email_invalido))
                    .setNeutralButton(R.string.ok, null)
                    .create();
            TextView messageView = (TextView) alertDialog.findViewById(android.R.id.message);
            if (messageView != null) {
                messageView.setPadding(15, 20, 10, 10);
                messageView.setGravity(Gravity.CENTER_VERTICAL);
            }                                     alertDialog.show(); 

But that produces the following error:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content
            at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:249)
            at com.android.internal.app.AlertController.installContent(AlertController.java:234)
            at android.app.AlertDialog.onCreate(AlertDialog.java:337)
            at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
            at android.app.Dialog.show(Dialog.java:262)
            at xxx.activity.validateMailAndMayRequestRecoverPassword(EsqueciSenhaActivity.java:34)
            at xxx. activity onClickEnviarButton(EsqueciSenhaActivity.java:62)             at xxx.activity$$ViewInjector$1.onClick(EsqueciSenhaActivity$$ViewInjector.java:18)
            at android.view.View.performClick(View.java:4456)
            at android.view.View$PerformClick.run(View.java:18465)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Analyzing the PhoneWindow.java source code I've found out that you need to call show before using findViewById() because of some side effects.

So the correct code is

            final AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setMessage(getString(R.string.email_invalido))
                    .setNeutralButton(R.string.ok, null)
                    .create(); alertDialog.show();             TextView messageView = (TextView) alertDialog.findViewById(android.R.id.message);
            if (messageView != null) {
                messageView.setPadding(15, 20, 10, 10);
                messageView.setGravity(Gravity.CENTER_VERTICAL);
            } 

Nenhum comentário:

Postar um comentário