0

I need to change the default blue colour of the separator line below the title of the AlertDialog just as asked in this Question

On Searching I found some work arounds to do so but on trying this code its giving exception.

Heres my code:

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Hello World")
            .setTitle("Alert Dialog")
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setCancelable(false)
            .setPositiveButton("Close",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }
            );
    AlertDialog dialog = builder.create();
    try {
        ((ViewGroup) ((ViewGroup) dialog.getWindow().getDecorView()).getChildAt(0))
                .getChildAt(1)
                .setBackgroundColor(getResources().getColor(R.color.app_orange));
    }catch (NullPointerException ex){
        Timber.e(ex.getStackTrace().toString());
    }
    dialog.show();

And here is the LogCat:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.example/com.android.example.activities.HomeScreenActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
        at android.app.ActivityThread.access$900(ActivityThread.java:161)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        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:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:318)
        at com.android.internal.app.AlertController.installContent(AlertController.java:235)
        at android.app.AlertDialog.onCreate(AlertDialog.java:337)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:363)
        at android.app.Dialog.show(Dialog.java:264)
        at com.android.example.fragments.HomeScreenFragment.onActivityCreated(HomeScreenFragment.java:114)
        at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1189)
        at android.app.Activity.performStart(Activity.java:5436)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            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:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
AabidMulani
  • 2,325
  • 1
  • 28
  • 47
  • Have you tried searching for the exception you get? – Sufian Jun 17 '14 at 14:32
  • yup! The problem is where to place the code try{}catch(). And i did not find any post for the complete code – AabidMulani Jun 17 '14 at 14:36
  • possible duplicate of [requestFeature() must be called before adding content](http://stackoverflow.com/questions/4250149/requestfeature-must-be-called-before-adding-content) please read the first message of your exception to the end, also you generally **don't** catch runtime exceptions, you deal with them. – Ceiling Gecko Jun 17 '14 at 14:46
  • @CeilingGecko: I am aware of the statement **requestFeature() must be called before adding content ** but there is no setContentView in my code i am using a AlertDialog().Builder and dont know where to post my try_catch code. If you have any link which handle's this issue in case of a dialog then this question can be marked as duplicate. – AabidMulani Jun 17 '14 at 14:55
  • @username_AB check this answer to know why your app crashes http://stackoverflow.com/a/15572855/1276636 – Sufian Jun 17 '14 at 15:01

1 Answers1

0

Calling getDecorView() creates the decor view if it doesn't exist.

Calling show() on a dialog calls requestFeature() and it will fail with this exception if the decor view has already been created. Most often the decor view is created by the first call to setContentView(). Hence the wording in the exception.

The code seems hacky, but you can try moving the try-catch involving getDecorView() after the dialog show().

laalto
  • 150,114
  • 66
  • 286
  • 303