Rahul Gupta
Rahul Gupta

Reputation: 5295

Popup Window with Listview not Wrapped

I am trying to implement a popup window with listview. When i am adding items in listview, it is filling the screen below the anchor view.

Basically my listview is not wrapping. Below is the image and code which i am using :-

button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //              ToolTipView toolTipView = new ToolTipView(MainActivity.this, "");
            //              toolTipView.setText("swfwefw wefwe8732ryf89 8yu2fc h8wuijcb8unwi ");
            //              toolTipView.show(v);
            LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
            View popupView = layoutInflater.inflate(R.layout.popupwindoiw, null);

            final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            ListView btnDismiss = (ListView)popupView.findViewById(R.id.list);
            final ArrayList<String> list = new ArrayList<String>();
            list.add("hello");
            list.add("helloworld");
            list.add("helloexzeo");


            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_row, list);
            btnDismiss.setAdapter(adapter);
            btnDismiss.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                    Toast.makeText(MainActivity.this, list.get(arg2), Toast.LENGTH_SHORT).show();
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAsDropDown(v);

        }
    });

popwindow.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white" >

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ListView>

</RelativeLayout>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" />

In the image, currently listview is showing in the red region but i want it in blue region.

I cannot use custom width and height of a popupwindow as i am supporting multiple screen size enter image description here

Upvotes: 1

Views: 947

Answers (1)

Nagesh Jatagond
Nagesh Jatagond

Reputation: 11

You can measure the width of biggest view in adapter content and initiate the popwindow with this width.

You can use this method to measure the biggest view in adapter content -

private int measureContentWidth(ListAdapter listAdapter) {
ViewGroup mMeasureParent = null;
int maxWidth = 0;
View itemView = null;
int itemType = 0;

final ListAdapter adapter = listAdapter;
final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
final int count = adapter.getCount();
for (int i = 0; i < count; i++) {
    final int positionType = adapter.getItemViewType(i);
    if (positionType != itemType) {
        itemType = positionType;
        itemView = null;
    } 

    if (mMeasureParent == null) {
        mMeasureParent = new FrameLayout(mContext);
    } 

    itemView = adapter.getView(i, itemView, mMeasureParent);
    itemView.measure(widthMeasureSpec, heightMeasureSpec);

    final int itemWidth = itemView.getMeasuredWidth();

    if (itemWidth > maxWidth) {
        maxWidth = itemWidth;
    } 
} 

return maxWidth;

}

You can initiate popupwindow like this -

PopupWindow popupWindow = new PopupWindow(popupView,measureContentWidth(adapter), LayoutParams.WRAP_CONTENT);

Upvotes: 1

Related Questions