Android DialogFragment Fullscreen Like Activity
August 27, 2019We can use DialogFragment
to show a fullscreen overlay (like loading an Activity, but with Fragment).
Why not use a Fragment
instead of DialogFragment
. With Fragment
, you would need to prepare a fullscreen layout and load/replace the Fragment
into the layout. With DialogFragment
, there is no need of such layout.
The following code and layout will create a layout with black overlay which filled 90% of the screen.
class GiftDialog : DialogFragment() {
companion object {
private const val FRAGMENT_TAG = "gift_dialog"
fun newInstance() = GiftDialog()
fun show(fragmentManager: FragmentManager): GiftDialog {
val dialog = newInstance()
// dialog.isCancelable = false
dialog.show(fragmentManager, FRAGMENT_TAG)
return dialog
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// return super.onCreateView(inflater, container, savedInstanceState)
return activity!!.layoutInflater.inflate(R.layout.gift, container)
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="?dialogPreferredPadding"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World" />
/>
</LinearLayout>
</RelativeLayout>
NOTE: If you use LinearLayout as root layout, the dialog size will shrink to minimum. Refer Android DialogFragment Match Parent Width and Height.
To match 100% with and height (like an Activity) without black overlay background.
class GiftDialog : DialogFragment() {
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar)
// this will make it fullscreen without top status bar
// setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen)
}
}
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn