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