Advertisement
Guest User

Untitled

a guest
Dec 26th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. import android.graphics.drawable.BitmapDrawable;
  2. import android.graphics.drawable.Drawable;
  3.  
  4. import android.view.KeyEvent;
  5. import android.view.LayoutInflater;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.view.WindowManager;
  9. import android.view.View.OnTouchListener;
  10.  
  11. import android.widget.EditText;
  12. import android.widget.PopupWindow;
  13. import android.app.AlertDialog;
  14. import android.content.Context;
  15.  
  16. /**
  17.  * Custom popup window.
  18.  *
  19.  * @author Lorensius W. L. T <lorenz@londatiga.net>
  20.  *
  21.  */
  22. public class PopupWindowsNotification {
  23.     protected Context mContext;
  24.     protected PopupWindow mWindow;
  25.     protected View mRootView;
  26.     protected Drawable mBackground = null;
  27.     protected WindowManager mWindowManager;
  28.    
  29.     public String windowShowing = "";
  30.     public boolean isShowing = false;
  31.    
  32.    
  33.     /**
  34.      * Constructor.
  35.      *
  36.      * @param context Context
  37.      */
  38.     public PopupWindowsNotification(Context context) {
  39.         mContext    = context;
  40.         mWindow     = new PopupWindow(context);    
  41.         mWindow.setTouchInterceptor(new OnTouchListener() {
  42.             @Override
  43.             public boolean onTouch(View v, MotionEvent event) {
  44.                 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
  45.                     mWindow.dismiss();
  46.                     return false;
  47.                 }
  48.                
  49.                 return false;
  50.             }
  51.         });
  52.  
  53.         mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  54.     }
  55.    
  56.     /**
  57.      * On dismiss
  58.      */
  59.     protected void onDismiss() {   
  60.         isShowing = false;
  61.         windowShowing = "";
  62.     }
  63.    
  64.     /**
  65.      * On show
  66.      */
  67.     protected void onShow() {      
  68.     }
  69.  
  70.     /**
  71.      * On pre show
  72.      */
  73.     protected void preShow() {
  74.         preShow(false);
  75.     }
  76.     protected void preShow(Boolean focus) {
  77.         if (mRootView == null)
  78.             throw new IllegalStateException("setContentView was not called with a view to display.");
  79.    
  80.         onShow();
  81.  
  82.         if (mBackground == null)
  83.             mWindow.setBackgroundDrawable(new BitmapDrawable());
  84.         else
  85.             mWindow.setBackgroundDrawable(mBackground);
  86.  
  87.         mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
  88.         mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
  89.         mWindow.setTouchable(true);
  90.         mWindow.setFocusable(focus);
  91.         mWindow.setOutsideTouchable(true);
  92.         mWindow.setContentView(mRootView);
  93.     }
  94.  
  95.     /**
  96.      * Set background drawable.
  97.      *
  98.      * @param background Background drawable
  99.      */
  100.     public void setBackgroundDrawable(Drawable background) {
  101.         mBackground = background;
  102.     }
  103.  
  104.     /**
  105.      * Set content view.
  106.      *
  107.      * @param root Root view
  108.      */
  109.     public void setContentView(View root) {
  110.         mRootView = root;
  111.        
  112.         mWindow.setContentView(root);
  113.     }
  114.  
  115.     /**
  116.      * Set content view.
  117.      *
  118.      * @param layoutResID Resource id
  119.      */
  120.     public void setContentView(int layoutResID) {
  121.         LayoutInflater inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  122.        
  123.         setContentView(inflator.inflate(layoutResID, null));
  124.     }
  125.  
  126.     /**
  127.      * Set listener on window dismissed.
  128.      *
  129.      * @param listener
  130.      */
  131.     public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
  132.         mWindow.setOnDismissListener(listener);  
  133.     }
  134.  
  135.     /**
  136.      * Dismiss the popup window.
  137.      */
  138.     public void dismiss() {
  139.         isShowing = false;
  140.         windowShowing = "";
  141.         mWindow.dismiss();
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement