Skip to content

Commit 801ae96

Browse files
committed
Attached dialog sample
1 parent ac99967 commit 801ae96

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

e2e/android/app/src/main/java/com/example/androidobservability/masking/XMLMaskingActivity.kt

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

e2e/android/app/src/main/res/layout/activity_masking_bench.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@
100100
android:layout_height="wrap_content"
101101
android:layout_weight="1"
102102
android:text="Floating Popup" />
103+
104+
<Space
105+
android:layout_width="12dp"
106+
android:layout_height="1dp" />
107+
108+
<Button
109+
android:id="@+id/button_attached_dialog"
110+
android:layout_width="0dp"
111+
android:layout_height="wrap_content"
112+
android:layout_weight="1"
113+
android:text="Attached Dialog" />
103114
</LinearLayout>
104115

105116
<TextView

0 commit comments

Comments
 (0)