Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void popupWindowListPicker(String options[], View rootView, final HasUpdate update){
- final Context con = rootView.getContext();
- final PopupWindow popupWindow = new PopupWindow(con);
- final Animation.AnimationListener animationListener = new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- popupWindow.dismiss();
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
- };
- int padding = Misc.dpToPx(7, con);
- //create the layout and views that will go into the popupWindow
- final LinearLayout popupLayout = new LinearLayout(con);
- popupLayout.setOrientation(LinearLayout.VERTICAL);
- popupLayout.setClipChildren(false);
- popupLayout.setOnClickListener(null);
- popupLayout.setBackgroundDrawable(con.getResources().getDrawable(R.drawable.border));
- popupLayout.setGravity(Gravity.CENTER);
- //this will be the dimmed background
- final LinearLayout mainLayout = new LinearLayout(con);
- mainLayout.setGravity(Gravity.CENTER);
- mainLayout.setBackgroundColor(con.getResources().getColor(R.color.dim));
- mainLayout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Animation anim = AnimationUtils.loadAnimation(con, R.anim.scale_out);
- anim.setAnimationListener(animationListener);
- popupLayout.startAnimation(anim);
- anim = AnimationUtils.loadAnimation(con,R.anim.fade_out);
- mainLayout.startAnimation(anim);
- }
- });
- for (int i = 0; i < options.length ; i++) {
- //the text for the list item
- final TextView tv = new TextView(con);
- tv.setText(options[i]);
- tv.setTextAppearance(con, android.R.style.TextAppearance_Medium);
- tv.setTextColor(con.getResources().getColor(R.color.FontDark));
- tv.setGravity(Gravity.CENTER);
- tv.setClickable(false);
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- lp.gravity = Gravity.CENTER;
- LinearLayout layout = new LinearLayout(con);
- layout.setGravity(Gravity.CENTER);
- layout.setPadding(0,padding,0,padding);
- layout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- update.update(tv.getText().toString());
- Animation anim = AnimationUtils.loadAnimation(con, R.anim.scale_out);
- anim.setAnimationListener(animationListener);
- popupLayout.startAnimation(anim);
- anim = AnimationUtils.loadAnimation(con,R.anim.fade_out);
- mainLayout.startAnimation(anim);
- }
- });
- //add the row to the popup window
- layout.addView(tv);
- popupLayout.addView(layout, lp);
- //add separator if we aren't done yet.
- if(i < options.length - 1){
- View separator = new View(con);
- separator.setBackgroundColor(con.getResources().getColor( R.color.colorSeparator));
- LinearLayout.LayoutParams lps = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,Misc.dpToPx(1,con));
- separator.setPadding(padding,0,padding,0);
- popupLayout.addView(separator,lps);
- }
- }
- popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
- @Override
- public void onDismiss() {
- //nothing happens here.
- }
- });
- Point size = Misc.getScreenPixels(con);
- mainLayout.setMinimumWidth(size.x + 20);
- mainLayout.setMinimumHeight(size.y + 20);
- //todo: find an alternative to this hardcoded width, if needed.
- mainLayout.addView(popupLayout, dpToPx(300, con), ViewGroup.LayoutParams.WRAP_CONTENT);
- //set the content of the popup window
- popupWindow.setContentView(mainLayout);
- //this allows the background to look as expected
- popupWindow.setBackgroundDrawable(null);
- //show the goddamn popup
- popupWindow.showAtLocation(rootView, Gravity.CENTER, size.x, size.y);
- //start animation of the various components inside the popup
- Animation anim = AnimationUtils.loadAnimation(con,R.anim.scale_in);
- popupLayout.startAnimation(anim);
- anim = AnimationUtils.loadAnimation(con,R.anim.fade_in);
- mainLayout.startAnimation(anim);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement