23

I am following this code to create custom dialog but i am not getting how to remove dialog title bar ?

  AlertDialog alertDialog;

   @Override
   protected Dialog onCreateDialog(int id) {

      AlertDialog dialogDetails = null;

      switch (id) {
      case DIALOG_LOGIN:

       LayoutInflater inflater = LayoutInflater.from(this);
       View dialogview = inflater.inflate(R.layout.dialog_layout, null);

           AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
           dialogbuilder.setTitle("Login");
           dialogbuilder.setView(dialogview);
           dialogDetails = dialogbuilder.create();

           break;
          }

      return dialogDetails;
     }

     @Override
     protected void onPrepareDialog(int id, Dialog dialog) {

      switch (id) {
      case DIALOG_LOGIN:
      alertDialog = (AlertDialog) dialog;

      .......

}

I know to remove title area of the Alert Dialog, we have to use requestWindowFeature(Window.FEATURE_NO_TITLE);

But don't know where i have to place above line ?

Sun
  • 6,768
  • 25
  • 76
  • 131

11 Answers11

29

If you don't want title bar in alert dialog then just remove below line from code.

dialogbuilder.setTitle("Login");

If still not working then add below line.

dialogbuilder.requestWindowFeature(Window.FEATURE_NO_TITLE);
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • what if i want my text should look same in both (phone and tablet) ? – Sun Feb 23 '15 at 06:59
  • You can also create custom dialog. – Niranj Patel Feb 23 '15 at 07:01
  • hey bro this time i am not talking about custom alert dialog, i am concern about how to show text always of same size in both devices phones and tablets – Sun Feb 23 '15 at 07:03
  • we can't customize default alert dialog. so you have to create custom dialog for that. – Niranj Patel Feb 23 '15 at 07:07
  • bro pls try to understand my point, i am not talking about dialog here, my question is like in my activity i have text view with text as "Hello World" and i want it should look same sizes on all devices (phones/tabs) – Sun Feb 23 '15 at 07:10
  • @Sun hmm, use SP dimension & stop chat here – Niranj Patel Feb 23 '15 at 07:11
  • Not sure why this was selected as the correct answer. When using this answer, the title text is indeed gone, but the very noticeable big white TITLE BAR still remains. The answer the others have posted (... FEATURE_NO_TITLE ... ) works to completely remove the title bar (and title text). – Kalnode Mar 15 '17 at 13:34
  • If use custom layout, must add `dialogbuilder.requestWindowFeature(Window.FEATURE_NO_TITLE);` before `setContentView` instead. – Latief Anwar Dec 06 '17 at 21:04
19

Use dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); before dialog.setContentView(R.layout.logindialog); by doing so you will be able to hide the Dialog's title.

SMR
  • 6,628
  • 2
  • 35
  • 56
4

Use dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
2

First remove this line:

dialogbuilder.setTitle("Login");

Then add this one:

dialogbuilder.requestWindowFeature(Window.FEATURE_NO_TITLE);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
serabile
  • 329
  • 3
  • 11
1

just remove

dialogbuilder.setTitle("Login");
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
Ravi
  • 34,851
  • 21
  • 122
  • 183
1

Try this::

AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
        mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
        (ViewGroup) findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

Refer here

Community
  • 1
  • 1
vijay
  • 1,475
  • 2
  • 16
  • 26
1

I could not find .requestWindowFeature with AlertDialog Builder.

If you do not want to have a title when constructing an Alert Dialog with a Builder, use new AlertDialog.Builder(getContext(), android.R.style.Theme_Material_Light_Dialog_NoActionBar_MinWidth);

Alan
  • 9,331
  • 14
  • 52
  • 97
0

Eventually on a Motorola Droid Razr M (AOS 4.4) smartphone these approaches does not work and I believe there are other smatphones like this exist on a market. The only effect gained from:

        setTitle(null);
        setCustomTitle(null);

is that title has no text but its view still persists in the dialog view (looks like an empty view at a top). So the only way I found is based on this answer:

        int titleId = getContext().getResources().getIdentifier( "alertTitle", "id", "android" );
        if (titleId > 0) {
            View dialogTitle = findViewById(titleId);
            if (dialogTitle != null) {
                ((View)dialogTitle.getParent().getParent()).setVisibility(View.GONE);
            }
        }
Stan
  • 6,511
  • 8
  • 55
  • 87
0

Use Dialog with a custom layout it will definitely work

final Dialog dialog = new Dialog(this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.custom_layout);
            Button btOk = dialog.findViewById(R.id.btOk);
            Button btCancel = dialog.findViewById(R.id.btCancel);
            btOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //To Do
                    dialog.dismiss();
                }
            });
            btCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            dialog.setCancelable(false);
            dialog.show();

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Ok" />
    <Button
        android:id="@+id/btCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />
</LinearLayout>
SUBHA DAS
  • 11
  • 1
0

Passing a theme to your dialog can remove the title bar for you.

<style name="NoTitleDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
</style>

Pass the theme to your dialog:

Dialog dialog = new Dialog(this, R.style.NoTitleDialog);
jenos kon
  • 504
  • 4
  • 7
-6

Use this

AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
           dialogbuilder .requestWindowFeature(Window.FEATURE_NO_TITLE);
           dialogbuilder.setView(dialogview);
           dialogDetails = dialogbuilder.create();
Fahim
  • 12,198
  • 5
  • 39
  • 57
  • 1
    "dialogbuilder.requestWindowFeature" isn't supported. – android developer May 09 '15 at 14:49
  • Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); – Ashish John Sep 27 '16 at 04:27
  • @AshishJohn: Read the full question and title before commenting anythng rubbish! Alert Dialogue and Dialogue are two different classes – Bhavik Mehta Jun 12 '17 at 16:04
  • @BhavikMehta Given question is "....but i am not getting how to remove dialog title bar"; & I have proposed solution to remove dialog title bar... I think you read only the code where its written "Alert Dialogue" & not the question – Ashish John Jun 13 '17 at 07:30