Skip to content

Instantly share code, notes, and snippets.

@ongakuer
Created September 12, 2016 04:57
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ongakuer/6ceca8d5c8246466cc30902cd3166376 to your computer and use it in GitHub Desktop.
Save ongakuer/6ceca8d5c8246466cc30902cd3166376 to your computer and use it in GitHub Desktop.
Show PopupWindow in Immersive Sticky mode
public class MainActivity extends AppCompatActivity {
private PopupWindow mPopupWindow;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int windowWidth = 480;
int windowHeight = 120;
TextView popupView = (TextView) LayoutInflater.from(getApplicationContext())
.inflate(android.R.layout.simple_list_item_1, null);
popupView.setText("PopupWindow");
popupView.setGravity(Gravity.CENTER);
popupView.setBackgroundColor(Color.BLACK);
popupView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Toast.makeText(getApplicationContext(), "PopupWindow Click", Toast.LENGTH_SHORT)
.show();
}
});
mPopupWindow = new PopupWindow(popupView, windowWidth, windowHeight);
mPopupWindow.setFocusable(false);
mPopupWindow.setBackgroundDrawable(new ColorDrawable());
mPopupWindow.setOutsideTouchable(true);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
if (!mPopupWindow.isShowing()) {
showPopup(v);
} else {
mPopupWindow.dismiss();
}
}
});
}
@Override public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
fullScreenImmersive(getWindow());
}
}
public void showPopup(View anchor) {
mPopupWindow.setFocusable(false);
mPopupWindow.update();
PopupWindowCompat.showAsDropDown(mPopupWindow, anchor,
-mPopupWindow.getWidth() / 2 + anchor.getWidth() / 2,
-mPopupWindow.getHeight() - anchor.getHeight(), Gravity.CENTER);
fullScreenImmersive(mPopupWindow.getContentView());
mPopupWindow.setFocusable(true);
mPopupWindow.update();
}
public void fullScreenImmersive(Window window) {
if (window != null) {
fullScreenImmersive(window.getDecorView());
}
}
public void fullScreenImmersive(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN;
view.setSystemUiVisibility(uiOptions);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment