ViewStub inflation of ConstraintLayout - android

I want to leverage ViewStub to optionally show a portion of UI. When the root of my inflated item is ConstraintLayout then it doesn't render.
But if that root is MaterialCardView then it displays as intended.
<!-- my_fragment.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragmentRootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewStub
android:id="#+id/fragmentViewStub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="#+id/fragmentViewStub"
android:layout="#layout/item"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- item.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/itemRootLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/itemLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/itemLabelText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
/* MyFragment.kt */
#AndroidEntryPoint
class MyFragment : Fragment(R.layout.my_fragment) {
private val viewModel: MyViewModel by viewModels()
private val viewBinding: MyFragmentBinding by viewLifecycleLazy {
MyFragmentBinding.bind(requireView())
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewBinding.fragmentViewStub.inflate()
}
}
I've tried matching the ViewStub's id/inflatedId attributes and programmatically setting dimensions/constraints but neither solved the issue.
What am I overlooking about making these pieces work together?

Forgot to update with my answer/oversight. This worked as expected in both cases all along.
Nightmode was enabled and the behavior of MaterialCardView has automatic contrast between its card and content. Meanwhile, when ConstraintLayout inverts its theme the label appears to be missing but is indeed present, as white text on white background.

Related

Loading Dialog will not go transparent

I have surprisingly been stuck on this one for a little while.
User Story:
The user should see a Loading Dialog that can be reused through the application with a transparent background so you only see the progress spinner and the text under the progress spinner.
Currently, I have a DialogFragment that inflates this XML to present itself:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="wrap_content"
android:background="#android:color/transparent">
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="1"
android:background="#android:color/transparent"
android:indeterminateDrawable="#drawable/loading_spinner"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:alpha="1"
android:background="#android:color/transparent"
android:text="Loading..."
android:textColor="#FFFFFF"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am trying to set the transparency in the background and have had these results:
alpha set changes children elements to transparent as well
above XML setting does nothing and shows a white background
Setting it programmatically(See below) also does nothing and displays it white.
LoadingDialog():
class LoadingDialog(): DialogFragment() {
private var _binding: FragmentLoadingDialogBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState:Bundle?
): View? {
_binding = FragmentLoadingDialogBinding.inflate(inflater, container, false)
return binding.root
}
//Always do this in Dialog to maintain memory management
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
How can I get the above LoadingDialog to present the Loading Progress Spinner and the Text without the white background?
I think you should call onCreate() method before setting the background to transparent after which you should set the view for your dialog. Try this
val dialogBinding = // inflate dialog here using dataBinding for example
val customDialog = AlertDialog.Builder(this, 0).create() // works with other dialogs as well
customDialog.apply {
window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
setView(dialogBinding?.root)
}.show()
I hope this helps you solve your problem. For more information about Dialogs in Android, please refer to my article on section.io
something else, consider removing the transparent background on your root viewGroup as shown below
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
...
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
edit to
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
...
android:layout_width="match_parent"
android:layout_height="wrap_content">

How to use binding in both a fragment and within its included layout?

Say I have a fragment defined as:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="cpaTabViewModel"
type="org.romco.example.CpaTabViewModel" />
</data>
<LinearLayout
android:id="#+id/fragment_pac_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/testTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{cpaTabViewModel.testString}"
tools:text="test" />
<include
android:id="#+id/addPerformedPAField"
layout="#layout/add_performed_pa"
app:cpaTabViewModel="#{cpaTabViewModel}"/>
[...]
</LinearLayout>
</layout>
The included layout is add_performed_pa.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="cpaTabViewModel"
type="org.romco.example.CpaTabViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="24dp"
tools:showIn="#layout/cpa_tab_fragment">
<Spinner
android:id="#+id/inputActivitySpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAlignment="textStart"
app:layout_constraintStart_toEndOf="#+id/cancelButton"
app:layout_constraintTop_toTopOf="#+id/cancelButton"
app:entries="#{cpaTabViewModel.activities}" <---- THIS CAUSES THE PROBLEM
tools:listitem="#layout/sample_textview" />
[...]
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
The class representing the fragment is CpaTabFragment.kt:
class CpaTabFragment : Fragment() {
private lateinit var cpaTabFragBinding: CpaTabFragmentBinding
private val cpaTabViewModel: CpaTabViewModel by lazy {
ViewModelProvider(this, CpaTabViewModel.Factory(activity?.application!!))
.get(CpaTabViewModel::class.java)
.apply {
setPeriodType(arguments?.getSerializable(ARG_SECTION_TYPE) as PlanPeriodType)
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
cpaTabFragBinding = DataBindingUtil.inflate(inflater, R.layout.cpa_tab_fragment, container, false)
cpaTabFragBinding.testTextView.text = "TEST HEADER"
cpaTabFragBinding.addPerformedPAField.root.visibility = View.GONE
cpaTabFragBinding.cpaTabViewModel = cpaTabViewModel
// (this next line is replaced by passing the viewModel in the <include app:cpaTabViewModel="#{cpaTabViewModel} - I hope this is correct, but that's beside the point here)"
// cpaTabFragBinding.addPerformedPAField.cpaTabViewModel = cpaTabViewModel
return cpaTabFragBinding.root
}
Obviously, I removed most of the unimportant (or at least what I'm hoping is unimportant) code above, leaving only what I think is relevant here.
Without actually using data-binding in the add_performed_pa.xml layout (the line marked with "THIS CAUSES THE PROBLEM"), the entire thing works fine. I can access the included layout as a binding just fine, change it's root visibility, even access any elements in it, etc.
However, as soon as I try to add any data-binding into the included .xml, app won't compile, and the error is the following:
...\app\build\generated\source\kapt\debug\org\romco\example\DataBinderMapperImpl.java:18: error: cannot find symbol
import org.romco.example.databinding.AddPerformedPaBindingImpl;
I can't really find anyone else with this problem. Can you please help?

AndroidStudio: Explode animation doesn't work when i set background color to the root layout in the xml

I don't know if it is a bug or i am making some mistake but when i set Explode animation when opening an Activity it animates like a Slide animation from top to bottom. I did some trial and error and it turns out that when i use a custom background color in the root layout in the xml file this unexpected behavior occures. When i remove the background color everything works as expected.
Can anyone tell me what's going on here??? Because it is important for me to set background color in the root layout.
Here's a sample of my xml code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#000000" // This causes the unexpected behavior.
tools:context=".ExplodeActivity">
<ImageView
android:id="#+id/imageView2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/b" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the ExplodeActivity 's onCreate method where i am assigning the animation:
class ExplodeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_explode)
val enterTransition = Explode()
enterTransition.duration = 500
window.enterTransition = enterTransition
}
}
Here's the previous activity's code which responsible for starting the ExplodeActivity:
val options = ActivityOptions.makeSceneTransitionAnimation(this).toBundle()
val intent = Intent(this, ExplodeActivity::class.java)
startActivity(intent, options)

Android layout in preview different when I execute the app

I made an app with a fragment and a recyclerviw inside.
The Fragment layout is this
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="homeViewModel"
type="com.phatedev.eliquidcalculator.ui.home.HomeViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/e_liquid_list"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:listData="#{homeViewModel.eLiquids}"
tools:listitem="#layout/list_e_liquid_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
and the layout of the single row list_e_liquid_item is
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="eLiquid"
type="com.phatedev.eliquidcalculator.domains.ELiquid" />
</data>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="8dp"
android:backgroundTint="#00AA44">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!-- Title, secondary and supporting text -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{eLiquid.name}"
tools:text="Banana, Fragola, Ananas"
android:textAppearance="?attr/textAppearanceHeadline6" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#{eLiquid.description}"
tools:text="https://www.netflix.com/watch/80111460?trackId=155573558"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#{eLiquid.base}"
tools:text="80/20"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</layout>
In the Android Studio preview everything look perfect, as you can see in this screenshot that I take on Android Studio
but when I run the app on the emulator the layout is broke and I don't understand what I'm doing wrong
EDIT:
The app:listData="#{homeViewModel.eLiquids}" in RecyclerView is this bind function
#BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: List<ELiquid>?) {
val adapter = recyclerView.adapter as ELiquidAdapter
adapter.submitList(data)
}
EDIT: Ok, I found a solution to my problem here but can someone explain me why it work?
You didn't share your adapter code, but the issue would likely be that you are inflating the item view without "parent" parameter. I have faced the same issue and below are the changes I have made to fix the problem.
Before:
No parent parameter specified - list item width will be set to wrap content even though set to match parent in the xml.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(ListViewItemBinding.inflate(LayoutInflater.from(parent.context)))
}
After:
Specified 'parent' parameter and the list item is inflated properly matching the parent view. Also parameter false is added, as recycler view will be attaching the view.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(ListViewItemBinding.inflate(LayoutInflater.from(parent.context),parent, false))
}
It also differs from phone to another because of the density variations
edit your layout to matchparent the width
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:backgroundTint="#00AA44">
try to change in your activity.xml
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
to
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
Actally i have also faced same kind of problem it looks fine in preview must while run ii it is getting wrap around so what i did is.please add dummy match_parent View like below in your linearlayout of list_e_liquid_item file.
<View
android:layout_width=match_parent
android :layout_height= 1dp
/>
Please let me know if it doent work.

looking for Android dialog widget with rounded corners

I'm looking for an Android dialog component like this screenshot:
Apologies for the white dialog on a white background but hopefully you can see the nice elevation shadow, the rounded corners, and importantly the close button on the top right of the dialog.
(The screenshot is taken from a tab-less version of the Chrome app).
Does anyone know of a library/snippet that achieves this, or something like it? Thanks.
edit:
Some background info which hopefully will make the question clearer: I have a scrolling activity which has a lot of content - text and data - in which I would like to show further details/images via clickable text/image. Yes I could use an AlertDialog but I much prefer the look of the dialog window in the screenshot.
you can try this layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:background="#B4CCCCCC"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</androidx.cardview.widget.CardView>
Well, following the suggestion above, this is the result of a rough test of a basic activity, containing a CardView, with the Theme.Holo.Dialog theme:
And using a CardView does show the rounded corners and elevation nicely, as required, but there is a big problem - the original layout (the one from where the dailog is called) isn't seen underneath and below the dialog, this area of the screen is blank. So it's not acting like a dialog. A custom dialog, based on DialogFragment, is the only way forward, and that is my next objective.
Hmm, I've failed. This is the best I can come up with:
The dialog is now acting like a dialog, placed above the user screen, and the CardView widget, as suggested above, is fine, but the DialogFragment places it within a white rectangular window. Not what I wanted. In case someone else feels the urge to take up the challenge, here is my layout xml, the dialog class and the code snippet that calls it:
dialog_bigly.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="550dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.6">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text=" Dialog Title here"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/txtDialog_title"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" app:srcCompat="#drawable/ic_do_not_disturb"
android:id="#+id/btnDialog_close" android:layout_gravity="right" android:clickable="true"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent" app:srcCompat="#drawable/test_image207x344"
android:id="#+id/ivDialog_Image"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
DialogBig.kt
package ...
import ...
class DialogBig: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity!!)
val inflater = activity!!.layoutInflater
val dialogView = inflater.inflate(R.layout.dialog_bigly, null)
val txtTitle = dialogView.findViewById<TextView>(R.id.txtDialog_title)
val imgImage = dialogView.findViewById<ImageView>(R.id.ivDialog_Image)
val btnClose = dialogView.findViewById<ImageView>(R.id.btnDialog_close)
builder.setView(dialogView)
btnClose.setOnClickListener { dismiss() }
txtTitle.text = "dialog bigly title"
return builder.create()
}
}
and finally code snippet
btn_test.setOnClickListener {
//create a new bigly dialog
val dialog = DialogBig()
dialog.show(supportFragmentManager, "testing")
}
I have finally found a solution (one month later). #Bunny suggests making the background of the dialog fragment transparent.
Specificially, in the onCreateView of the DialogFragment class, add:
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
This works, but with the restriction that it has to be a 'drawable' background. But first of all, the result - a dialog with rounded corners:
Okay, the code is as follows. Here is my 'rounded_corners.xml' shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="30dp" />
<padding
android:bottom="16dp"
android:left="16dp"
android:right="16dp"
android:top="16dp" />
</shape>
Which is used in the dialog fragment layout, dialog_bigly.xml, within the base CardView widget:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/base_card"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#drawable/rounded_corners"
android:orientation="vertical"
app:cardCornerRadius="20dp"
app:cardElevation="10dp" >
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtDialog_title"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="2dp"
android:fontFamily="#font/open_sans"
android:text="Dialog Title here"
app:layout_constraintBottom_toBottomOf="#+id/btnDialog_close"
app:layout_constraintEnd_toStartOf="#+id/btnDialog_close"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/btnDialog_close"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="8dp"
style="#style/Widget.AppCompat.ActionButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_red_close_24x24" />
<ImageView
android:id="#+id/ivDialog_Image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintTop_toBottomOf="#+id/txtDialog_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="#drawable/test_image185x285" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
And lastly here is the DialogBig.kt kotlin code:
package ...
import ...
class DialogBig: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity!!)
val inflater = activity!!.layoutInflater
val dialogView = inflater.inflate(R.layout.dialog_bigly, null)
val txtTitle = dialogView.findViewById<TextView>(R.id.txtDialog_title)
val imgImage = dialogView.findViewById<ImageView>(R.id.ivDialog_Image)
val btnClose = dialogView.findViewById<ImageView>(R.id.btnDialog_close)
builder.setView(dialogView)
btnClose.setOnClickListener { dismiss() }
txtTitle.text = "my bigly dialog test title"
return builder.create()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
return super.onCreateView(inflater, container, savedInstanceState)
}
}
Although I don't really understand how this works, I'm glad to have found a solution. I think part of the issue is trying to get access to the fragment holder container in the depths of the DialogFragment class to apply the transparency, rather than simply the fragment layout itself.

Categories

Resources