I have a DialogFragment
with an xml layout and I need it to be, let's say 75% as wide as the screen. When I look for answers I always find the common weight
-solution, which does not help me, as my fragment overlays the current activity as shown below and does not take the full screen's size. I also don't want to use "ems" as a fix width as I can't know the user's screen size.
Is there a way (preferably in xml) to set the root LinearLayout
's width to said 75%? I think it would be a possible workaround to define a whole set of LinearLayout
s and make some of them transparent but this seems to be a rather ugly solution...
If it helps, here is the fragment's layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:orientation = "vertical" >
<EditText
android:hint = "@string/inputhint_feedbackName"
android:id = "@+id/input_feedbackName"
android:imeOptions = "actionNext"
android:inputType = "textPersonName"
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:lines = "1"
android:maxLines = "1"
android:singleLine = "true" >
<requestFocus />
</EditText>
<EditText
android:gravity = "top"
android:id = "@+id/input_feedbackMessage"
android:imeOptions = "actionDone"
android:layout_height = "wrap_content"
android:layout_marginBottom = "20dp"
android:layout_width = "match_parent"
android:lines = "10"
android:inputType = "textMultiLine" />
<Button
android:id = "@+id/button_feedbackSend"
android:layout_height = "wrap_content"
android:layout_width = "match_parent"
android:text = "@string/label_go"
android:textSize = "16sp"
android:textStyle = "bold" />
</LinearLayout>
If you use LinearLayout for the DialogFragment layout, the dialog size will shrink to minimum. The problem can be solved by using RelativeLayout, where dialog is shown with normal size.
If you have a dialog fragment based on layout, you can use any of these 2 methods. If you use onCreateView, then you should use onActivityCreated to set width. If you use onCreateDialog instead of onCreateView, you can set parameters there. onActivityCreated won't be needed. override fun onCreateDialog (savedInstanceState: Bundle?):
The problem can be solved by using RelativeLayout, where dialog is shown with normal size. NOTE: If LinearLayout.layout_height="wrap_content", the dialog will match content size.
If you want your landscape dialog to not be so wide, you can do a check of whether getResources ().getConfiguration ().orientation == Configuration.ORIENTATION_PORTRAIT beforehand. The dimension in outermost layout doesn't work in dialog. You can add a layout where set dimension below the outermost.
You can achieve this by setting a style to your dialogfragment on it's onCreate method:
setStyle(DialogFragment.STYLE_NORMAL,R.style.yourStyle);
and in your styles.xml
<style name="yourStyle" parent="android:Theme.Dialog">
<item name="android:windowBackground">@drawable/some_background</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowMinWidthMajor">75%</item>
<item name="android:windowMinWidthMinor">75%</item>
<item name="android:windowNoTitle">true</item>
</style>
I don't think it is possible to achieve this without using code.
Anyway I'm using this code inside my custom DialogFragment class. Hope it helps.
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
Point size = new Point();
Display display = window.getWindowManager().getDefaultDisplay();
display.getSize(size);
int width = size.x;
window.setLayout((int) (width * 0.75), WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With