How To Set Left And Right Margin In Buttomsheetdialogfragment Android?
I tried to set Margin in ButtonSheetDialogFragment layout but its not working. I have tried to set margin from layout and programmatically but its has same result This is my XML fi
Solution 1:
Use this code for BottomSheetDialog
<stylename="BottomSheetDialog"parent="Theme.Design.Light.BottomSheetDialog"><itemname="bottomSheetStyle">@style/bottomSheetStyleWrapper</item></style><stylename="bottomSheetStyleWrapper"parent="Widget.Design.BottomSheet.Modal"><itemname="behavior_peekHeight">350dp</item><itemname="android:layout_margin">30dp</item></style>
BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
dialog.setContentView(R.layout.custom_dialog);
dialog.show();
Solution 2:
Call requestLayout()
once you are done setting the margin.
In your case, something like
contentView.requestLayout();
after adding left margin.
Solution 3:
You can do this by setting layoutParams.xxMrgin
of the root layout of the dialog; but his requires also to use a different theme:
Java:
@OverridepublicvoidonViewCreated(@NonNull@NotNull View view, @Nullable@org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FrameLayout.LayoutParamslayoutParams=
(FrameLayout.LayoutParams) view.getLayoutParams();
intmargin_10dp= dpToPx(10);
layoutParams.rightMargin = dpToPx(margin_10dp );
layoutParams.leftMargin = dpToPx(margin_10dp);
view.setLayoutParams(layoutParams);
view.requestLayout();
}
privateintdpToPx(int dp) {
Resourcesr= getResources();
intpx= (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
r.getDisplayMetrics()
);
return px;
}
Kotlin:
overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val layoutParams: FrameLayout.LayoutParams =
view.layoutParams as FrameLayout.LayoutParams
layoutParams.bottomMargin = 32.toPx().toInt()
layoutParams.rightMargin = 32.toPx().toInt()
layoutParams.leftMargin = 32.toPx().toInt()
view.layoutParams = layoutParams
}
fun Number.toPx() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics
)
Changing the theme:
Option 1: Easy and neat
Create this style that removes the background color:
<stylename="NoBackgroundDialogTheme"parent="Theme.AppCompat.Light.Dialog"><itemname="android:windowBackground">@null</item></style>
Then apply it to the dialog by overriding getTheme()
:
// KotlinoverridefungetTheme(): Int {
return R.style.NoBackgroundDialogTheme
}
// Java@Overridepublic int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
Option 2:
Set android.R.style.Theme_Translucent
theme to the dialog:
@Override
publicintgetTheme() {
// Step 1return android.R.style.Theme_Translucent;
}
But this requires to retain the dimmed background again:
getDialog().getWindow().setBackgroundDrawableResource(R.color.color_trans);
The entire class:
Kotlin using option 1:
classMyDialogFragment() : BottomSheetDialogFragment() {
overridefungetTheme(): Int {
return R.style.NoBackgroundDialogTheme
}
overridefunonCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view: View = View.inflate(context, R.layout.fragment_bottomsheet, null)
return view
}
overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
addMargin(view)
}
privatefunaddMargin(view: View) {
val layoutParams: FrameLayout.LayoutParams =
view.layoutParams as FrameLayout.LayoutParams
layoutParams.bottomMargin = 32.toPx().toInt()
layoutParams.rightMargin = 32.toPx().toInt()
layoutParams.leftMargin = 32.toPx().toInt()
view.layoutParams = layoutParams
}
fun Number.toPx() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
Resources.getSystem().displayMetrics
)
}
Java using option 2:
publicclassButtomSheetFragmentextendsBottomSheetDialogFragment {
//...@OverridepublicintgetTheme() {
// Step 1return android.R.style.Theme_Translucent;
}
@OverridepublicvoidonViewCreated(@NonNull@NotNull View view, @Nullable@org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Step 2
getDialog().getWindow().setBackgroundDrawableResource(R.color.color_trans);
// Step 3
addMargin(view);
}
privatevoidaddMargin(View view) {
FrameLayout.LayoutParamslayoutParams=
(FrameLayout.LayoutParams) view.getLayoutParams();
intmargin_10dp= dpToPx(10);
layoutParams.rightMargin = dpToPx(margin_10dp );
layoutParams.leftMargin = dpToPx(margin_10dp);
view.setLayoutParams(layoutParams);
view.requestLayout();
}
privateintdpToPx(int dp) {
Resourcesr= getResources();
intpx= (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
r.getDisplayMetrics()
);
return px;
}
}
Post a Comment for "How To Set Left And Right Margin In Buttomsheetdialogfragment Android?"