5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DIalogFrgamentを画面いっぱいに表示する

Last updated at Posted at 2019-06-01

# やりたいこと
DialogFragmentはデフォルトだと画面いっぱいに表示されない為、画面いっぱいに表示する

実装

MainActivity.kt
class MainActivity {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()
        StampDialogFragment().show(supportFragmentManager, StampDialogFragment::class.java.simpleName)
    }
}
FullWindowDialogFragment.kt
class FullWindowDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireActivity())
        dialog.setContentView(R.layout.view_full_window_custom) // custom layout
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))    // 必要なさそうだが、これがないと全画面にならない
        dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        return dialog
    }

}
view_full_window_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Full Window Custom View"
            android:textColor="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

layoutが真っ黒の為、背景が黒い画面が全体に表示される
スクリーンショット 2019-06-01 20.39.34.png

CustomViewにする

FullWindowDialogFragment.kt
class FullWindowDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireActivity())
        dialog.setContentView(FullWindowCustomView(requireContext())) // custom layout
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))    // 必要なさそうだが、これがないと前画面にならない
        dialog.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        return dialog
    }

    // custom view
    private class FullWindowCustomView(context: Context) : FrameLayout(context) {
        init {
            View.inflate(context, R.layout.view_full_window_custom, this)
        }
    }
}
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?