Reputation: 231
MyCustomDialogFragment always wraps it's content and does not keep the min width like a Alertdialog. How can I set the width to respond as a standard Android dialog ?
This is the function I call to display the dialog:
private void displayDialogEdit(String title, String edit,
MyCustomEditDialog.MyCustomEditDialogListener listener, boolean onlyNumbers){
MyCustomEditDialog dialog = MyCustomEditDialog.newInstance(title, edit, onlyNumbers);
dialog.setInterface(listener);
dialog.show(getSupportFragmentManager(), "edit");
}
This is MyCustomDialogFragment.
public class MyCustomEditDialog extends DialogFragment {
private MyCustomEditDialog.MyCustomEditDialogListener mInterface;
public static MyCustomEditDialog newInstance(String title, String editText, boolean onlyNumbers){
MyCustomEditDialog dialog = new MyCustomEditDialog();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("edit", editText);
args.putBoolean("numbers", onlyNumbers);
dialog.setArguments(args);
return dialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_edit_dialog, container);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
// Bundle args = getArguments();
//
// boolean numbers = args.getBoolean("numbers");
//
// TextView txt = (TextView)v.findViewById(R.id.title);
// txt.setText(args.getString("title"));
//
// final EditText edit = (EditText) v.findViewById(R.id.editText);
// edit.setText(args.getString("edit"));
//
// if (numbers) edit.setInputType(InputType.TYPE_CLASS_NUMBER);
//
// Button btn = (Button)v.findViewById(R.id.save);
//
// btn.setOnClickListener(new View.OnClickListener() {
// @Override public void onClick(View view) {
// if (mInterface!=null) mInterface.customDialogOnClick(edit.getText().toString());
// }
// });
//
// setCancelable(true);
return v;
}
public void setInterface(MyCustomEditDialog.MyCustomEditDialogListener listener){
this.mInterface = listener;
}
public interface MyCustomEditDialogListener {
void customDialogOnClick(String result);
}
}
This is my layout for the custom dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:gravity="center_horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="@color/blue_dark"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:layout_marginBottom="10dp"
android:id="@+id/title"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:inputType="text"
/>
<Button
style="@style/btnBasic.blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/save"
android:id="@+id/save" />
</LinearLayout>
Sample of result:
Upvotes: 1
Views: 2826
Reputation: 9
onViewCreated write this code:
dialog?.window?.apply {
setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
}
Upvotes: 0
Reputation: 2761
I came across this issue today and I found an easy solution: you can override the getTheme()
method from DialogFragment
and return R.style.Theme_AppCompat_Light_Dialog_Alert
from it (or any other theme you want, I used the AppCompat
one so this will work in older versions of Android).
Example code in Kotlin:
class ExampleDialog : DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.example_dialog, container, false)
}
override fun getTheme(): Int {
return R.style.Theme_AppCompat_Light_Dialog_Alert
}
}
Upvotes: 1
Reputation: 231
Found a simple solution to keep the standard with of a alertDialog on a fragmentDialog.
Override the onCreate method from the fragment with this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Material_Light_Dialog_Alert);
}
Upvotes: 3
Reputation: 4182
Try these method
set layout width and height in code by
alertDialog.getWindow().setLayout(600, 400);
set on your main theme for dialog
android:theme="@android:style/Theme.Dialog"
Theme
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:dialogTheme">@style/dialog_light</item>
</style>
<style name="dialog_light" parent="@android:style/Theme.Dialog">
<item name="@android:windowBackground">@color/whitegrey</item>
<item name="@android:textColor">@color/black</item>
</style>
Set theme In code Also like that
new AlertDialog.Builder(
new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
Upvotes: 1