background
i'm trying to allow any view to show a drop down list (not a dialog, but a list that is shown below/above the view), as shown for spinners (starting from API 11 , as shown here) .
for this, i'm using IcsListPopupWindow.java from actionBarSherlock library.
the problem
i can't get it to show a list of items well:
the list doesn't show the thumb (of the scrollbar) when it get too large to be shown.
when selecting an item, it sometimes has the normal selection effect, and sometimes it make a lot of gray background, as shown here:
the question
what am i doing wrong? i've looked at the code of IcsListPopupWindow.java and it's quite hard to understand and see what is needed and what is not.
here's my sample code:
public class MainActivity extends Activity {
private PopupMenuAdapter mAdapter;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//prepare popup menu:
final PopupMenuItem[] items = new PopupMenuItem[10];
for (int i = 0; i < items.length; ++i)
items[i] = new PopupMenuItem(android.R.drawable.sym_def_app_icon, R.string.hello_world);
mAdapter = new PopupMenuAdapter(this, android.R.layout.simple_list_item_1, items);
final IcsListPopupWindow popupWindow = new IcsListPopupWindow(this, null, R.attr.popupMenuStyle); popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.abs__menu_dropdown_panel_holo_light));
popupWindow.setAdapter(mAdapter);
popupWindow.setModal(true);
popupWindow.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> arg0, final View arg1, final int arg2, final long arg3) {
popupWindow.dismiss();
}
});
//upon clicking on a button, show the popup menu :
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
// show popup menu:
popupWindow.setContentWidth(MainActivity.this.getWindowManager().getDefaultDisplay().getWidth() / 2);
mAdapter.notifyDataSetChanged(); // if you change anything
popupWindow.setAnchorView(v);
popupWindow.show();
}
});
}
// //////////////////////////////////////////////////////////
// PopupMenuAdapter //
// ///////////////////
private class PopupMenuAdapter extends ArrayAdapter<PopupMenuItem> {
Context context;
int layoutResourceId;
PopupMenuItem data[] = null;
public PopupMenuAdapter(final Context context, final int layoutResourceId, final PopupMenuItem[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
final LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResourceId, parent, false);
}
final PopupMenuItem pItem = data[position];
final TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText(pItem.textResId);
text.setCompoundDrawablesWithIntrinsicBounds(pItem.iconResId, 0, 0, 0);
return view;
}
}
private static class PopupMenuItem {
public int iconResId;
public int textResId;
public PopupMenuItem(final int iconResId, final int textResId) {
this.iconResId = iconResId;
this.textResId = textResId;
}
}
}
EDIT: i've now tried the ListPopupWindow.java file from HoloEverywhere library (and also imported just the files that it needs) , and it has the exact same problems.
EDIT: for the second problem (the selection effect), i've used my own customized listView item, which has a the next attribute for its root view:
android:background="@android:drawable/list_selector_background"
still no idea how to show the scrollbar. if i use :
mPopup.getListView().setVerticalScrollBarEnabled(true);
mPopup.getListView().setScrollbarFadingEnabled(false);
than for actionBarSherlock nothing happens, while for holoEverywhere i get an NPE :
07-03 14:28:09.359: E/AndroidRuntime(1869): java.lang.NullPointerException
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.View.onDrawScrollBars(View.java:5976)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.View.draw(View.java:6939)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.widget.AbsListView.draw(AbsListView.java:3030)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.View.draw(View.java:6936)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewRoot.draw(ViewRoot.java:1529)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewRoot.performTraversals(ViewRoot.java:1265)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.os.Looper.loop(Looper.java:130)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-03 14:28:09.359: E/AndroidRuntime(1869): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 14:28:09.359: E/AndroidRuntime(1869): at java.lang.reflect.Method.invoke(Method.java:507)
07-03 14:28:09.359: E/AndroidRuntime(1869): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-03 14:28:09.359: E/AndroidRuntime(1869): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-03 14:28:09.359: E/AndroidRuntime(1869): at dalvik.system.NativeStart.main(Native Method)