0

I am trying to make share button when clicked a Popupmenu appears with three horizontal choices facebook, twitter and google+.

I keep searching for a while but I got nothing until now.

Is it possible to create horizontal or even grid PopupMenu? Is it possible to use RecyclerView in PopupMenu?

4 Answers 4

0

Instead of a PopupMenu, would you be able to use a DialogFragment with a custom layout?

1
  • A PopupMenu is designed to anchor a floating view (typically for selecting items) to a specific point in the current view, while a DialogFragment is meant to be a freestanding dialog view with its own context and root. You would be exponentially increasing the steps to take a vertical layout and make it horizontal. Commented Mar 10, 2020 at 13:22
0

In this case, you can use PopupWindow. You can use ListView to display items in each line. Example code to show a PopupWindow:

 public PopupWindow popupWindowsort() {

    // initialize a pop up window type
    popupWindow = new PopupWindow(this);

    ArrayList<String> sortList = new ArrayList<String>();
    sortList.add("Google+");
    sortList.add("Facebook");
    sortList.add("Twitter");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, sortList);
    // the drop down list is a list view
    ListView listViewSort = new ListView(this);

    // set our adapter and pass our pop up window contents
    listViewSort.setAdapter(adapter);

    // set on item selected
    listViewSort.setOnItemClickListener(onItemClickListener());

    // some other visual settings for popup window
    popupWindow.setFocusable(true);
    popupWindow.setWidth(250);
    //popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

    // set the list view as pop up window content
    popupWindow.setContentView(listViewSort);

    return popupWindow;
}

Visit my post for more details: http://www.devexchanges.info/2015/02/android-popupwindow-show-as-dropdown.html.
Hope this help!

1
  • You still need to address the horizontal portion of a horizontal list Commented Mar 10, 2020 at 13:11
0

Create a basic layout XML with an inner horizontal layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/popup_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>
</LinearLayout>

Assign the layout to the PopupMenu, then use the inner layout.

// PopupMenu accepts ViewGroup, so cast the inflated layout view
// Replace ROOT_VIEW_HERE with the parent view of the PopupMenu
LinearLayout popupWindow = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.popup_window, ROOT_VIEW_HERE);
PopupMenu popupMenu = new PopupMenu(this, popupWindow);
LinearLayout innerView = popupWindow.findViewById(R.id.popup_horizontal);

It really is that simple.

0

A complete solution is:

1 - The Custom PopupMenu class:

public class PopupMenuCustomLayout {
    private PopupMenuCustomOnClickListener onClickListener;
    private Context context;
    private PopupWindow popupWindow;
    private int rLayoutId;
    private View popupView;

    public PopupMenuCustomLayout(Context context, int rLayoutId, PopupMenuCustomOnClickListener onClickListener) {
        this.context = context;
        this.onClickListener = onClickListener;
        this.rLayoutId = rLayoutId;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        popupView = inflater.inflate(rLayoutId, null);
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true;
        popupWindow = new PopupWindow(popupView, width, height, focusable);
        popupWindow.setElevation(10);

        LinearLayout linearLayout = (LinearLayout) popupView;
        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            View v = linearLayout.getChildAt(i);
            v.setOnClickListener( v1 -> { onClickListener.onClick( v1.getId()); popupWindow.dismiss(); });
        }
    }
    public void setAnimationStyle( int animationStyle) {
        popupWindow.setAnimationStyle(animationStyle);
    }
    public void show() {
        popupWindow.showAtLocation( popupView, Gravity.CENTER, 0, 0);
    }

    public void show( View anchorView, int gravity, int offsetX, int offsetY) {
        popupWindow.showAsDropDown( anchorView, 0, -2 * (anchorView.getHeight()));
    }

    public interface PopupMenuCustomOnClickListener {
        public void onClick(int menuItemId);
    }
}

2 - Your custom layout, e.g. linearlayout with horizontal layout. In this case I use a simple LinearLayout with TextView items. You can use Buttons, etc.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/popup_menu_custom_item_a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="A"
        android:textAppearance="?android:textAppearanceMedium" />
    <TextView
        android:id="@+id/popup_menu_custom_item_b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="B"
        android:textAppearance="?android:textAppearanceMedium" />
    // ...
</LinearLayout>

3 - Using the Custom PopupMenu like the normal PopupMenu.

PopupMenuCustomLayout popupMenu = new PopupMenuCustomLayout(
        MainActivity.mainActivity, R.layout.popup_menu_custom_layout,
        new PopupMenuCustomLayout.PopupMenuCustomOnClickListener() {
            @Override
            public void onClick(int itemId) {
                // log statement: "Clicked on: " + itemId
                switch (itemId) {
                    case R.id.popup_menu_custom_item_a:
                        // log statement: "Item A was clicked!"
                        break;
                }
            }
        });
// Method 1: popupMenu.show();
// Method 2: via an anchor view: 
popupMenu.show( anchorView, Gravity.CENTER, 0, 0);
1
  • Isn't this a bit excessive to switch orientations, though? Commented Mar 10, 2020 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.