benkdev
benkdev

Reputation: 673

ListPopupWindow: setWidth, setHeight have no effect

I'm trying to set the width and height of a ListPopupWindow with no luck. I read about LayatParams but there is no setLayoutParams for ListPopupWindow. How do I set these parameters?

ListPopupWindow lpw = new ListPopupWindow(TestActivity.this);
CustomAdapter stringAdapter = new CustomAdapter(TestActivity.this, R.layout.row,strings);
lpw.setAdapter(stringAdapter);
lpw.setAnchorView(v);
lpw.setWidth(150);
lpw.setHeight(300);
lpw.show();

where v is a Button.

Upvotes: 2

Views: 4185

Answers (1)

Jermin Bazazian
Jermin Bazazian

Reputation: 1970

The code below is working very fine for me.

String[] listItems = {"item 1", "item 2 ", "list", "android", "item 3", "foobar", "bar", };
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btn=(Button)findViewById(R.id.btnwillappear);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ListPopupWindow lpw = new ListPopupWindow(Test_listpopActivity.this);
            lpw.setAdapter(new ArrayAdapter(Test_listpopActivity.this,  android.R.layout.simple_list_item_1, listItems));
            lpw.setAnchorView(findViewById(R.id.txtwillappear));
            lpw.setWidth(150);
            lpw.setHeight(300);
            lpw.show();

        }
    });

}

There are two possible problems. First is your adapter and the second is the place your code is being executed. For the latter make sure your code is not in onCreate.

Upvotes: 4

Related Questions