0

I have a view class that draws different colored circles onto the canvas.

I want to display a popup window near the circle when the circle is clicked to display the color of the circle. Right now I am facing problem trying to create the popup window.

Most of the tutorials does the popup window on activity class thus i could not find a example to reference to.

My code:

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
                    x = event.getX();
            y = event.getY();

                   // Open popup window
                       for(int i =0; i < circles.size(); i++){
            if (buttonClick == true && circles.get(i).contains(x, y)) {

                                   View view = inflate(getContext(),R.layout.popup_layout, null); 
                PopupWindow popup = new PopupWindow(view);
    popup.showAtLocation(MainActivity.returnView(), Gravity.LEFT, (int)x, (int)y);

                        }
                           } 
             break;
}

Popup window xml file

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@android:color/background_light">
 <LinearLayout 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:layout_margin="1dp"
     android:background="@android:color/WHITE">
     >
     <LinearLayout 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical" 
      android:layout_margin="20dp">
      <TextView
           android:id="@+id/ColorTV"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Insert Text here" />
      <Button
          android:id="@+id/dismissbtn"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Dismiss" />
      </LinearLayout>
 </LinearLayout>
</LinearLayout>
frogatto
  • 28,539
  • 11
  • 83
  • 129
user3306996
  • 421
  • 2
  • 4
  • 15

1 Answers1

0

You can use PopupWindow. Use PopupWindow#showAtLocation (View parent, int gravity, int x, int y) method to put your popup where you want it, using your view itself as the parent.

So, inside your View's onTouchEvent, you could have:

View view = inflate(R.layout.my_popup_view, null); // however you'll inflate your view
PopupWindow popup = new PopupWindow(view, 300, 300, true);
popup.showAtLocation(this, Gravity.LEFT, x, y);

or something similar.

Makario
  • 2,097
  • 21
  • 19
  • the R.layout.my_popup_view refers to layout of my popup window right? or my view layout? – user3306996 Mar 20 '14 at 16:55
  • Thank you for your reply. I have tried out but it doesn't work:( i have included the xml file of my popup window above and your suggestion. adding on, i dont see any errors in my program when the codes above is running. – user3306996 Mar 20 '14 at 19:24
  • Try adding a width and height (and focusable) parameter to the PopupWindow's constructor. I updated the answer to reflect this. Also, try using getRawX() and getRawY() instead of getX() and getY(). – Makario Mar 20 '14 at 20:41