Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Position Element on Screen

Tags:

android

window

I have a listview and when the user presses a button I want to gather the coordinates of the button and place an edittext that I inflate right over top of it on the screen. When the user clicks anywhere else on the screen the edittext will disappear and it will fire a method that uses the data the user entered into the box. How would I go about doing something like this? I would like something similar to QuickActions, but not quite as intrusive. Could someone point me in the direction of at least how to go about getting the button coordinates?

like image 690
ryandlf Avatar asked Oct 24 '22 02:10

ryandlf


1 Answers

Ok, so here is how i've been able to achieve what i'm looking to do. Is it possible to dynamically place a PopupWindow without having to mess with adjusting margins etc.

public void showPopup(View view, View parentView, final int getId, String getLbs){
    int pWidth = 100;
    int pHeight = 80;
    int vHeight = parentView.getHeight(); //The listview rows height.
    int[] location = new int[2];

    view.getLocationOnScreen(location);
    final View pView = inflater.inflate(R.layout.list_popup, null, false);
    final PopupWindow pw = new PopupWindow(pView, pWidth, pHeight, false);
    pw.setTouchable(true);
    pw.setFocusable(true);
    pw.setOutsideTouchable(true);
    pw.setBackgroundDrawable(new BitmapDrawable());
    pw.showAtLocation(view, Gravity.NO_GRAVITY, location[0]-(pWidth/4), location[1]+vHeight);

    final EditText input = (EditText)pView.findViewById(R.id.Input);
    input.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            Log.i("Focus", "Focus Changed");
            if (hasFocus) {
                //Shows the keyboard when the EditText is focused.
                InputMethodManager inputMgr = (InputMethodManager)RecipeGrainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                inputMgr.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
            }

        }
    });
    input.setText("");
    input.requestFocus();
    Log.i("Input Has Focus", "" + input.hasFocus());
    pw.setOnDismissListener(new OnDismissListener(){

        @Override
        public void onDismiss() {
            changeWeight(getId, Double.parseDouble(input.getText().toString()));
            Log.i("View Dismiss", "View Dismissed");
        }

    });

    pw.setTouchInterceptor(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                Log.i("Background", "Back Touched");
                pw.dismiss();
                return true;
            }
            return false;
        }
    });
}

The pWidth and pHeight are the size of the PopupWindow I chose and the vHeight is the height of the main parent view that I collected from the onCreate context. Keep in mind this is not polished code. I still need to add a few things like animate in and out as well as a nice little arrow or something to show what the window is being associated with. The setBackgroundDrawable is very important and if you don't use it you won't be able to click outside the box to close it.

Right now, its weird. I have to click twice outside the box to close the window. The first click just seems to highlight my textbox and the second click actually closes it out. Anyone have any idea why that might be happening?

like image 57
ryandlf Avatar answered Oct 27 '22 09:10

ryandlf