How to Create a Dialogfragment Without Title

How to create a DialogFragment without title?

Just add this line of code in your HelpDialog.onCreateView(...)

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

This way you're explicitly asking to get a window without title :)



EDIT

As @DataGraham and @Blundell pointed out on the comments below, it's safer to add the request for a title-less window in the onCreateDialog() method instead of onCreateView(). This way you can prevent ennoying NPE when you're not using your fragment as a Dialog:

@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;
}

how to remove title from DialogFragment?

you must override onCreateDialog method in your dialogfragment class.

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
RelativeLayout root = new RelativeLayout(getActivity());
root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(root);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

return dialog;
}

Removing title of a DialogFragment

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");

Fragment Dialog without Title issue

put this code in your onCreateView() of dialog fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_image, null);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}

It will remove title bar from Dialog Fragment

DialogFragment with no title shrinks dialog width

The issue seems to be when using LinearLayout as a root layout. I switched to RelativeLayout everything fit into place. Maybe this is a bug when using LinearLayout as a root layout in dialogs and removing title?

Android: How to create a Dialog without a title?

You can hide the title of a dialog using:

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


Previous version of this answer, which is overcomplicated:

You need to use an AlertDialog. There's a good explanation on the Android Developer's site about custom dialogs.

In very short summary, you do this with code like copied below from the official website. That takes a custom layot file, inflates it, gives it some basic text and icon, then creates it. You'd show it then with alertDialog.show().

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();

In response to comment:

I assume that TextView with the id nr is in the View you are inflating with View view = inflater..... If so, then you need to change just one bit: instead of dialog.findView... make it view.findView.... Then once you've done that, remember to use dialog.show(), or even builder.show() without bothering to do builder.create().

How to set the title of DialogFragment?

You can use getDialog().setTitle("My Dialog Title")

Just like this:

public static class MyDialogFragment extends DialogFragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set title for this dialog
getDialog().setTitle("My Dialog Title");

View v = inflater.inflate(R.layout.mydialog, container, false);
// ...
return v;
}
// ...
}


Related Topics



Leave a reply



Submit