Open dialog exactly above clicked item like context menu
Asked Answered
L

2

5

I've created custom dialog and want to bind it to my list item. I want this dialog to behave like context menu when long click on list item.

In other words, I don't want this dialog to appear in the center of the screen, but to appear near the item in the list.

I've spent a lot of time searching a way, but unfortunately no result. Is there some good solution?

Lissotrichous answered 13/7, 2015 at 5:8 Comment(3)
Use 'PopupWindow' that will help you to show window on your view.Sulphone
thanks , i will search for itLissotrichous
rajeshandroiddeveloper.blogspot.in/2013/07/…Ignatzia
T
5
   btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        // infalte menu

        PopupMenu popup = new PopupMenu(activity, v);
        popup.getMenuInflater().inflate(R.menu.clipboard_popup,
                popup.getMenu());
        popup.show();
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                case R.id.install:

                    // Or Some other code you want to put
                    // here.. This is just an example.
                    Toast.makeText(
                            activity,
                            " Install Clicked at position " + " : "
                                    + position, Toast.LENGTH_LONG)
                            .show();

                    break;
                case R.id.addtowishlist:

                    Toast.makeText(
                            activity,
                            "Add to Wish List Clicked at position "
                                    + " : " + position,
                            Toast.LENGTH_LONG).show();

                    break;

                default:
                    break;
                }

                return true;
            }
        });

    }
});

Updated answer with custom layout

 PopupWindow popupwindow_obj = popupDisplay();
 popupwindow_obj.showAsDropDown(clickbtn, -40, 18); 

 // where u want show on 
 //view click event popupwindow.showAsDropDown(view, x, y);

    public PopupWindow popupDisplay() 
    { 

        final PopupWindow popupWindow = new PopupWindow(this);

          // inflate your layout or dynamically add view
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

            View view = inflater.inflate(R.layout.mylayout, null);

            Button item = (LinearLayout) view.findViewById(R.id.button1);

            popupWindow.setFocusable(true);
            popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setContentView(view);

            return popupWindow;
        }

mylayout.xml

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Window test" />
    </LinearLayout>
Tolmann answered 13/7, 2015 at 5:19 Comment(4)
I dont want to use menu ! i have a custom layout that contain 3 button and in horizonal orientationLissotrichous
my problem is here : popup.getMenuInflater().inflate(R.menu.clipboard_popup, popup.getMenu());Lissotrichous
its not a menu ! its a layoutLissotrichous
Great , its work now :) i need to change it abit for a better position , thanks :)Lissotrichous
O
2

If you know the position where you want your dialog to appear you can do like this (Note: X : left --> right and Y : top --> bottom)

 AlertDialog dialog = builder.create();
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
     WindowManager.LayoutParams abc= dialog.getWindow().getAttributes();

 abc.gravity = Gravity.TOP | Gravity.LEFT;
 abc.x = 100;   //x position
 abc.y = 100;   //y position

 dialog.show();
Orang answered 13/7, 2015 at 5:16 Comment(2)
I have no idea about how i can calculate position of clicked item in list view :/Lissotrichous
You have the ID of listview . Check for its arguments. It's the 4th argument of the onItemClick method.Orang

© 2022 - 2024 — McMap. All rights reserved.