4

I want to show a small popup at the point where user clicks on the graph.I have read the answer given for the question

Showing popup on clicking a point in graph AChartEngine

it suggests toast as the answer but I want user to select between two options in the popup.I tried using of alertdialog and even created a custom dialog but they cover the whole screen area whereas I want just a small popup at the place where user clicks on the graph.

Here is my onotuch listener where I get the x,y coordinate

     mChart.setOnTouchListener(new OnTouchListener() {
                if(event.getAction() == MotionEvent.ACTION_UP)
                              {                     
                    int xd= (int) event.getX();
                    int yd = (int) event.getY();
                                       }

The code for repositioning the dialog

    AlertDialog alertDialog = builder.create();
    alertDialog.show();
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
    lp.x= xd;
    lp.y= yd;
    alertDialog.getWindow().setAttributes(lp);                      
Community
  • 1
  • 1
Prerak
  • 109
  • 10
  • how about popping up two small images referring to ur two options – ik024 Mar 14 '14 at 10:24
  • Thanks , but no I don't think displaying images will work I want specific options to be displayed – Prerak Mar 14 '14 at 10:56
  • since u r using custom dialog u shud be able to do padding as for normal text. refer this post, hopefully it will help http://stackoverflow.com/questions/1967085/android-how-to-resize-dialog – ik024 Mar 14 '14 at 11:04

1 Answers1

4

Here is the code to resize ur dialog box

dialog.show();

Display display =((WindowManager)getSystemService(context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();

Log.v("width", width+"");
dialog.getWindow().setLayout((6*width)/7,(4*height)/5);

UPDATED

The MotionEvent class has methods getX() to get X touch point corresponding to that view and getRawX() to get X touch point corresponding to screen.

So to get X touch point corresponding to parent you can get that by a simple calculation:

 view.getLeft() + motionEvent.getX()

The getLeft() returns Left position of this view relative to its parent

ik024
  • 3,566
  • 7
  • 38
  • 61
  • Thanks I was able to resize it. I even tried repositioning it using the screen coordinates which I get from on touch event but the dialog is shown quite far from where I click the graph. I will add th code in my question. – Prerak Mar 14 '14 at 11:21
  • probably because the dialog box is drawn from top left corner wer u touch. Wer as the center of the dialog shud be present wer u touch. try dividing the width n height of ur dialog box by 2, the same trick we use to place the image at screen wer we touch – ik024 Mar 14 '14 at 11:29
  • I want the top left corner to be present where I touch.in the I tried dividing by two now in some places dialog appears to the right of the place where I click and sometimes in the middle of the position where I click. I will add the images – Prerak Mar 14 '14 at 11:50
  • lp.x = xd - (alertDialog.getWidth()/2); lp.y = yd - (alertDialog.getHeight()/2); - Hopefully dis shud work – ik024 Mar 14 '14 at 11:51
  • alertDialog.getWidth() and alertDialog.getHeight() were giving error so I used 100 in their place because I have set the width and height of alert dialog explicitly as 200 but still no success. Also I want to ask does event.getX() gives x coordinate with respect to the whole screen or with respect to the view in which the event happens because this might be the reason behind the improper positioning of the dialog box – Prerak Mar 14 '14 at 12:10
  • MotionEvent class has methods getX() and getRawX(). getX() gives value based on that view and getRawX() gives value corresponding to the screen – ik024 Mar 14 '14 at 12:18
  • I tried using getRawX() but still no effect now the dialog is even more farther than before – Prerak Mar 14 '14 at 12:28
  • i dnt think i will be able to give exact sol for this. Trail and error might help u best in this situation. One more thing make use of logcat it comes in handy in situation like this – ik024 Mar 14 '14 at 12:38
  • I read somewhere that alert dialog takes center of the screen as (0,0) so I set the gravity as lp.gravity = Gravity.TOP | Gravity.LEFT; and now the dialog is coming almost at the point still a bit away from where i click but I think I can manage with that.Thanks for your help appreciate it – Prerak Mar 14 '14 at 13:19