2

I have been trying for a while to remove the title of a DialogFragment, but several attempts have failed. I tried with these:

<style name="dialog" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:windowNoTitle">true</item>
</style>

and this:

    int style, theme;

    style = DialogFragment.STYLE_NO_TITLE;
    theme = R.style.dialog;//Tried using Theme.Holo.Dialog here too


    setStyle(style, theme);//Setting theme to 0 renders it invisible. Content only

And this(has no effect):

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
     Dialog dialog = super.onCreateDialog(savedInstanceState);

     // request a window without the title
     dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
     return dialog;
}

None have an effect in removing the title.

Any other ideas?

EDIT:

Important code in the DIalogFragment:

public class MenuDialog extends DialogFragment implements View.OnClickListener {
    Context c;
    Clicker cl;
    Game game;

    public static MenuDialog newInstance() {
        MenuDialog f = new MenuDialog();

        // Supply num input as an argument.
        Bundle args = new Bundle();

        f.setArguments(args);

        return f;
    }

    public void params(Context c, Clicker cl, Game game){
        this.c = c;
        this.cl = cl;
        this.game = game;
    }




    @Override
    public void onCreate(Bundle sis){
        super.onCreate(sis);

        int style, theme;

        style = DialogFragment.STYLE_NO_TITLE;
        theme = R.style.dialog;//This theme is as defined above


        setStyle(style, theme);


    }

    private View v;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.menu, container, false);
        this.v = v;
        setButtons();
        return v;
    }

}

Displaying the dialog:

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment prev = getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    MenuDialog newFragment = MenuDialog.newInstance();
    newFragment.params(getBaseContext(), clicker, this);
    newFragment.show(ft, "dialog");
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • I had this same issue. Solved by `getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);` in `onViewCreated()` – mrtpk Sep 20 '16 at 19:20
  • Added in onViewCreated : No effect and no crash – Zoe Sep 20 '16 at 19:26
  • I used onCreateDialog as to a different SO question. I am adding some more code that is the fragment. I create the content of the fragment in onCreateView – Zoe Sep 20 '16 at 19:32
  • Can you add your code that shows this DialogFragment? Are you using a FragmentTransaction or showing it as a Dialog? – Daniel Nugent Sep 20 '16 at 19:44
  • Added the code showing how I create it. – Zoe Sep 20 '16 at 19:48
  • Isn't that the equivalent of what I did(.show(FragmentTransaction, String)? – Zoe Sep 20 '16 at 19:52
  • It should be, yes. The `onCreateDialog()` code you tried there really should work, take a look at this Google Sample: https://developer.android.com/guide/topics/ui/dialogs.html#FullscreenDialog – Daniel Nugent Sep 20 '16 at 19:55
  • Copied and pasted and still no effect. – Zoe Sep 20 '16 at 20:01
  • @Polarbear0106 Hey tested the code you posted with `myDialog.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);`. Sorry, but I'm getting result as expected. No window and no crash. – mrtpk Sep 21 '16 at 08:04
  • just in case, I have used `android.support.v4.app.DialogFragment` and `getSupportFragmentManager()` to add `MenuDialog` – mrtpk Sep 21 '16 at 08:06
  • Could it have something to do with me running on Android 6.0.1? I'm going to try using android.support.v4.app.DialogFragment now – Zoe Sep 21 '16 at 13:46

1 Answers1

5

Create your dialog normally, and add a line like this before showing:

myDialog.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

Edit: added the .getDialog() call

Edit 2: Tested with the link of your example

I have tested this code now and it works:

public class HelpDialog extends DialogFragment {

    public HelpDialog() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //Inflate the XML view for the help dialog fragment
        View view = inflater.inflate(R.my_layout, container);

        HelpDialog.this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);


        return view;
    }

}

and i called the Dialog like that :

HelpDialog dialog = new HelpDialog(); dialog.show(getFragmentManager(),"test");

  • "Cannot resolve symbol". Are you thinking of Dialog or DialogFragment? It is possible with Dialog but not DialogFragment – Zoe Sep 20 '16 at 19:28
  • java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.Dialog.requestWindowFeature(int)' on a null object reference - The dialog is defined before calling – Zoe Sep 20 '16 at 19:36
  • Well that's strange, as far i know the title is part of the Dialog, not the Window, that's why you have no effect when call in getWindow(), you are calling this method inside the Dialog class ? – Lucas Queiroz Ribeiro Sep 20 '16 at 19:41
  • I'm not sure. I have tried both getWindow and getDialog but no effect. The answer I based that part on is this: http://stackoverflow.com/a/15279400/6296561 but no effect when used just like that or crash when used like you suggested – Zoe Sep 20 '16 at 19:43
  • Try this, Dialog your_dialog= new Dialog(this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); – Ajith Pandian Sep 20 '16 at 19:44
  • First I had to do getDialog but that is fine. Upon adding the code, it says the type required is the fragment(MyFragment f = new MyFragment()) requires MyFragment, not boolean – Zoe Sep 20 '16 at 19:47
  • @Polarbear0106 check the new Edit – Lucas Queiroz Ribeiro Sep 20 '16 at 19:57
  • No crash but no effect. I don't understand why this doesn't work. Even when adding getFragmentManager nothing changes. – Zoe Sep 20 '16 at 20:00
  • it's strange indeed, did you tried the suggestion in the another question ? in `onCreateDialog()` – Lucas Queiroz Ribeiro Sep 20 '16 at 20:05
  • Yes, still nothing – Zoe Sep 20 '16 at 20:08
  • Maybe another feature in your code is somehow breaking this, try create a all new Dialog, with other layout resource/style, then add the features back one by one to find what's wrong, because the code to remove the title is right, he should be working – Lucas Queiroz Ribeiro Sep 20 '16 at 20:11
  • 1
    so I did some work and I managed to solve it. I have been running Instant run, and upon removing it, everything seems to work(for now) – Zoe Sep 21 '16 at 13:57
  • Oh god, it's true, at least for me, instant run is a nightmare, always break something – Lucas Queiroz Ribeiro Sep 21 '16 at 14:15
  • `getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);` is enough to solve the problem. it doesn't need `HelpDialog.this. ...` – Alireza Noorali Nov 26 '17 at 10:46