How Do I Position Custom Dialog at specific Coordinate?

16,048

Override onTouch() of your view

AlertDialog dialog;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            showDialog();  // display dialog
            break;
        case MotionEvent.ACTION_MOVE:
            if(dialog!=null)
              dialog.dismiss(); 
             // do something
            break;
        case MotionEvent.ACTION_UP:
            // do somethig
            break;
    }
    return true;
    } 
     public void showDialog()
      {

             AlertDialog.Builder builder = new AlertDialog.Builder(FingerPaintActivity.this);
             dialog = builder.create();
             dialog.setTitle("my dialog");
             dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
             WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
         wmlp.gravity = Gravity.TOP | Gravity.LEFT;
             wmlp.x = 100;   //x position
             wmlp.y = 100;   //y position
         dialog.show();
      }

Even to draw user touches the screen then also dialog is displayed. so dismiss dialog in on move.

Share:
16,048
B. Money
Author by

B. Money

Just a noob, workin to become a vet.

Updated on June 12, 2022

Comments

  • B. Money
    B. Money about 2 years

    I'm a noob to android development and I am trying to figure out how to display the NewQuickAction3D popup dialog at a specific coordinate in a view. I am integrating the popup with this tutorial. Essentially, I want to use the popup dialog to display data where the users touch instead painting on the canvas using "infoview" Currently, the popup displays at the top&center of the view that I anchor it to. How can i make it display a particular coordinate? Any help is greatly appreciated.

    MY CODE

    public void updateMsg(String t_info, float t_x, float t_y, int t_c){
         infoView.updateInfo(t_info, t_x, t_y, t_c); //Infoview paints to on a specific coordinate
         quickAction.show(infoView); //How do I use the t_x & t_y coordinates here instead of just anchoring infoview
    

    EDIT

    public void updateMsg(String t_info, float t_x, float t_y, int t_c){
         infoView.updateInfo(t_info, t_x, t_y, t_c);
         WindowManager.LayoutParams wmlp = quickAction.getWindow().getAttributes(); //Error here getting window attributes
         wmlp.gravity = Gravity.TOP | Gravity.LEFT;
             wmlp.x = 100;   //x position
             wmlp.y = 100;   //y position
         quickAction.show(infoView);
    }
    
  • B. Money
    B. Money about 11 years
    Thanks for responding. I'm having some trouble with your answer. NewQuickAction is a custom dialog so it won't allow me to get the window attributes. any thoughts?
  • Raghunandan
    Raghunandan about 11 years
    make your custom view inner class of your activity class