1

I am trying to show an AlertDialog in android. The problem is that the title of the dialog appears 2 times. I want it to show only one title? How can I do it?

That is how the dialog looks like Dialog showing 2 titles

And that is how i show the dialog

   AlertDialog alertDialog = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog).create();
                    alertDialog.setTitle(R.string.ttl_alrt_dlg_dont_asked_again);
                    alertDialog.setMessage("AI bifat nu ma mai intreba asa ca mergi in setari");
                    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();


                                    actv.finish();
                                    //ActivityCompat.requestPermissions(actv,
                                      //      new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                        //    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                                }
                            });
                    alertDialog.show();
Liviu
  • 83
  • 1
  • 9

5 Answers5

2

Try this:

AlertDialog alertDialog = new AlertDialog.Builder(this)
            .setTitle("title")
            .setMessage("AI bifat nu ma mai intreba asa ca mergi in setari")
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialog.dismiss();
                    actv.finish();
                }
            })
            .create();
    alertDialog.show();
Sayem
  • 4,891
  • 3
  • 28
  • 43
  • your code should work fine. but as it shows duplicate two thing might mess up. one is your theme. another is you are setting title after creating the dialog. so, you can try this. – Sayem Jan 09 '18 at 08:32
1

Its look like its due to android.R.style.Theme_Material_Dialog, First title is ActionBar .

Solutions

1. Just use it without style . It will show in material design appearance anyway .

2. Or you can use android.R.style.Theme_Material_Dialog_NoActionBar

  AlertDialog alertDialog = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_NoActionBar).create();
  alertDialog.setTitle(R.string.ttl_alrt_dlg_dont_asked_again);
  alertDialog.setMessage("AI bifat nu ma mai intreba asa ca mergi in setari");
  alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
    alertDialog.show();
Emil
  • 2,786
  • 20
  • 24
ADM
  • 20,406
  • 11
  • 52
  • 83
  • @Sayem answer is also a good one. But i checked this one as the right answer because it conserves the material appearance. – Liviu Jan 09 '18 at 16:30
0

Remove this line-:

alertDialog.setTitle(R.string.ttl_alrt_dlg_dont_asked_again);
Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
0

It's Theme_Material_Dialog issue. so you have to customize your own dialog by creating style.

see this helpful answer here

And this complete answer another_here

FarshidABZ
  • 3,860
  • 4
  • 32
  • 63
0

Faced a similar issue while using AppCompatDialogFragment. Ended up using as below to avoid, title showing up twice:

<style name="dialogfrag_title" parent="Theme.MaterialComponents.Light.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:padding">@dimen/lyt_margin</item>
    <item name="android:windowBackground">@color/appBackground</item>
</style>

along with below line in the fragment class.

        getDialog().setTitle("About "+ user.name());