Advertisement
maxizrin

Popup window

Jan 25th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. public static void popupWindowListPicker(String options[], View rootView, final HasUpdate update){
  2.         final Context con = rootView.getContext();
  3.         final PopupWindow popupWindow = new PopupWindow(con);
  4.         final Animation.AnimationListener animationListener = new Animation.AnimationListener() {
  5.             @Override
  6.             public void onAnimationStart(Animation animation) {
  7.  
  8.             }
  9.  
  10.             @Override
  11.             public void onAnimationEnd(Animation animation) {
  12.                 popupWindow.dismiss();
  13.             }
  14.  
  15.             @Override
  16.             public void onAnimationRepeat(Animation animation) {
  17.  
  18.             }
  19.         };
  20.  
  21.         int padding = Misc.dpToPx(7, con);
  22.  
  23.         //create the layout and views that will go into the popupWindow
  24.         final LinearLayout popupLayout = new LinearLayout(con);
  25.         popupLayout.setOrientation(LinearLayout.VERTICAL);
  26.         popupLayout.setClipChildren(false);
  27.         popupLayout.setOnClickListener(null);
  28.         popupLayout.setBackgroundDrawable(con.getResources().getDrawable(R.drawable.border));
  29.         popupLayout.setGravity(Gravity.CENTER);
  30.  
  31.         //this will be the dimmed background
  32.         final LinearLayout mainLayout = new LinearLayout(con);
  33.         mainLayout.setGravity(Gravity.CENTER);
  34.         mainLayout.setBackgroundColor(con.getResources().getColor(R.color.dim));
  35.         mainLayout.setOnClickListener(new View.OnClickListener() {
  36.             @Override
  37.             public void onClick(View v) {
  38.                 Animation anim = AnimationUtils.loadAnimation(con, R.anim.scale_out);
  39.                 anim.setAnimationListener(animationListener);
  40.                 popupLayout.startAnimation(anim);
  41.                 anim = AnimationUtils.loadAnimation(con,R.anim.fade_out);
  42.                 mainLayout.startAnimation(anim);
  43.             }
  44.         });
  45.  
  46.         for (int i = 0; i < options.length ; i++) {
  47.             //the text for the list item
  48.             final TextView tv = new TextView(con);
  49.  
  50.             tv.setText(options[i]);
  51.             tv.setTextAppearance(con, android.R.style.TextAppearance_Medium);
  52.             tv.setTextColor(con.getResources().getColor(R.color.FontDark));
  53.             tv.setGravity(Gravity.CENTER);
  54.             tv.setClickable(false);
  55.  
  56.             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  57.  
  58.             lp.gravity = Gravity.CENTER;
  59.  
  60.             LinearLayout layout = new LinearLayout(con);
  61.             layout.setGravity(Gravity.CENTER);
  62.             layout.setPadding(0,padding,0,padding);
  63.             layout.setOnClickListener(new View.OnClickListener() {
  64.                 @Override
  65.                 public void onClick(View v) {
  66.                     update.update(tv.getText().toString());
  67.  
  68.                     Animation anim = AnimationUtils.loadAnimation(con, R.anim.scale_out);
  69.                     anim.setAnimationListener(animationListener);
  70.                     popupLayout.startAnimation(anim);
  71.                     anim = AnimationUtils.loadAnimation(con,R.anim.fade_out);
  72.                     mainLayout.startAnimation(anim);
  73.                 }
  74.             });
  75.  
  76.             //add the row to the popup window
  77.             layout.addView(tv);
  78.             popupLayout.addView(layout, lp);
  79.  
  80.             //add separator if we aren't done yet.
  81.             if(i < options.length - 1){
  82.                 View separator = new View(con);
  83.                 separator.setBackgroundColor(con.getResources().getColor( R.color.colorSeparator));
  84.                 LinearLayout.LayoutParams lps = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,Misc.dpToPx(1,con));
  85.                 separator.setPadding(padding,0,padding,0);
  86.                 popupLayout.addView(separator,lps);
  87.             }
  88.         }
  89.  
  90.         popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
  91.             @Override
  92.             public void onDismiss() {
  93.                 //nothing happens here.
  94.             }
  95.         });
  96.  
  97.         Point size = Misc.getScreenPixels(con);
  98.         mainLayout.setMinimumWidth(size.x + 20);
  99.         mainLayout.setMinimumHeight(size.y + 20);
  100.         //todo: find an alternative to this hardcoded width, if needed.
  101.         mainLayout.addView(popupLayout, dpToPx(300, con), ViewGroup.LayoutParams.WRAP_CONTENT);
  102.  
  103.         //set the content of the popup window
  104.         popupWindow.setContentView(mainLayout);
  105.  
  106.         //this allows the background to look as expected
  107.         popupWindow.setBackgroundDrawable(null);
  108.  
  109.         //show the goddamn popup
  110.         popupWindow.showAtLocation(rootView, Gravity.CENTER, size.x, size.y);
  111.  
  112.         //start animation of the various components inside the popup
  113.         Animation anim = AnimationUtils.loadAnimation(con,R.anim.scale_in);
  114.         popupLayout.startAnimation(anim);
  115.         anim = AnimationUtils.loadAnimation(con,R.anim.fade_in);
  116.         mainLayout.startAnimation(anim);
  117.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement