Android DialogFragment Match Parent Width and Height (90% size with black opacity background)
April 21, 2019DialogFragment size too small
Solution 1: User relative layout
If you use LinearLayout
for the DialogFragment
layout, the dialog size will shrink to minimum.
The problem can be solved by using RelativeLayout
, where dialog is shown with normal size.
<?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="wrap_content"
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 LinearLayout.layout_height="wrap_content"
, the dialog will match content size. If LinearLayout.layout_height="match_parent"
, the dialog will go full/90% height.
class TestDialog : DialogFragment() {
companion object {
private const val FRAGMENT_TAG = "test_dialog"
fun newInstance() = TestDialog()
fun show(supportFragmentManager: FragmentManager): SyncSheetsDialog {
val dialog = newInstance()
dialog.show(supportFragmentManager, 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.test, container)
}
override fun onStart() {
super.onStart()
}
}
Solution 2: dialog.window.setLayout
class TestDialog : DialogFragment() {
override fun onStart() {
super.onStart()
dialog?.window?.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
}
}
References:
- 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