Android position item on screen

I have a list, and when the user clicks the button, I want to collect the coordinates of the button and place the editor, which I puff over it on top of the screen. When the user clicks on another place on the screen, the edittext will disappear and he will launch a method that uses the data entered by the user in the field. How will I do something like this? I would like something like QuickActions, but not so intrusive. Can someone point me in the direction, at least, how to get the coordinates of the buttons?

+5
source share
2 answers

Well, that’s how I was able to achieve what I’m looking for. Is it possible to dynamically place PopupWindow without the need for confusion with adjustment fields, 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;
        }
    });
}

pWidth and pHeight are the size of the selected PopupWindow, and vHeight is the height of the main parent view, which I collected from the onCreate context. Keep in mind that this is not polished code. I still need to add a few things, such as animation and exit, as well as a pretty little arrow or something to show what the window is connected to. SetBackgroundDrawable is very important, and if you are not using it, you cannot click it to close it.

Right now, this is weird. I have to click twice outside the window to close the window. The first click seems to highlight my text box, and the second click actually closes it. Does anyone know why this might happen?

+2

, View. getLeft() ( getRight, getTop getBottom) View parent. getLocationOnScreen , , .

+1

All Articles