Android Open Source - MyNote Popup Helper






From Project

Back to project page MyNote.

License

The source code is released under:

MIT License

If you think the Android project MyNote listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.app.mynote.util;
/*from   w w w  .  jav  a 2  s  .co m*/
import com.app.mynote.R;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

public class PopupHelper 
{
  public static PopupWindow newBasicPopupWindow(Context context) 
  {
    final PopupWindow window = new PopupWindow(context);
    
    // when a touch even happens outside of the window
    // make the window go away
    window.setTouchInterceptor(new OnTouchListener() 
    {
      @Override
      public boolean onTouch(View v, MotionEvent event) 
      {
        if(event.getAction() == MotionEvent.ACTION_OUTSIDE) 
        {
          window.dismiss();
          return true;
        }
        return false;
      }
    });
    
    window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    window.setTouchable(true);
    window.setFocusable(true);
    window.setOutsideTouchable(true);
    
    window.setBackgroundDrawable(new BitmapDrawable());    
    
    return window;
  }
  
  /**
   * Displays like a QuickAction from the anchor view.
   * 
   * @param xOffset
   *            offset in the X direction
   * @param yOffset
   *            offset in the Y direction
   */
  public static void showLikeQuickAction(PopupWindow window, View root, View anchor, WindowManager windowManager, int xOffset, int yOffset)
  {
    window.setAnimationStyle(R.style.Animations_GrowFromBottomRight);

    int[] location = new int[2];
    anchor.getLocationOnScreen(location);

    Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1] + anchor.getHeight());
    
    root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
    int rootWidth = root.getMeasuredWidth();
    int rootHeight = root.getMeasuredHeight();
    
    int screenWidth = windowManager.getDefaultDisplay().getWidth();
    int screenHeight = windowManager.getDefaultDisplay().getHeight();

    int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
    int yPos = anchorRect.top - rootHeight + yOffset;

    window.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
  }  
}




Java Source Code List

com.app.mynote.MainActivity.java
com.app.mynote.adapter.ListViewAdapter.java
com.app.mynote.db.DatabaseHandler.java
com.app.mynote.util.Constants.java
com.app.mynote.util.MyNote.java
com.app.mynote.util.PopupHelper.java