Created
April 9, 2022 09:31
-
-
Save prongbang/c9fe51596737e75befd926a70ef6265f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.SuppressLint | |
import android.app.Activity | |
import android.graphics.Color | |
import android.view.Gravity | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.PopupWindow | |
import android.widget.RelativeLayout | |
import android.os.Build | |
class CustomPopupWindow( | |
private val activity: Activity | |
) { | |
fun create(view: View? = null) { | |
try { | |
val customView = view | |
?: RelativeLayout(activity).apply { | |
layoutParams = RelativeLayout.LayoutParams( | |
RelativeLayout.LayoutParams.MATCH_PARENT, | |
RelativeLayout.LayoutParams.MATCH_PARENT | |
) | |
setBackgroundColor(Color.WHITE) | |
} | |
popupWindow = PopupWindow( | |
customView, | |
ViewGroup.LayoutParams.MATCH_PARENT, | |
ViewGroup.LayoutParams.MATCH_PARENT, | |
true | |
) | |
} catch (e: Exception) { | |
} | |
} | |
fun dismiss() { | |
try { | |
popupWindow?.dismiss() | |
} catch (e: Exception) { | |
} | |
} | |
fun show() { | |
showAtLocation(rootView(), Gravity.CENTER, 0, 0) | |
} | |
fun showAtLocation(parent: View, gravity: Int, x: Int, y: Int) { | |
try { | |
parent.post { | |
popupWindow?.showAtLocation(parent, gravity, x, y) | |
} | |
} catch (e: Exception) { | |
} | |
} | |
private fun rootView() = activity.findViewById<ViewGroup>(android.R.id.content) | |
fun fullScreenImmersive() { | |
val view = popupWindow?.contentView | |
if (view != null) { | |
fullScreenImmersive(view) | |
popupWindow?.update() | |
} | |
} | |
@SuppressLint("ObsoleteSdkInt") | |
private fun fullScreenImmersive(view: View) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
val uiOptions = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE | |
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | |
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | |
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | |
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | |
or View.SYSTEM_UI_FLAG_FULLSCREEN) | |
view.systemUiVisibility = uiOptions | |
} | |
} | |
companion object { | |
private var popupWindow: PopupWindow? = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment