I am working on an android application where I am using DialogFragment
to display the dialog but its width is very small. How I can make this width to fil
This is the solution I figured out to handle this issue:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
Edit:
You can use the code below in onCrateView
method of Fragment before return the inflated view.
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Create a custom style in your style.xml file. Just copy paste this code into your style.xml file
<style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
<item name="android:windowBackground">@null</item>
<item name="android:windowIsFloating">true</item>
</style>
Then in the createDialog method of your DialogFragment, create the dialog object by the code
dialog = new Dialog(getActivity(), R.style.CustomDialog);
This is working for me and hope this will help you too
User AlertDialog.Builder inside your DialogFragment. Add it in onCreateDialog
method like this. And in onCreateView
do nothing.
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
View view = LayoutInflater.from(getContext()).inflate(R.layout.gf_confirm_order_timeout_dialog, null);
final Bundle args = getArguments();
String message = args.getString(GfConstant.EXTRA_DATA);
TextView txtMessage = (TextView) view.findViewById(R.id.txt_message);
txtMessage.setText(message);
view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (mListener != null)
{
mListener.onDialogConfirmOK();
}
}
});
builder.setView(view);
Dialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
in your layout root, set android:minWidth to a very large value e.g
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="9999999dp">
...
</LinearLayout>
I want clarify this. Both the ways are right, But with different DialogFragment.
android.app.DialogFragment
@Override
public void onStart()
{
super.onStart();
Dialog dialog = getDialog();
if (dialog != null)
{
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width, height);
}
}
android.support.v4.app.DialogFragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
This works for me perfectly.
android:minWidth="300dp"
Through this you can give the width to dialog Fragment.