@@ -81,7 +81,36 @@ class XMLMaskingActivity : ComponentActivity() {
8181 gravity = Gravity .CENTER
8282 }
8383
84- val popupView = FloatingPopupView (this ).apply {
84+ val popupView = FloatingPopupView (this , " Floating Popup" ).apply {
85+ onSendClicked = {
86+ Toast .makeText(this @XMLMaskingActivity, " Send clicked" , Toast .LENGTH_SHORT ).show()
87+ }
88+ onDismissRequested = {
89+ try {
90+ windowManager.removeView(this )
91+ } catch (_: Exception ) {
92+ }
93+ }
94+ }
95+
96+ windowManager.addView(popupView, params)
97+ }
98+
99+ findViewById<Button >(R .id.button_attached_dialog).setOnClickListener {
100+ val windowManager = getSystemService(WINDOW_SERVICE ) as WindowManager
101+
102+ val params = WindowManager .LayoutParams ().apply {
103+ width = WindowManager .LayoutParams .MATCH_PARENT
104+ height = WindowManager .LayoutParams .MATCH_PARENT
105+ format = PixelFormat .TRANSLUCENT
106+ flags = WindowManager .LayoutParams .FLAG_LAYOUT_IN_SCREEN
107+ type = WindowManager .LayoutParams .TYPE_APPLICATION_ATTACHED_DIALOG
108+ // Attach to this window so it behaves like an attached dialog
109+ token = window.decorView.windowToken
110+ gravity = Gravity .CENTER
111+ }
112+
113+ val popupView = FloatingPopupView (this , " Attached Dialog" ).apply {
85114 onSendClicked = {
86115 Toast .makeText(this @XMLMaskingActivity, " Send clicked" , Toast .LENGTH_SHORT ).show()
87116 }
@@ -107,7 +136,7 @@ class XMLMaskingActivity : ComponentActivity() {
107136 }
108137}
109138
110- class FloatingPopupView (context : Context ) : FrameLayout(context) {
139+ class FloatingPopupView (context : Context , private val headerTitle : String ) : FrameLayout(context) {
111140
112141 var onSendClicked: (() -> Unit )? = null
113142 var onDismissRequested: (() -> Unit )? = null
@@ -117,13 +146,24 @@ class FloatingPopupView(context: Context) : FrameLayout(context) {
117146 isClickable = true
118147 isFocusable = true
119148
120- val content = LinearLayout (context).apply {
121- orientation = LinearLayout .HORIZONTAL
149+ // White card container with vertical layout to host header and row
150+ val contentContainer = LinearLayout (context).apply {
151+ orientation = LinearLayout .VERTICAL
122152 setBackgroundColor(Color .WHITE )
123153 elevation = 12f
124154 setPadding(48 , 32 , 32 , 32 )
125155 }
126156
157+ val header = TextView (context).apply {
158+ text = headerTitle
159+ setTextColor(Color .BLACK )
160+ textSize = 16f
161+ }
162+
163+ val row = LinearLayout (context).apply {
164+ orientation = LinearLayout .HORIZONTAL
165+ }
166+
127167 val label = TextView (context).apply {
128168 text = " UserName"
129169 setTextColor(Color .BLACK )
@@ -135,17 +175,20 @@ class FloatingPopupView(context: Context) : FrameLayout(context) {
135175 background = null
136176 }
137177
138- content .addView(label)
139- content .addView(
178+ row .addView(label)
179+ row .addView(
140180 sendButton,
141181 LinearLayout .LayoutParams (
142182 ViewGroup .LayoutParams .WRAP_CONTENT ,
143183 ViewGroup .LayoutParams .WRAP_CONTENT
144184 ).apply { leftMargin = 24 }
145185 )
146186
187+ contentContainer.addView(header)
188+ contentContainer.addView(row)
189+
147190 addView(
148- content ,
191+ contentContainer ,
149192 FrameLayout .LayoutParams (
150193 ViewGroup .LayoutParams .WRAP_CONTENT ,
151194 ViewGroup .LayoutParams .WRAP_CONTENT ,
@@ -156,7 +199,7 @@ class FloatingPopupView(context: Context) : FrameLayout(context) {
156199 // Outside tap dismiss
157200 setOnClickListener { onDismissRequested?.invoke() }
158201 // Consume inner content clicks
159- content .setOnClickListener { }
202+ contentContainer .setOnClickListener { }
160203 sendButton.setOnClickListener {
161204 onSendClicked?.invoke()
162205 onDismissRequested?.invoke()
0 commit comments