@Override
 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
   super.onLayout(changed, left, top, right, bottom);
   if (popupWindow != null && popupWindow.isShowing()) {
     if (showFromBottom) {
       popupWindow.update(
           this,
           -popupLayout.getMeasuredWidth() + getMeasuredWidth() + AndroidUtilities.dp(14),
           getOffsetY(),
           -1,
           -1);
     } else {
       popupWindow.update(
           this,
           parentMenu.parentActionBar.getMeasuredWidth()
               - popupLayout.getMeasuredWidth()
               - getLeft()
               - parentMenu.getLeft(),
           getOffsetY(),
           -1,
           -1);
     }
   }
 }
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
     if (hasSubMenu()
         && (popupWindow == null || popupWindow != null && !popupWindow.isShowing())) {
       showMenuRunnable =
           new Runnable() {
             @Override
             public void run() {
               if (getParent() != null) {
                 getParent().requestDisallowInterceptTouchEvent(true);
               }
               toggleSubMenu();
             }
           };
       AndroidUtilities.runOnUIThread(showMenuRunnable, 200);
     }
   } else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
     if (hasSubMenu()
         && (popupWindow == null || popupWindow != null && !popupWindow.isShowing())) {
       if (event.getY() > getHeight()) {
         if (getParent() != null) {
           getParent().requestDisallowInterceptTouchEvent(true);
         }
         toggleSubMenu();
         return true;
       }
     } else if (popupWindow != null && popupWindow.isShowing()) {
       getLocationOnScreen(location);
       float x = event.getX() + location[0];
       float y = event.getY() + location[1];
       popupLayout.getLocationOnScreen(location);
       x -= location[0];
       y -= location[1];
       selectedMenuView = null;
       for (int a = 0; a < popupLayout.getChildCount(); a++) {
         View child = popupLayout.getChildAt(a);
         child.getHitRect(rect);
         if ((Integer) child.getTag() < 100) {
           if (!rect.contains((int) x, (int) y)) {
             child.setPressed(false);
             child.setSelected(false);
             if (Build.VERSION.SDK_INT >= 21) {
               child.getBackground().setVisible(false, false);
             }
           } else {
             child.setPressed(true);
             child.setSelected(true);
             if (Build.VERSION.SDK_INT >= 21) {
               child.getBackground().setVisible(true, false);
               child.drawableHotspotChanged(x, y - child.getTop());
             }
             selectedMenuView = child;
           }
         }
       }
     }
   } else if (popupWindow != null
       && popupWindow.isShowing()
       && event.getActionMasked() == MotionEvent.ACTION_UP) {
     if (selectedMenuView != null) {
       selectedMenuView.setSelected(false);
       parentMenu.onItemClick((Integer) selectedMenuView.getTag());
     }
     popupWindow.dismiss();
   } else {
     if (selectedMenuView != null) {
       selectedMenuView.setSelected(false);
       selectedMenuView = null;
     }
   }
   return super.onTouchEvent(event);
 }
 public void closeSubMenu() {
   if (popupWindow != null && popupWindow.isShowing()) {
     popupWindow.dismiss();
   }
 }
 public void toggleSubMenu() {
   if (popupLayout == null) {
     return;
   }
   if (showMenuRunnable != null) {
     AndroidUtilities.cancelRunOnUIThread(showMenuRunnable);
     showMenuRunnable = null;
   }
   if (popupWindow != null && popupWindow.isShowing()) {
     popupWindow.dismiss();
     return;
   }
   if (popupWindow == null) {
     popupWindow =
         new ActionBarPopupWindow(
             popupLayout,
             FrameLayout.LayoutParams.WRAP_CONTENT,
             FrameLayout.LayoutParams.WRAP_CONTENT);
     // popupWindow.setBackgroundDrawable(new BitmapDrawable());
     popupWindow.setAnimationStyle(R.style.PopupAnimation);
     popupWindow.setOutsideTouchable(true);
     popupWindow.setClippingEnabled(true);
     popupWindow.setInputMethodMode(ActionBarPopupWindow.INPUT_METHOD_NOT_NEEDED);
     popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
     popupLayout.measure(
         MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), MeasureSpec.AT_MOST),
         MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(1000), MeasureSpec.AT_MOST));
     popupWindow.getContentView().setFocusableInTouchMode(true);
     popupWindow
         .getContentView()
         .setOnKeyListener(
             new OnKeyListener() {
               @Override
               public boolean onKey(View v, int keyCode, KeyEvent event) {
                 if (keyCode == KeyEvent.KEYCODE_MENU
                     && event.getRepeatCount() == 0
                     && event.getAction() == KeyEvent.ACTION_UP
                     && popupWindow != null
                     && popupWindow.isShowing()) {
                   popupWindow.dismiss();
                   return true;
                 }
                 return false;
               }
             });
   }
   popupWindow.setFocusable(true);
   if (popupLayout.getMeasuredWidth() == 0) {
     if (showFromBottom) {
       popupWindow.showAsDropDown(
           this,
           -popupLayout.getMeasuredWidth() + getMeasuredWidth() + AndroidUtilities.dp(14),
           getOffsetY());
       popupWindow.update(
           this,
           -popupLayout.getMeasuredWidth() + getMeasuredWidth() + AndroidUtilities.dp(14),
           getOffsetY(),
           -1,
           -1);
     } else {
       popupWindow.showAsDropDown(
           this,
           parentMenu.parentActionBar.getMeasuredWidth()
               - popupLayout.getMeasuredWidth()
               - getLeft()
               - parentMenu.getLeft(),
           getOffsetY());
       popupWindow.update(
           this,
           parentMenu.parentActionBar.getMeasuredWidth()
               - popupLayout.getMeasuredWidth()
               - getLeft()
               - parentMenu.getLeft(),
           getOffsetY(),
           -1,
           -1);
     }
   } else {
     if (showFromBottom) {
       popupWindow.showAsDropDown(
           this,
           -popupLayout.getMeasuredWidth() + getMeasuredWidth() + AndroidUtilities.dp(14),
           getOffsetY());
     } else {
       popupWindow.showAsDropDown(
           this,
           parentMenu.parentActionBar.getMeasuredWidth()
               - popupLayout.getMeasuredWidth()
               - getLeft()
               - parentMenu.getLeft(),
           getOffsetY());
     }
   }
 }