Applying a theme to the AlertDialog Builder causes the title to work incorrectly

I am trying to add a title to my AlertDialog Builder. When I add a theme, my title moves to the selection area.
Here's the first example:

    classificationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog) );
                    //building my selection options
                    builder.setItems(classificationList, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            String desiredClassification = classificationList[which];
                            if ( !getClassification().equals(desiredClassification) ) {
                                CallsignContract.updateClassification(desiredClassification, mContext);
                                setClassification(desiredClassification);
                                classificationButton.setText(desiredClassification);
                            }
                        }
                    });
                    builder.setTitle(R.string.classification_alert_header)
                            .create().show();
        }
    });

      

This is the result.
no title
In my second try, I am creating an alertdialog from the builder and giving that title. The result is the correct title, but the title reappears in the selection area.

        classificationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog) );
                        //building my selection options
                        builder.setItems(classificationList, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            String desiredClassification = classificationList[which];
                            if ( !getClassification().equals(desiredClassification) ) {
                                CallsignContract.updateClassification(desiredClassification, mContext);
                                setClassification(desiredClassification);
                                classificationButton.setText(desiredClassification);
                            }
                        }
                    });
            AlertDialog alertDialog = builder.create();
            alertDialog.setTitle(R.string.classification_alert_header);
            alertDialog.show();
        }
    });

      


double titles

Thank!

+3


source to share


1 answer


To show only one title, you must call alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE)

.



AlertDialog.Builder builder = new AlertDialog.Builder(
    new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog)
);

//building my selection options
builder.setItems(classificationList,
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String desiredClassification = classificationList[which];

            if (!getClassification().equals(desiredClassification)) {
                CallsignContract.updateClassification(desiredClassification, mContext);
                setClassification(desiredClassification);
                classificationButton.setText(desiredClassification);
            }
        }
    }
);

AlertDialog alertDialog = builder.create();
alertDialog.setTitle(R.string.classification_alert_header);
// Requesting dialog to remove the title
alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();

      

+1


source







All Articles