Issue
How can I make the keyboard appear when opening the BottomSheetDialog and the dialog itself appears above it? And EditText immediately became active.
Now my dialog is shown like this:
Here is my code:
abstract class CustomDialog(@LayoutRes layout: Int) : DialogFragment() {
val layoutDialog = layout
val dialogView: View? by lazy { View.inflate(activity, layout, null) as ViewGroup }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return dialogView
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val bottomSheetDialog = BottomSheetDialog(requireActivity(), R.style.BottomSheetDialogTheme)
val bottomSheetView = LayoutInflater.from(context).inflate(layoutDialog, null)
bottomSheetDialog.setContentView(bottomSheetView)
return bottomSheetDialog
}
}
Create dialog:
class CreateTaskDialog() : CustomDialog(R.layout.dialog_add_task) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
Here I show the dialog:
requireActivity().showDialog(CreateTaskDialog())
fun FragmentActivity.showDialog(dialog: DialogFragment, tag: String? = null)
= dialog.show(this.supportFragmentManager, tag)
This is style:
<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/BottomSheetStyle</item>
</style>
<style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="android:background">@android:color/transparent</item>
</style>
Solution
The solution was not obvious) I have searched through the entire stackoverflow. I found several solutions, but none of them helped me until I made the style as follows (an example immediately with rounded edges):
<style name="BottomSheetDialogKeyboardTheme" parent="Theme.MaterialComponents.DayNight.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">adjustResize</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:background">@android:color/transparent</item>
<item name="shapeAppearanceOverlay">@style/ShapeBottomSheetDialogKeyboard</item>
</style>
<style name="ShapeBottomSheetDialogKeyboard" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">16dp</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
When creating a fragment, I also added such a moment (although it also works fine without it):
val bottomSheet = bottomSheetDialog.window?.decorView?.findViewById<FrameLayout>(R.id.design_bottom_sheet) as FrameLayout
val mBehavior = BottomSheetBehavior.from(bottomSheet)
mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
And to apply the style with rounded edges, I had to add another code like this:
val newMaterialShapeDrawable: MaterialShapeDrawable = createMaterialShapeDrawable(requireContext(), bottomSheet)
ViewCompat.setBackground(bottomSheet, newMaterialShapeDrawable)
fun createMaterialShapeDrawable(context: Context, bottomSheet: View): MaterialShapeDrawable {
val shapeAppearanceModel = ShapeAppearanceModel
.builder(context, 0, R.style.ShapeBottomSheetDialogKeyboard)
.build()
val currentMaterialShapeDrawable = bottomSheet.background as MaterialShapeDrawable
val newMaterialShapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)
newMaterialShapeDrawable.initializeElevationOverlay(context)
newMaterialShapeDrawable.fillColor = currentMaterialShapeDrawable.fillColor
newMaterialShapeDrawable.tintList = currentMaterialShapeDrawable.tintList
newMaterialShapeDrawable.elevation = currentMaterialShapeDrawable.elevation
newMaterialShapeDrawable.strokeWidth = currentMaterialShapeDrawable.strokeWidth
newMaterialShapeDrawable.strokeColor = currentMaterialShapeDrawable.strokeColor
return newMaterialShapeDrawable
}
The entire CustomDialog code turned out like this:
abstract class CustomDialogKeyboard(@LayoutRes layout: Int) : BottomSheetDialogFragment() {
private val layoutDialog = layout
private lateinit var mBehavior: BottomSheetBehavior<FrameLayout>
val dialogView: View? by lazy { View.inflate(activity, layout, null) as ViewGroup }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return dialogView
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val bottomSheetDialog = BottomSheetDialog(requireActivity(), R.style.BottomSheetDialogKeyboardTheme)
val bottomSheetView = LayoutInflater.from(context).inflate(layoutDialog, null)
bottomSheetDialog.setContentView(bottomSheetView)
bottomSheetDialog.setOnShowListener {
try {
val bottomSheet = bottomSheetDialog.window?.decorView?.findViewById<FrameLayout>(R.id.design_bottom_sheet) as FrameLayout
mBehavior = BottomSheetBehavior.from(bottomSheet)
mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
val newMaterialShapeDrawable: MaterialShapeDrawable = createMaterialShapeDrawable(requireContext(), bottomSheet)
ViewCompat.setBackground(bottomSheet, newMaterialShapeDrawable)
} catch (e: Exception) {}
}
return bottomSheetDialog
}
}
Answered By - Виктор Шамрук
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.