How do I replace my popupmenu code with ListPopupWindow?
Asked Answered
S

2

11

Currently , I am programatically creating a popupmenu which displays a list of floors and a title. However, changing the background color of just the title and adding a close button to title is turning out to be a nightmare.

I want to replace this popupmenu with a list popup window so I can add an XML file with background attribute for the title with a black color as the background and a close button on the right and white background for items in the menu. Is there a way I can achieve this with list popup window? Here's my code for that:

private void floorMenu(ImageView btnFloorMenu){
    MapData data = new MapDao(MyPlugin.mapId);
    final List<Floor> flList = dao.getFloors();
    // set popupMenu
    final PopupMenu floorsPm = new PopupMenu(MapViewActivity.this,btnFloorMenu);

    MenuItem titleItem =  floorsPm.getMenu().add(Menu.NONE, Menu.NONE, Menu.NONE, "Floors");

    int i = 1;
    for(Floor fl : flList)
    {
        floorsPm.getMenu().add(Menu.NONE, i,i, fl.getName());
        if(i>3)
            break;
        i++;
    }


    // add popup listener
    floorsPm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        //    onClick
        @Override
        public boolean onMenuItemClick(MenuItem item){
            //       get floorname

            int flOrder = item.getOrder();
            if(flOrder == Menu.NONE )
                return true;
            flOrder--;

            final String floorId = flList.get(flOrder).getMapId();

            //       set camera to floor

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    floorsPm.dismiss();
                    mapFragment.getMapManager().setCameraLayer(floorId, false);
                     Log.d(TAG, "post cameraLayer set");
                    changedSteps = true;
                    pauseNav();

                }
            });




            return true;
        }

    });
    floorsPm.show();


}
Sliding answered 24/4, 2017 at 16:13 Comment(1)
ListPopupWindow is backed by a ListView and offers few to no customization at all. You can find here a popup that can host any view. Just call setView(view) and than show().Heteroousian
S
13

Here is my example to create show a ListPopupWindow

First, create layout item_list_popup_window for each item of ListPopupWindow

<?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="#e4e4e4"
    android:paddingTop="1dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete" />

</LinearLayout>

Second, create an Adapter for your ListPopupWindow like

public class ListPopupWindowAdapter extends BaseAdapter{
    private Activity mActivity;
    private List<String> mDataSource = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private OnClickDeleteButtonListener clickDeleteButtonListener;

    ListPopupWindowAdapter(Activity activity, List<String> dataSource, @NonNull OnClickDeleteButtonListener clickDeleteButtonListener){
        this.mActivity = activity;
        this.mDataSource = dataSource;
        layoutInflater = mActivity.getLayoutInflater();
        this.clickDeleteButtonListener = clickDeleteButtonListener;
    }

    @Override
    public int getCount() {
        return mDataSource.size();
    }

    @Override
    public String getItem(int position) {
        return mDataSource.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_list_popup_window, null);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.text_title);
            holder.btnDelete = (Button) convertView.findViewById(R.id.button_delete);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        // bind data
        holder.tvTitle.setText(getItem(position));
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickDeleteButtonListener.onClickDeleteButton(position);
            }
        });
        return convertView;
    }

    public class ViewHolder{
        private TextView tvTitle;
        private Button btnDelete;
    }

    // interface to return callback to activity
    public interface OnClickDeleteButtonListener{
        void onClickDeleteButton(int position);
    }
}

Third, You create a function for create and show ListPopupWindow

private void showListPopupWindow(View anchorView) {
    final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
    listPopupWindow.setWidth(600);
    List<String> sampleData = new ArrayList<>();
    sampleData.add("A");
    sampleData.add("B");
    sampleData.add("CCCCCCCCCCCCCC");
    sampleData.add("D");
    sampleData.add("EEEEEEEEE");

    listPopupWindow.setAnchorView(anchorView);
    ListPopupWindowAdapter listPopupWindowAdapter = new ListPopupWindowAdapter(this, sampleData, new ListPopupWindowAdapter.OnClickDeleteButtonListener() {
        @Override
        public void onClickDeleteButton(int position) {
            Toast.makeText(MainActivity.this, "Click delete " + position, Toast.LENGTH_SHORT).show();
            listPopupWindow.dismiss();
        }
    });
    listPopupWindow.setAdapter(listPopupWindowAdapter);
    listPopupWindow.show();
}

Finally, you can show the ListPopupWindow by

showListPopupWindow(v);

for example, if you want to show it when click button

anyButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showListPopupWindow(v);
    }
});

enter image description here

Full Demo is here

Supper answered 27/4, 2017 at 6:13 Comment(2)
the solution looks great, will test it out. can you also modify it based on my code and also show me how i can modify the popup to slide in from left ?Sliding
this still isnt what i was looking for, but since you were the first to answer 50 points for youSliding
S
-2

Please Try this code, Maybe you wont like this

private void floorMenu(ImageView btnFloorMenu){
final Dialog customDialog = new Dialog(this);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.item_dialog_coustom_design);

TextView clickItem = (TextView)customDialog.findViewById(R.id.item_click);
TextView clickItem1 = (TextView)customDialog.findViewById(R.id.item_click1);
TextView clickItem2 = (TextView)customDialog.findViewById(R.id.item_click2);
Button btnClose = (Button)customDialog.findViewById(R.id.btn_close);

clickItem.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        customDialog.dismiss();
        // wright your Button Action
    }
});

clickItem1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        customDialog.dismiss();
        // wright your Button Action
    }
});

clickItem2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        customDialog.dismiss();
        // wright your Button Action
    }
});

btnClose.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        customDialog.dismiss();
    }
});
customDialog.show();

}

Create Linearlayout layout_width="280dp" layout_height="wrap_content" android:orientation="vertical" file name item_dialog_coustom_design.xml then put this code

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your Title"
        android:background="#000"
        android:textColor="#fff"
        android:padding="12dp"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:background="#fff"
        android:padding="10dp"
        android:text="Your Item"
        android:id="@+id/item_click"
        android:textSize="16sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:background="#fff"
        android:padding="10dp"
        android:text="Your Item"
        android:id="@+id/item_click1"
        android:textSize="16sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:background="#fff"
        android:padding="10dp"
        android:text="Your Item"
        android:id="@+id/item_click2"
        android:textSize="16sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:background="#fff"
        android:gravity="right">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_close"
                android:text="Close"/>
    </LinearLayout>
Stull answered 3/5, 2017 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.